EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDetParRootFileIo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDetParRootFileIo.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 21/10/2004
3 
5 // FairDetParRootFileIo
6 //
7 // Interface class to ROOT file for parameter input/output
8 // Base class for all detectors
9 //
10 // This class is derived from the common base class FairDetParIo
11 // for all inputs/outputs (ROOT file, Oracle, Ascii file).
12 // It contains all common functionality for all detectors which can be
13 // implemeted without knowing the exact type of the detector or of the detector
14 // dependend parameter containers using only the base class types.
16 
17 
18 #include "FairDetParRootFileIo.h"
19 
20 #include "FairParRootFileIo.h"
21 #include "FairRtdbRun.h"
22 #include "FairRuntimeDb.h"
23 #include "FairParSet.h"
24 
25 #include "TKey.h"
26 #include "TROOT.h"
27 
28 #include <iostream>
29 
30 using std::cout;
31 using std::endl;
32 
34 
36  :FairDetParIo(),
37  pFile(f)
38 {
39  // Constructor takes a pointer to the parameter ROOT file
40  // pFile=f;
41 }
42 
44 {
45  // generic read function for parameter containers
46  Text_t* name=(char*)pPar->GetName();
47  Int_t version=findInputVersion(name);
48  if (version<=0) {
49  pPar->setInputVersion(-1,inputNumber);
50  return kFALSE;
51  }
52  if (pPar->getInputVersion(inputNumber)==version
53  && pPar->getInputVersion(inputNumber)!=-1) { return kTRUE; }
54  TKey* key = (TKey*)gDirectory->GetKey(name,version);
55  if(key) {
56  pPar->clear();
57  key->Read(pPar);
58  pPar->setInputVersion(version,inputNumber);
59  pPar->setChanged();
60  cout<<"Container "<<pPar->GetName()<<" initialized from ROOT file."<<endl;
61  return kTRUE;
62  }
63  pPar->setInputVersion(-1,inputNumber);
64  return kFALSE;
65 }
66 
68 {
69  // writes a parameter container to the ROOT file and returns the new version
70  // number (returns -1 if the file is not writable)
71  if (pFile) {
72  pFile->cd();
73  if (pFile->IsWritable()) {
74  Text_t* name=(char*)pPar->GetName();
75  pPar->Write(name);
76  pPar->setChanged(kFALSE);
78  return getMaxVersion(name);
79  } else { Error(":write","Output is not writable"); }
80  }
81  Error(":write","No output open");
82  return -1;
83 }
84 
86 {
87  // returns the maximum version of the container given by name in the ROOT
88  // file (return -1 if not found)
89  TKey* key=pFile->GetKey(name);
90  if (key) { return key->GetCycle(); }
91  else { return -1; }
92 }
93 
95 {
96  // finds the input version to initialize the container given by name;
97  // returns -1 if the version cannot be determined
98  FairParVersion* currVers=
100  Int_t v=currVers->getInputVersion(inputNumber);
101  if (v>0) { return v; } // predefined
102  FairRtdbRun* r=pFile->getRun();
103  if (!r) { return -1; } // run not in ROOT file
104  FairParVersion* vers=r->getParVersion(name);
105  if (!vers) { return -1; } // container not in ROOT file
106  return vers->getRootVersion();
107 }
108 
110 {
111  // finds the parameter container given by its name with a special version in
112  // the ROOT file (returns 0 if not successful)
113  // This funtion uses internally the ROOT function FindObject(Text_t*), which
114  // creates a new object. The calling function must therefore delete the
115  // object after usage!
116  Text_t cn[80];
117  sprintf(cn,"%s;%i",name,vers);
118  pFile->cd();
119  TObject* p=gROOT->FindObject(cn);
120  return p;
121 }