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