GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ExportManager.cxx
Go to the documentation of this file.
1 // $Id: TGo4ExportManager.cxx 999 2013-07-25 11:58:59Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4ExportManager.h"
15 
16 #include <stdexcept>
17 
18 #include "Riostream.h"
19 #include "TSystem.h"
20 #include "TFolder.h"
21 #include "TDirectory.h"
22 #include "TKey.h"
23 #include "TFile.h"
24 #include "TH1.h"
25 #include "TH2.h"
26 #include "TH3.h"
27 #include "TGraph.h"
28 
29 #include "TGo4Log.h"
30 
32  : TNamed(name,"This is a Go4 export manager"),fiFilter(GO4EX_ROOT)
33 {
34  fxOutFile="Go4Export";
35 }
36 
38  :fiFilter(GO4EX_ROOT)
39 {
40 }
41 
43 {
44 }
45 
47 {
48  fiFilter = format;
49 }
50 
51 void TGo4ExportManager::SetCurrentDir(const char* dir)
52 {
53  if(dir)
54  {
55  fxCurrentDir=dir;
56  gSystem->cd(dir);
57  }
58  else
59  {
60  fxCurrentDir=gSystem->WorkingDirectory();
61  }
62 }
63 
64 void TGo4ExportManager::SetStartDir(const char* dir)
65 {
66  if(dir)
67  fxStartDir=dir;
68  else
69  fxStartDir=gSystem->WorkingDirectory();
70 }
71 
72 void TGo4ExportManager::Export(TObject* ob, Go4Export_t format)
73 {
74 if(ob==0) return;
75 SetFilter(format);
76 Export(ob);
77 
78 }
79 
80 
81 void TGo4ExportManager::Export(TObject* myobject)
82 {
83  if(myobject==0) return;
84  if(myobject->InheritsFrom(TDirectory::Class()))
85  {
86  TDirectory* subdir=dynamic_cast<TDirectory*>(myobject);
87  Export(subdir);
88  }
89  else if (myobject->InheritsFrom(TFolder::Class()))
90  {
91  TFolder* subfold=dynamic_cast<TFolder*> (myobject);
92  Export(subfold);
93  }
94  else if (myobject->InheritsFrom(TCollection::Class()))
95  {
96  TCollection* col=dynamic_cast<TCollection*> (myobject);
97  Export(col);
98  }
99  else if(myobject->InheritsFrom(TH1::Class()))
100  {
101  TH1* histo= dynamic_cast<TH1*>(myobject);
102  Export(histo);
103  }
104  else if (myobject->InheritsFrom(TGraph::Class()))
105  {
106  TGraph* graph= dynamic_cast<TGraph*>(myobject);
107  Export(graph);
108  }
109  else
110  switch(fiFilter) {
111  case GO4EX_ROOT:
112  ExportRoot(myobject);
113  break;
114  case GO4EX_XML:
115  ExportXML(myobject);
116  break;
117  default:
118  TGo4Log::Message(2,"ExportManager: NOT Converting object %s of class %s",
119  myobject->GetName(),myobject->ClassName());
120  }
121 }
122 
123 
124 void TGo4ExportManager::Export(TFolder* fold)
125 {
126 if(fold==0) return;
127 TGo4Log::Message(0,"ExportManager: Converting contents of folder %s",fold->GetName());
128 if(fiFilter==GO4EX_ROOT)
129  {
130  // root filter will write collection completely into one root file
131  // otherwise, each object would be written separately into flat hierarchy
132  ExportRoot(fold);
133  return;
134  }
135 
136 TString dirname=fold->GetName();
137 gSystem->cd(fxCurrentDir.Data()); // create subdirectory in file system
138 TString com="mkdir "+dirname;
139 gSystem->Exec(com);
140 gSystem->cd(dirname.Data());
141 fxCurrentDir=gSystem->WorkingDirectory();
142 TCollection* folderlist=fold->GetListOfFolders();
143 Export(folderlist);
144 gSystem->cd(fxCurrentDir.Data());
145 gSystem->cd("..");
146 fxCurrentDir=gSystem->WorkingDirectory(); // go up one directory level again
147 gSystem->cd(fxStartDir.Data());
148 
149 }
150 
151 void TGo4ExportManager::Export(TDirectory* source)
152 {
153 if(source==0) return;
154 TGo4Log::Message(0,"ExportManager: Converting contents of directory %s",source->GetName());
155 if(fiFilter==GO4EX_ROOT)
156  {
157  // root filter will write collection completely into one root file
158  // otherwise, each object would be written separately into flat hierarchy
159  ExportRoot(source);
160  return;
161  }
162 
163 TString dirname=source->GetName();
164 if(!dirname.Contains(".root"))
165 {
166  gSystem->cd(fxCurrentDir.Data()); // create subdirectory in file system
167  TString com="mkdir "+dirname;
168  gSystem->Exec(com);
169  gSystem->cd(dirname.Data());
170  fxCurrentDir=gSystem->WorkingDirectory();
171 }
172 TObject* myobject=0;
173 TKey* mykey=0;
174 source->cd();
175 gSystem->cd(fxStartDir.Data());
176 TIter iter(source->GetListOfKeys());
177 while((mykey=(TKey*) iter())!=0)
178  {
179  myobject= mykey->ReadObj();
180  if(myobject)
181  {
182  Export(myobject);
183  }
184  else
185  {
186  TGo4Log::Message(2,"ExportManager: Could not read key %s", mykey->GetName());
187  }
188  } // while
189 if(!dirname.Contains(".root"))
190  {
191  gSystem->cd(fxCurrentDir.Data());
192  gSystem->cd("..");
193  fxCurrentDir=gSystem->WorkingDirectory(); // go up one directory level again
194  gSystem->cd(fxStartDir.Data());
195  }
196 }
197 
198 void TGo4ExportManager::Export(TCollection* col)
199 {
200  if(col==0) return;
201  TGo4Log::Message(0,"ExportManager: Converting contents of collection %s",col->GetName());
202  if(fiFilter==GO4EX_ROOT) {
203  // root filter will write collection completely into one root file
204  // otherwise, each object would be written separately into flat hierarchy
205  ExportRoot(col);
206  return;
207  }
208 
209  TIter iter(col);
210  TObject* ob=0;
211  while((ob=iter())!=0)
212  Export(ob);
213 }
214 
216 {
217 switch(fiFilter)
218  {
219  case GO4EX_ASCII:
220  ExportASCII(histo,kFALSE);
221  break;
223  ExportASCII(histo,kTRUE);
224  break;
225  case GO4EX_RADWARE:
226  ExportRadware(histo);
227  break;
228  case GO4EX_ROOT:
229  ExportRoot(histo);
230  break;
231  case GO4EX_XML:
232  ExportXML(histo);
233  break;
234  default:
235  TGo4Log::Message(2,"ExportManager: Unknown Export filter %d!",fiFilter);
236  break;
237  };
238 
239 }
240 
241 void TGo4ExportManager::Export(TGraph* gra)
242 {
243 switch(fiFilter)
244  {
245  case GO4EX_ASCII:
247  ExportASCII(gra);
248  break;
249  case GO4EX_RADWARE:
250  ExportRadware(gra);
251  break;
252  case GO4EX_XML:
253  ExportXML(gra);
254  break;
255  case GO4EX_ROOT:
256  ExportRoot(gra);
257  break;
258  default:
259  TGo4Log::Message(2,"ExportManager: Unknown Export filter %d!",fiFilter);
260  break;
261  };
262 
263 }
264 
265 void TGo4ExportManager::ExportASCII(TH1* histo, Bool_t channels)
266 {
267 if(histo==0) return;
268 try{
269  TString objectname=histo->GetName();
270  TString outname=objectname;
271  outname.Append(".hdat");
272  gSystem->cd(fxCurrentDir.Data());
273 
274  std::ofstream outfile(outname.Data());
275  if(!outfile)
276  {
277  TGo4Log::Message(3,"ExportManager: Error opening outputfile %s",outname.Data());
278  //std::cout <<"Error opening outputfile "<<outname.Data() << std::endl;
279  return;
280  }
281  TGo4Log::Message(0,"ExportManager: Converting histogram %s to ASCII",histo->GetName());
282  //std::cout <<"Converting histogram "<< histo->GetName() << std::endl;
283  Int_t maxbinX=histo->GetNbinsX();
284  Int_t maxbinY=histo->GetNbinsY();
285  Int_t maxbinZ=histo->GetNbinsZ();
286  Int_t globalbin=0;
287  Stat_t cont=0;
288  outfile <<"# Histogram "<<histo->ClassName() <<": "<<histo->GetName()<< std::endl;
289  if(channels)
290  outfile <<"# Xbin \tYbin \tZbin \tContent"<< std::endl;
291  else
292  outfile <<"# X \tY \tZ \tContent"<< std::endl;
293  for(Int_t x=1; x<=maxbinX; ++x)
294  {
295  for(Int_t y=1; y<=maxbinY; ++y)
296  {
297  for(Int_t z=1; z<=maxbinZ; ++z)
298  {
299  globalbin=histo->GetBin(x,y,z);
300  cont=histo->GetBinContent(globalbin);
301  if(channels)
302  {
303  outfile <<x<<" \t"<<y<<" \t"<<z<<" \t"<<cont<< std::endl;
304  }
305  else
306  {
307  Axis_t xval=0;
308  TAxis* xax=histo->GetXaxis();
309  //if(xax) xval=xax->GetBinCenter(x);
310  if(xax) xval=xax->GetBinLowEdge(x);
311  Axis_t yval=0;
312  TAxis* yax=histo->GetYaxis();
313  //if(yax) yval=yax->GetBinCenter(y);
314  if(yax) yval=yax->GetBinLowEdge(y);
315  Axis_t zval=0;
316  TAxis* zax=histo->GetZaxis();
317  //if(zax) zval=zax->GetBinCenter(z);
318  if(zax) zval=zax->GetBinLowEdge(z);
319  outfile <<xval<<" \t"<<yval<<" \t"<<zval<<" \t"<<cont<< std::endl;
320  }
321  }
322  }
323 
324  }
325  outfile.close();
326  gSystem->cd(fxStartDir.Data()); // needed in case of TKey read from (file) directory
327 }// try
328 catch(std::exception& ex) // treat standard library exceptions
329 {
330  TGo4Log::Message(3,"standard exception %s in TGo4ExportManager::ExportASCII(TH1*)",
331  ex.what());
332 }
333 catch(...)
334 {
335  TGo4Log::Message(3,"!!! Unexpected exception in TGo4ExportManager::ExportASCII(TH1*)!!!");
336 } // catch
337 
338 
339 
340 }
341 
343 {
344 if(graph==0) return;
345 try{
346  TString objectname=graph->GetName();
347  TString outname=objectname;
348  outname.Append(".gdat");
349  gSystem->cd(fxCurrentDir.Data());
350  std::ofstream outfile(outname.Data());
351  if(!outfile) {
352  TGo4Log::Message(3,"ExportManager: Error opening outputfile %s",outname.Data());
353  return;
354  }
355  TGo4Log::Message(0,"ExportManager: Converting graph %s to ASCII",graph->GetName());
356  Int_t maxpoints=graph->GetN();
357  outfile <<"# Graph "<<graph->ClassName() <<": "<<graph->GetName()<< std::endl;
358  outfile <<"# Point \tX \tY"<< std::endl;
359  for(Int_t point=0; point<maxpoints; ++point)
360  {
361  Double_t xg=0;
362  Double_t yg=0;
363  graph->GetPoint(point,xg,yg);
364  outfile <<point<<" \t\t"<<xg<<" \t"<<yg<< std::endl;
365  }
366  outfile.close();
367  gSystem->cd(fxStartDir.Data());
368 }//try
369 
370 catch(std::exception& ex) // treat standard library exceptions
371 {
372  TGo4Log::Message(3,"standard exception %s in TGo4ExportManager::ExportASCII(TGraph*)",
373  ex.what());
374 }
375 catch(...)
376 {
377  TGo4Log::Message(3,"!!! Unexpected exception in TGo4ExportManager::ExportASCII(TGraph*)!!!");
378 } // catch
379 
380 
381 
382 }
383 
384 
385 
386 
387 
389 {
390 //TGo4Log::Message(2,"ExportManager: Converting histogram %s to radware not supported yet!",histo->GetName());
391 //return;
392 if(histo->InheritsFrom(TH2::Class()))
393  {
394  TH2* map=dynamic_cast<TH2*>(histo);
395  ExportRadware(map);
396  }
397 else if (histo->InheritsFrom(TH3::Class()))
398  {
399  TGo4Log::Message(2,"ExportManager: Converting 3d histogram %s to radware not supported yet!",histo->GetName());
400  }
401 else
402  {
403 
404 try{
405  TString objectname=histo->GetName();
406  TString outname=objectname;
407  outname.Append(".spe");
408  TString hname = objectname;
409  hname.Append(" ");
410  Int_t maxbinX=histo->GetNbinsX();
411  Int_t l_chan=maxbinX; // get record size from histo here...
412  Int_t l_head[6];
413  l_head[0]=l_chan;
414  l_head[1]=1;
415  l_head[2]=1;
416  l_head[3]=1;
417  l_head[4]=24;
418  l_head[5]=l_chan*4; /* record size */
419  gSystem->cd(fxCurrentDir.Data());
420  std::ofstream outfile(outname.Data());
421  if(!outfile)
422  {
423  TGo4Log::Message(3,"ExportManager: Error opening outputfile %s",outname.Data());
424  return;
425  }
426  TGo4Log::Message(0,"ExportManager: Converting 1d histogram %s to RADWARE",histo->GetName());
428  Int_t first=24;
429  outfile.write((char *) &first, sizeof(Int_t));
431  outfile.write(hname.Data(), 8);// name limited to 8 characters
433  outfile.write((char *) l_head, 6*sizeof(Int_t));
435  Float_t cont=0;
436  for(Int_t xbin=0; xbin<maxbinX; ++xbin)
437  {
438  cont=(Float_t) histo->GetBinContent(xbin);
439  outfile.write( (char *) &cont, sizeof(Float_t) );
440  }
442  Int_t charnum=l_chan*sizeof(Float_t);
443  outfile.write((char *) &charnum, sizeof(Int_t));
444  Int_t hislen = l_chan*4 + 40;
445  Int_t byteswritten=outfile.tellp();
446  //std::cout <<"position in stream is "<<byteswritten << std::endl;
447  //std::cout <<"datalength was"<<hislen << std::endl;
448  // consistency check: chars written are data field+header size
449  if (byteswritten == hislen)
450  {
451  TGo4Log::Message(1,"Histogram %s: %d bytes (data %d) written to %s",
452  hname.Data(), byteswritten, hislen, outname.Data());
453  }
454  else
455  {
456  TGo4Log::Message(3,"Histogram %s: Size mismatch: %d bytes written to %s, datalength is %d",
457  hname.Data(), byteswritten, outname.Data(), hislen);
458  }
459  outfile.close();
460  gSystem->cd(fxStartDir.Data());
461 }// try /////////////////////////////////////////////
462 
463 
464 catch(std::exception& ex) // treat standard library exceptions
465 {
466  TGo4Log::Message(3,"standard exception %s in TGo4ExportManager::ExportRadware(TH1*)",
467  ex.what());
468 }
469 catch(...)
470 {
471  TGo4Log::Message(3,"!!! Unexpected exception in TGo4ExportManager::ExportRadware(TH1*)!!!");
472 } // catch
473 
474 }// if(histo->InheritsFrom...)
475 
476 }
477 
479 {
480  TGo4Log::Message(2,"ExportManager: Converting 2d histo %s to radware not supported yet!",histo->GetName());
481 }
483 {
484 TGo4Log::Message(2,"ExportManager: Converting graph %s to radware not supported yet!",graph->GetName());
485 }
486 
488 {
489  if (ob==0) return;
490  TString fname=ob->GetName();
491  fname += ".xml";
492 
493  TFile* f = TFile::Open(fname.Data(), "recreate");
494  if (f==0) return;
495  f->cd();
496  ob->Write();
497  delete f;
498  TGo4Log::Message(0,"ExportManager: Wrote object %s of class %s to root file %s",
499  ob->GetName(), ob->ClassName(),fname.Data() );
500 }
501 
502 
504 {
505  if(ob==0) return;
506  TString fname=fxOutFile;
507  TString ftitle=fxOutFileComment;
508  if(!fname.Contains(".root")) fname.Append(".root");
509  TFile* f = TFile::Open(fname.Data(), "recreate");
510  if (f==0) return;
511  f->SetTitle(ftitle.Data());
512  f->cd();
513  ob->Write();
514  delete f;
515  TGo4Log::Message(0,"ExportManager: Wrote object %s of class %s to root file %s",
516  ob->GetName(), ob->ClassName(),fname.Data() );
517 }
void SetStartDir(const char *dir=0)
void ExportASCII(TH1 *histo, Bool_t channels=kTRUE)
void SetCurrentDir(const char *dir=0)
void ExportXML(TObject *ob)
void ExportRadware(TH1 *histo)
void ExportRoot(TObject *ob)
void SetFilter(Go4Export_t format)
static const char * Message(Int_t prio, const char *text,...)
Definition: TGo4Log.cxx:209
void Export(TObject *ob, Go4Export_t format)
Go4Export_t