EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairParRootFileIo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairParRootFileIo.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 21/10/2004
3 //*-- Modified: 21/06/2005 Denis bertini
4 
6 // FairParRootFileIo
7 //
8 // Interface class for parameter I/O from/to ROOT file
9 // derived from interface base class FairParIo.
10 //
11 // It contains a pointer to the "connection class" of type FairParRootFile
12 // inherited from TFile. FairParRootFile contains an object of type FairRtdbRun
13 // which holds the list of container versions for an event file.
14 //
15 // It contains also a list of detector interface classes all having the common
16 // base type FairDetParRootFileIo. Every detector has its own interface class.
18 
19 
20 #include "FairParRootFileIo.h"
21 
22 #include "FairDetParIo.h"
23 #include "FairRuntimeDb.h"
24 #include "FairRtdbRun.h"
25 
26 //#include "TDirectory.h"
27 //#include "TROOT.h"
28 
29 #include <TKey.h>
30 #include <TObjString.h>
31 #include <TFileMerger.h>
32 
33 //#include <fstream>
34 //#include "stdio.h"
35 #include <iostream>
36 //#include <iomanip>
37 
38 using std::cout;
39 using std::cerr;
40 using std::endl;
41 
44 
45 //--------------------------------------------------------------------
46 FairParRootFile::FairParRootFile(const Text_t* fname, Option_t* option,
47  const Text_t* ftitle, Int_t compress)
48  :TNamed(fname, ftitle),
49  run(NULL),
50  RootFile(new TFile(fname,option,ftitle,compress))
51 {
52 // : TFile(fname,option,ftitle,compress) {
53  // constructor opens a ROOT file
54  // RootFile=new TFile(fname,option,ftitle,compress);
55  //run=0;
56 }
57 //--------------------------------------------------------------------
58 
59 //--------------------------------------------------------------------
61  :TNamed(f->GetName(), f->GetTitle()),
62  run(NULL),
63  RootFile(f)
64 {
65 // :TFile(f->GetName(),"UPDATE"){
66  // constructor opens a ROOT file
67  //RootFile=new TFile(f->GetName(),"UPDATE");
68  // RootFile=f;
69  // run=0;
70 }
71 //--------------------------------------------------------------------
72 
73 //--------------------------------------------------------------------
75 {
76  // destructor
77  if (run) {
78  delete run;
79  }
80  run=0;
81  //TODO: What about the file? Should it be closed or not
82 }
83 //--------------------------------------------------------------------
84 
85 //--------------------------------------------------------------------
87 {
88  // finds the current run containing the parameter container versions
89  // in the ROOT file
90  if (run) {
91  delete run;
92  }
93  run=(FairRtdbRun*)RootFile->Get(((char*)currentRun->GetName()));
94 }
95 //--------------------------------------------------------------------
96 
97 //--------------------------------------------------------------------
99  :FairParIo(),
100  file(NULL),
101  fMerging(kFALSE)
102 {
103  // constructor
104  // file=0;
105  // fMerging=kFALSE;
106 }
107 //--------------------------------------------------------------------
108 
109 //--------------------------------------------------------------------
111  :FairParIo(),
112  file(NULL),
113  fMerging(merged)
114 {
115  // constructor
116  // file=0;
117  // fMerging=merged;
118 
119 }
120 //--------------------------------------------------------------------
121 
122 //--------------------------------------------------------------------
124 {
125  // destructor closes an open file
126  close();
127 }
128 //--------------------------------------------------------------------
129 
130 //--------------------------------------------------------------------
131 Bool_t FairParRootFileIo::open(const Text_t* fname, Option_t* option,
132  const Text_t* ftitle, Int_t compress)
133 {
134  // It opens a ROOT file (default option "READ"). An open file will be closed.
135  // The detector I/Os for all detectors defined in the setup are activated.
136 
137  //DB, lock file for NFS in case of mulitple access
138  //gDirectory->ReadKeys();
139  close();
140  if (fMerging ) {
141  // used test merging
142  std::fstream* f = new std::fstream(fname);
143  if (f->good()) {
144  // check if file already exists
145  option = "UPDATE";
146  } else {
147  // if file doesn't exist recreate option is used
148  option = "RECREATE";
149  }
150  f->close();
151  delete f;
152  }
153 
154  file=new FairParRootFile(fname,option,ftitle,compress);
155 
156  if (file && file->IsOpen()) {
157  filename = fname;
159  return kTRUE;
160  }
161  cerr << "-E- Could not open input file " << fname << endl;
162  Fatal("open","Could not open input file");
163  return kFALSE;
164 }
165 //--------------------------------------------------------------------
166 
167 //--------------------------------------------------------------------
168 Bool_t FairParRootFileIo::open(const TList* fnamelist, Option_t* option,
169  const Text_t* ftitle, Int_t compress)
170 
171 {
172  TDatime currentDate;
173  TString newParFileName = "";
174  TFile* newParFile;
175 
176  TObjString* string;
177  TListIter myIter(fnamelist);
178 
179  TKey* inpKey;
180 
181  TFile* inFile;
182 
183  Int_t nofFiles = 0;
184  while((string = (TObjString*)myIter.Next())) {
185  inFile = TFile::Open(string->GetString().Data());
186  if ( !inFile ) {
187  cout << "-W- File \"" << string->GetString().Data() << "\" does not exist" << endl;
188  continue;
189  }
190 
191  if ( nofFiles == 0 ) {
192  newParFileName = string->GetString();
193  newParFileName.Replace(newParFileName.Last('/')+1,
194  newParFileName.Length(),"");
195  newParFileName = Form("%sallParams_%d_%d.root",
196  newParFileName.Data(),
197  currentDate.GetDate(),
198  currentDate.GetTime());
199  newParFile = new TFile(newParFileName.Data(),"RECREATE");
200  }
201 
202  TList* inputKeys = (TList*)inFile->GetListOfKeys();
203 
204  TListIter keyIter(inputKeys);
205  while((inpKey = (TKey*)keyIter.Next())) {
206  TObject* tempObj = inFile->Get(inpKey->GetName());
207 
208  newParFile->cd();
209  tempObj->Write();
210  }
211  inFile->Close();
212 
213  nofFiles++;
214  }
215  newParFile->Close();
216 
217  std::cout << "**** merged file = \"" << newParFileName.Data() << "\"" << std::endl;
218 
219  return this->open(newParFileName,option,ftitle,compress);
220 }
221 //--------------------------------------------------------------------
222 
223 //--------------------------------------------------------------------
224 Bool_t FairParRootFileIo::open(TFile* f)
225 {
226  // It opens a ROOT file (default option "READ"). An open file will be closed.
227  // The detector I/Os for all detectors defined in the setup are activated.
228 // close();
229  file=new FairParRootFile(f);
230  if (file && file->IsOpen()) {
231  filename = file->GetName();
233  return kTRUE;
234  }
235  return kFALSE;
236 }
237 //--------------------------------------------------------------------
238 
239 //--------------------------------------------------------------------
241 {
242  // closes an open ROOT file and deletes the detector I/Os
243  if (file) {
244  file->Close();
245  delete file;
246  file=0;
247  }
248  if (detParIoList) {
249  detParIoList->Delete();
250  }
251 }
252 //--------------------------------------------------------------------
253 
254 //--------------------------------------------------------------------
256 {
257  // prints the content of a open ROOT file and the list of detector I/Os
258  if (file) {
259  file->ls();
260  TIter next(detParIoList);
261  FairDetParIo* io;
262  cout<<"Root file I/O "<<file->GetName()<<" is open\n";
263  cout<<"detector I/Os: ";
264  while ((io=(FairDetParIo*)next())) {
265  cout<<" "<<io->GetName();
266  }
267  cout<<'\n';
268  } else {
269  cout<<"No ROOT file open\n";
270  }
271 }
272 //--------------------------------------------------------------------
273 
274 //--------------------------------------------------------------------
276 {
277  // returns a pointer to the current ROOT file
278  return file;
279 }
280 //--------------------------------------------------------------------
281 
282 //--------------------------------------------------------------------
284 {
285  // reads the parameter container versions for the current run from
286  // the ROOT file
287  if (file) {
288  file->readVersions(currentRun);
289  }
290 }
291 //--------------------------------------------------------------------
292 
293 //--------------------------------------------------------------------
295 {
296  // returns the list of keys found in the ROOT file
297  if (file) {
298  return file->GetListOfKeys();
299  }
300  return 0;
301 }
302 //--------------------------------------------------------------------