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