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