Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4ExportManager.cxx

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

Generated on Fri Nov 28 12:59:28 2008 for Go4-v3.04-1 by  doxygen 1.4.2