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