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