Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4Analysis/TGo4ExportManager.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4ExportManager.h"
00017 
00018 #include <iostream.h>
00019 #include <stdexcept>
00020 
00021 #include "TSystem.h"
00022 #include "TFolder.h"
00023 #include "TDirectory.h"
00024 #include "TKey.h"
00025 #include "TFile.h"
00026 #include "TH1.h"
00027 #include "TH2.h"
00028 #include "TGraph.h"
00029 
00030 #include "Go4Log/TGo4Log.h"
00031 
00032 TGo4ExportManager::TGo4ExportManager(const Text_t* name)
00033    : TNamed(name,"This is a Go4 export manager"),fiFilter(GO4EX_ROOT)
00034 {
00035   fxOutFile="Go4Export";
00036 }
00037 
00038 
00039 TGo4ExportManager::TGo4ExportManager()
00040    :fiFilter(GO4EX_ROOT)
00041 {
00042 }
00043 
00044 TGo4ExportManager::~TGo4ExportManager()
00045 {
00046 }
00047 
00048 void TGo4ExportManager::SetCurrentDir(const Text_t* dir)
00049 {
00050   if(dir)
00051       {
00052          fxCurrentDir=dir;
00053          gSystem->cd(dir);
00054       }
00055   else
00056       {
00057           fxCurrentDir=gSystem->WorkingDirectory();
00058       }
00059 }
00060 
00061 void TGo4ExportManager::SetStartDir(const Text_t* dir)
00062 {
00063      if(dir)
00064          fxStartDir=dir;
00065      else
00066          fxStartDir=gSystem->WorkingDirectory();
00067 }
00068 
00069 void TGo4ExportManager::Export(TObject* ob, Go4Export_t format)
00070 {
00071 if(ob==0) return;
00072 SetFilter(format);
00073 Export(ob);
00074 
00075 }
00076 
00077 
00078 void TGo4ExportManager::Export(TObject* myobject)
00079 {
00080 if(myobject==0) return;
00081   if(myobject->InheritsFrom("TDirectory"))
00082     {
00083       TDirectory* subdir=dynamic_cast<TDirectory*>(myobject);
00084       Export(subdir);
00085     }
00086   else if (myobject->InheritsFrom("TFolder"))
00087     {
00088       TFolder* subfold=dynamic_cast<TFolder*> (myobject);
00089       Export(subfold);
00090     }
00091 else if (myobject->InheritsFrom("TCollection"))
00092     {
00093       TCollection* col=dynamic_cast<TCollection*> (myobject);
00094       Export(col);
00095     }
00096   else if(myobject->InheritsFrom("TH1"))
00097     {
00098       TH1* histo= dynamic_cast<TH1*>(myobject);
00099       Export(histo);
00100     }
00101   else if (myobject->InheritsFrom("TGraph"))
00102     {
00103       TGraph* graph= dynamic_cast<TGraph*>(myobject);
00104       Export(graph);
00105     }
00106   else
00107     {
00108       TGo4Log::Message(2,"ExportManager: NOT Converting object %s of class %s",
00109          myobject->GetName(),myobject->IsA()->GetName());
00110     }
00111 }
00112 
00113 
00114 void TGo4ExportManager::Export(TFolder* fold)
00115 {
00116 if(fold==0) return;
00117 TGo4Log::Message(0,"ExportManager: Converting contents of folder %s",fold->GetName());
00118 if(fiFilter==GO4EX_ROOT)
00119    {
00120        // root filter will write collection completely into one root file
00121        // otherwise, each object would be written separately into flat hierarchy
00122        ExportRoot(fold);
00123        return;
00124    }
00125 
00126 TString dirname=fold->GetName();
00127 gSystem->cd(fxCurrentDir.Data()); // create subdirectory in file system
00128 TString com="mkdir "+dirname;
00129 gSystem->Exec(com);
00130 gSystem->cd(dirname.Data());
00131 fxCurrentDir=gSystem->WorkingDirectory();
00132 TCollection* folderlist=fold->GetListOfFolders();
00133 Export(folderlist);
00134 gSystem->cd(fxCurrentDir.Data());
00135 gSystem->cd("..");
00136 fxCurrentDir=gSystem->WorkingDirectory(); // go up one directory level again
00137 gSystem->cd(fxStartDir.Data());
00138 
00139 }
00140 
00141 void TGo4ExportManager::Export(TDirectory* source)
00142 {
00143 if(source==0) return;
00144 TGo4Log::Message(0,"ExportManager: Converting contents of directory %s",source->GetName());
00145 if(fiFilter==GO4EX_ROOT)
00146    {
00147        // root filter will write collection completely into one root file
00148        // otherwise, each object would be written separately into flat hierarchy
00149        ExportRoot(source);
00150        return;
00151    }
00152 
00153 TString dirname=source->GetName();
00154 if(!dirname.Contains(".root"))
00155 {
00156   gSystem->cd(fxCurrentDir.Data()); // create subdirectory in file system
00157   TString com="mkdir "+dirname;
00158   gSystem->Exec(com);
00159   gSystem->cd(dirname.Data());
00160   fxCurrentDir=gSystem->WorkingDirectory();
00161 }
00162 TObject* myobject=0;
00163 TKey* mykey=0;
00164 source->cd();
00165 gSystem->cd(fxStartDir.Data());
00166 TIter iter(source->GetListOfKeys());
00167 while((mykey=(TKey*) iter())!=0)
00168   {
00169     myobject= mykey->ReadObj();
00170     if(myobject)
00171       {
00172         Export(myobject);
00173       }
00174     else
00175       {
00176         TGo4Log::Message(2,"ExportManager: Could not read key %s", mykey->GetName());
00177       }
00178   } // while
00179 if(!dirname.Contains(".root"))
00180    {
00181      gSystem->cd(fxCurrentDir.Data());
00182      gSystem->cd("..");
00183      fxCurrentDir=gSystem->WorkingDirectory(); // go up one directory level again
00184      gSystem->cd(fxStartDir.Data());
00185    }
00186 }
00187 
00188 void TGo4ExportManager::Export(TCollection* col)
00189 {
00190 if(col==0) return;
00191 TGo4Log::Message(0,"ExportManager: Converting contents of collection %s",col->GetName());
00192 if(fiFilter==GO4EX_ROOT)
00193    {
00194        // root filter will write collection completely into one root file
00195        // otherwise, each object would be written separately into flat hierarchy
00196        ExportRoot(col);
00197        return;
00198    }
00199 
00200 TIterator* citer=col->MakeIterator();
00201 TObject* ob=0;
00202 while((ob=citer->Next())!=0)
00203    {
00204       Export(ob);
00205    }
00206 delete citer;
00207 }
00208 
00209 void TGo4ExportManager::Export(TH1* histo)
00210 {
00211 switch(fiFilter)
00212    {
00213       case GO4EX_ASCII:
00214          ExportASCII(histo,kFALSE);
00215          break;
00216       case GO4EX_ASCII_CHANNELS:
00217          ExportASCII(histo,kTRUE);
00218          break;
00219       case GO4EX_RADWARE:
00220          ExportRadware(histo);
00221          break;
00222       case GO4EX_ROOT:
00223          ExportRoot(histo);
00224          break;
00225       default:
00226          TGo4Log::Message(2,"ExportManager: Unknown Export filter %d!",fiFilter);
00227          break;
00228    };
00229 
00230 }
00231 
00232 void TGo4ExportManager::Export(TGraph* gra)
00233 {
00234 switch(fiFilter)
00235    {
00236       case GO4EX_ASCII:
00237       case GO4EX_ASCII_CHANNELS:
00238          ExportASCII(gra);
00239          break;
00240       case GO4EX_RADWARE:
00241          ExportRadware(gra);
00242          break;
00243       case GO4EX_ROOT:
00244          ExportRoot(gra);
00245          break;
00246       default:
00247          TGo4Log::Message(2,"ExportManager: Unknown Export filter %d!",fiFilter);
00248          break;
00249    };
00250 
00251 }
00252 
00253 void TGo4ExportManager::ExportASCII(TH1* histo, Bool_t channels)
00254 {
00255 if(histo==0) return;
00256 try{
00257    TString objectname=histo->GetName();
00258    TString outname=objectname;
00259    outname.Append(".hdat");
00260    char buffer[256];
00261    snprintf(buffer,255,"%s",outname.Data());
00262    gSystem->cd(fxCurrentDir.Data());
00263    std::ofstream outfile(buffer);
00264    if(!outfile)
00265     {
00266       TGo4Log::Message(3,"ExportManager: Error opening outputfile %s",outname.Data());
00267       //cout <<"Error opening outputfile "<<outname.Data() << endl;
00268       return;
00269     }
00270    TGo4Log::Message(0,"ExportManager: Converting histogram %s to ASCII",histo->GetName());
00271    //cout <<"Converting histogram "<< histo->GetName() << endl;
00272    Int_t maxbinX=histo->GetNbinsX();
00273    Int_t maxbinY=histo->GetNbinsY();
00274    Int_t maxbinZ=histo->GetNbinsZ();
00275    Int_t globalbin=0;
00276    Stat_t  cont=0;
00277    outfile <<"# Histogram "<<histo->IsA()->GetName() <<": "<<histo->GetName()<<endl;
00278    if(channels)
00279       outfile <<"# Xbin \tYbin \tZbin \tContent"<<endl;
00280    else
00281       outfile <<"# X \tY \tZ \tContent"<<endl;
00282    for(Int_t x=1; x<=maxbinX; ++x)
00283     {
00284       for(Int_t y=1; y<=maxbinY; ++y)
00285         {
00286           for(Int_t z=1; z<=maxbinZ; ++z)
00287             {
00288               globalbin=histo->GetBin(x,y,z);
00289               cont=histo->GetBinContent(globalbin);
00290               if(channels)
00291                {
00292                   outfile <<x<<" \t"<<y<<" \t"<<z<<" \t"<<cont<<endl;
00293                }
00294               else
00295                {
00296                   Axis_t xval=0;
00297                   TAxis* xax=histo->GetXaxis();                  
00298                   //if(xax) xval=xax->GetBinCenter(x);
00299                   if(xax) xval=xax->GetBinLowEdge(x);
00300                   Axis_t yval=0;
00301                   TAxis* yax=histo->GetYaxis();
00302                   //if(yax) yval=yax->GetBinCenter(y);
00303                   if(yax) yval=yax->GetBinLowEdge(y);
00304                   Axis_t zval=0;
00305                   TAxis* zax=histo->GetZaxis();   
00306                   //if(zax) zval=zax->GetBinCenter(z);
00307                   if(zax) zval=zax->GetBinLowEdge(z);             
00308                   outfile <<xval<<" \t"<<yval<<" \t"<<zval<<" \t"<<cont<<endl;
00309                }  
00310             }
00311         }
00312 
00313     }
00314    outfile.close();
00315    gSystem->cd(fxStartDir.Data()); // needed in case of TKey read from (file) directory
00316 }// try
00317 catch(std::exception& ex) // treat standard library exceptions
00318 {
00319   TGo4Log::Message(3,"standard exception %s in TGo4ExportManager::ExportASCII(TH1*)",
00320             ex.what());
00321 }
00322 catch(...)
00323 {
00324   TGo4Log::Message(3,"!!! Unexpected exception in TGo4ExportManager::ExportASCII(TH1*)!!!");
00325 } // catch
00326 
00327 
00328 
00329 }
00330 
00331 void TGo4ExportManager::ExportASCII(TGraph* graph)
00332 {
00333 if(graph==0) return;
00334 try{
00335    TString objectname=graph->GetName();
00336    TString outname=objectname;
00337    outname.Append(".gdat");
00338    char buffer[256];
00339    snprintf(buffer,255,"%s",outname.Data());
00340    gSystem->cd(fxCurrentDir.Data());
00341    std::ofstream outfile(buffer);
00342    if(!outfile)
00343     {
00344               TGo4Log::Message(3,"ExportManager: Error opening outputfile %s",outname.Data());
00345               return;
00346     }
00347    TGo4Log::Message(0,"ExportManager: Converting graph %s to ASCII",graph->GetName());
00348    Int_t maxpoints=graph->GetN();
00349    outfile <<"# Graph "<<graph->IsA()->GetName() <<": "<<graph->GetName()<<endl;
00350    outfile <<"# Point \tX \tY"<<endl;
00351    for(Int_t point=0; point<maxpoints; ++point)
00352     {
00353       Double_t xg=0;
00354       Double_t yg=0;
00355       graph->GetPoint(point,xg,yg);
00356       outfile <<point<<" \t\t"<<xg<<" \t"<<yg<<endl;
00357     }
00358    outfile.close();
00359    gSystem->cd(fxStartDir.Data());
00360 }//try
00361 
00362 catch(std::exception& ex) // treat standard library exceptions
00363 {
00364   TGo4Log::Message(3,"standard exception %s in TGo4ExportManager::ExportASCII(TGraph*)",
00365             ex.what());
00366 }
00367 catch(...)
00368 {
00369   TGo4Log::Message(3,"!!! Unexpected exception in TGo4ExportManager::ExportASCII(TGraph*)!!!");
00370 } // catch
00371 
00372 
00373 
00374 }
00375 
00376 
00377 
00378 
00379 
00380 void TGo4ExportManager::ExportRadware(TH1* histo)
00381 {
00382 //TGo4Log::Message(2,"ExportManager: Converting histogram %s to radware not supported yet!",histo->GetName());
00383 //return;
00384 if(histo->InheritsFrom("TH2"))
00385    {
00386        TH2* map=dynamic_cast<TH2*>(histo);
00387        ExportRadware(map);
00388    }
00389 else if (histo->InheritsFrom("TH3"))
00390    {
00391      TGo4Log::Message(2,"ExportManager: Converting 3d histogram %s to radware not supported yet!",histo->GetName());
00392    }
00393 else
00394    {
00395 
00396 try{
00397    TString objectname=histo->GetName();
00398    TString outname=objectname;
00399    outname.Append(".spe");
00400    char buffer[256];
00401    snprintf(buffer,255,"%s",outname.Data());
00402    TString hname=objectname;
00403    hname.Append("        ");
00404    Int_t maxbinX=histo->GetNbinsX();
00405    Int_t l_chan=maxbinX; // get record size from histo here...
00406    Int_t l_head[6];
00407    l_head[0]=l_chan;
00408    l_head[1]=1;
00409    l_head[2]=1;
00410    l_head[3]=1;
00411    l_head[4]=24;
00412    l_head[5]=l_chan*4; /* record size */
00413    gSystem->cd(fxCurrentDir.Data());
00414    std::ofstream outfile(buffer);
00415   if(!outfile)
00416     {
00417       TGo4Log::Message(3,"ExportManager: Error opening outputfile %s",outname.Data());
00418       return;
00419     }
00420   TGo4Log::Message(0,"ExportManager: Converting 1d histogram %s to RADWARE",histo->GetName());
00422    Int_t first=24;
00423    outfile.write((char *) &first, sizeof(Int_t));
00425    outfile.write(hname.Data(), 8);// name limited to 8 characters
00427    outfile.write((char *) l_head, 6*sizeof(Int_t));
00429    Float_t cont=0;
00430    for(Int_t xbin=0; xbin<maxbinX; ++xbin)
00431        {
00432           cont=(Float_t) histo->GetBinContent(xbin);
00433           outfile.write( (char *) &cont, sizeof(Float_t) );
00434        }
00436    Int_t charnum=l_chan*sizeof(Float_t);
00437    outfile.write((char *) &charnum, sizeof(Int_t));
00438    Int_t hislen = l_chan*4 + 40;
00439    Int_t byteswritten=outfile.tellp();
00440    //cout <<"position in stream is "<<byteswritten << endl;
00441    //cout <<"datalength was"<<hislen << endl;
00442    // consistency check: chars written are data field+header size
00443    if (byteswritten == hislen)
00444    {
00445      TGo4Log::Message(1,"Histogram %s:  %d bytes (data %d) written to %s",
00446         hname.Data(), byteswritten, hislen, outname.Data());
00447    }
00448    else
00449    {
00450      TGo4Log::Message(3,"Histogram %s:  Size mismatch: %d bytes written to %s, datalength is %d",
00451         hname.Data(), byteswritten, outname.Data(), hislen);
00452    }
00453    outfile.close();
00454    gSystem->cd(fxStartDir.Data());
00455 }// try /////////////////////////////////////////////
00456 
00457 
00458 catch(std::exception& ex) // treat standard library exceptions
00459 {
00460   TGo4Log::Message(3,"standard exception %s in TGo4ExportManager::ExportRadware(TH1*)",
00461             ex.what());
00462 }
00463 catch(...)
00464 {
00465   TGo4Log::Message(3,"!!! Unexpected exception in TGo4ExportManager::ExportRadware(TH1*)!!!");
00466 } // catch
00467 
00468 }// if(histo->InheritsFrom...)
00469 
00470 }
00471 
00472 void TGo4ExportManager::ExportRadware(TH2* histo)
00473 {
00474    TGo4Log::Message(2,"ExportManager: Converting 2d histo %s to radware not supported yet!",histo->GetName());
00475 }
00476 void TGo4ExportManager::ExportRadware(TGraph* graph)
00477 {
00478 TGo4Log::Message(2,"ExportManager: Converting graph %s to radware not supported yet!",graph->GetName());
00479 }
00480 
00481 void TGo4ExportManager::ExportRoot(TObject* ob)
00482 {
00483 if(ob==0) return;
00484    TString fname=fxOutFile;
00485    TString ftitle=fxOutFileComment;
00486    if(!fname.Contains(".root")) fname.Append(".root");
00487    TFile f(fname.Data(), "recreate", ftitle.Data());
00488    f.cd();
00489    ob->Write();
00490    f.Close();
00491    TGo4Log::Message(0,"ExportManager: Wrote object %s of class %s to root file %s",
00492       ob->GetName(), ob->IsA()->GetName(),fname.Data() );
00493 }
00494 
00495 
00496 
00497 ClassImp(TGo4ExportManager)
00498 
00499 
00500 
00501 
00502 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:49 2005 for Go4-v2.10-5 by doxygen1.2.15