FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairDetParRootFileIo.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 
12 // FairDetParRootFileIo
13 //
14 // Interface class to ROOT file for parameter input/output
15 // Base class for all detectors
16 //
17 // This class is derived from the common base class FairDetParIo
18 // for all inputs/outputs (ROOT file, Oracle, Ascii file).
19 // It contains all common functionality for all detectors which can be
20 // implemeted without knowing the exact type of the detector or of the detector
21 // dependend parameter containers using only the base class types.
23 #include "FairDetParRootFileIo.h"
24 
25 #include "FairParRootFileIo.h" // for FairParRootFile
26 #include "FairParSet.h" // for FairParSet
27 #include "FairRtdbRun.h" // for FairParVersion, FairRtdbRun
28 #include "FairRuntimeDb.h" // for FairRuntimeDb
29 
30 #include <TDirectory.h> // for TDirectory, gDirectory
31 #include <TKey.h> // for TKey
32 #include <TROOT.h> // for TROOT, gROOT
33 #include <iostream> // for operator<<, basic_ostream, etc
34 #include <stdio.h> // for sprintf
35 
36 class TObject;
37 
38 using std::cout;
39 using std::endl;
40 
42 
44  : FairDetParIo()
45  , pFile(f)
46 {
47  // Constructor takes a pointer to the parameter ROOT file
48  // pFile=f;
49 }
50 
52 {
53  // generic read function for parameter containers
54  Text_t *name = const_cast<char *>(pPar->GetName());
55  Int_t version = findInputVersion(name);
56 
57  // cout << "-I- FairDetParRootFileIo# " << name << " : " << version << endl;
58 
59  if (version <= 0) {
60  pPar->setInputVersion(-1, inputNumber);
61  return kFALSE;
62  }
63 
64  if (pPar->getInputVersion(inputNumber) == version && pPar->getInputVersion(inputNumber) != -1) {
65  return kTRUE;
66  }
67 
68  TKey *key = dynamic_cast<TKey *>(gDirectory->GetKey(name, version));
69  if (key) {
70  pPar->clear();
71  key->Read(pPar);
72  pPar->setInputVersion(version, inputNumber);
73  pPar->setChanged();
74  cout << "Container " << pPar->GetName() << " initialized from ROOT file." << endl;
75  return kTRUE;
76  }
77  pPar->setInputVersion(-1, inputNumber);
78  return kFALSE;
79 }
80 
82 {
83  // writes a parameter container to the ROOT file and returns the new version
84  // number (returns -1 if the file is not writable)
85  if (pFile) {
86  pFile->cd();
87  if (pFile->IsWritable()) {
88  Text_t *name = const_cast<char *>(pPar->GetName());
89  pPar->Write(name);
90  pPar->setChanged(kFALSE);
92  return getMaxVersion(name);
93  } else {
94  Error(":write", "Output is not writable");
95  }
96  }
97  Error(":write", "No output open");
98  return -1;
99 }
100 
102 {
103  // returns the maximum version of the container given by name in the ROOT
104  // file (return -1 if not found)
105  TKey *key = pFile->GetKey(name);
106  if (key) {
107  return key->GetCycle();
108  } else {
109  return -1;
110  }
111 }
112 
114 {
115  // finds the input version to initialize the container given by name;
116  // returns -1 if the version cannot be determined
118  Int_t v = currVers->getInputVersion(inputNumber);
119  if (v > 0) {
120  return v;
121  } // predefined
122  FairRtdbRun *r = pFile->getRun();
123  // cout << "-I- FairDetParRootFileIo::findInputVersion " << r << endl;
124  if (!r) {
125  return -1;
126  } // run not in ROOT file
127  FairParVersion *vers = r->getParVersion(name);
128  if (!vers) {
129  return -1;
130  } // container not in ROOT file
131  return vers->getRootVersion();
132 }
133 
134 TObject *FairDetParRootFileIo::findContainer(Text_t *name, Int_t vers)
135 {
136  // finds the parameter container given by its name with a special version in
137  // the ROOT file (returns 0 if not successful)
138  // This funtion uses internally the ROOT function FindObject(Text_t*), which
139  // creates a new object. The calling function must therefore delete the
140  // object after usage!
141  Text_t cn[80];
142  sprintf(cn, "%s;%i", name, vers);
143  pFile->cd();
144  TObject *p = gROOT->FindObject(cn);
145  return p;
146 }
virtual Bool_t read(FairParSet *)
virtual const char * GetName() const
Definition: FairParSet.h:38
void setVersionsChanged(Bool_t f=kTRUE)
Definition: FairRuntimeDb.h:86
static FairRuntimeDb * instance(void)
FairDetParRootFileIo(FairParRootFile *f)
FairParVersion * getParVersion(const Text_t *name)
void setChanged(Bool_t flag=kTRUE)
Definition: FairParSet.h:72
Int_t write(FairParSet *)
ClassImp(FairEventBuilder)
Int_t inputNumber
Definition: FairDetParIo.h:19
TKey * GetKey(Text_t *t)
Int_t getInputVersion(Int_t i)
Definition: FairParSet.h:57
Int_t findInputVersion(Text_t *contName)
FairRtdbRun * getCurrentRun(void)
Definition: FairRuntimeDb.h:78
virtual void clear()
Definition: FairParSet.h:45
Int_t getInputVersion(Int_t i)
Definition: FairRtdbRun.h:39
Int_t getRootVersion()
Definition: FairRtdbRun.h:54
void setInputVersion(Int_t v=-1, Int_t i=0)
Definition: FairParSet.h:51
TObject * findContainer(Text_t *contName, Int_t version)
Int_t getMaxVersion(Text_t *contName)
FairRtdbRun * getRun()
FairParRootFile * pFile