CMS 3D CMS Logo

AlignmentIORootBase.cc
Go to the documentation of this file.
1 // this class's header
3 
5 
6 #include "TFile.h"
7 #include "TTree.h"
8 
10 {
11  delete myFile; // tree is deleted automatically with file
12 }
13 
14 // ----------------------------------------------------------------------------
15 // open file/trees for write
16 
18 {
19  bWrite=write;
20  int iter;
21 
22  edm::LogInfo("AlignmentIORootBase") << "File: " << filename ;
23 
24  if (bWrite) { // writing
25 
26  int iterfile = testFile(filename,treename);
27  if (iterfile == -1) {
28  iter=iteration;
29  edm::LogInfo("AlignmentIORootBase") << "Write to new file; first iteration: " << iter ;
30  myFile = TFile::Open(filename,"recreate");
31  } else {
32  if (iteration == -1) {
33  iter=iterfile+1;
34  edm::LogInfo("AlignmentIORootBase")
35  << "Write to existing file; highest iteration: " << iter;
36  } else {
37  if (iteration<=iterfile) {
38  edm::LogError("AlignmentIORootBase")
39  << "Iteration " << iteration
40  <<" invalid or already exists for tree " << treename;
41  return -1;
42  }
43  iter = iteration;
44  edm::LogInfo("AlignmentIORootBase") << "Write to new iteration: " << iter;
45  }
46  myFile = TFile::Open(filename,"update");
47 
48  }
49 
50  // create tree
51  myFile->cd();
52  edm::LogInfo("AlignmentIORootBase") << "Tree: " << treeName(iter,treename);
53  tree = new TTree(treeName(iter,treename),treetxt);
55 
56  } else { // reading
57 
58  int iterfile = testFile(filename,treename);
59  if ( iterfile == -1 ) {
60  edm::LogError("AlignmentIORootBase") << "File does not exist!";
61  return -1;
62  } else if ( iterfile == -2 ) {
63  edm::LogError("AlignmentIORootBase") << "Tree " << treename
64  << " does not exist in file " << filename;
65  return -1;
66  } else {
67  if (iteration == -1) {
68  iter=iterfile;
69  edm::LogInfo("AlignmentIORootBase") << "Read from highest iteration: " << iter;
70  } else {
71  if (iteration>iterfile) {
72  edm::LogError("AlignmentIORootBase")
73  << "Iteration " << iteration << " does not exist for tree " << treename;
74  return -1;
75  }
76  iter = iteration;
77  edm::LogInfo("AlignmentIORootBase") << "Read from specified iteration: " << iter;
78  }
79  myFile = TFile::Open(filename, "read");
80  }
81 
82  myFile->cd();
83  // set trees
84  edm::LogInfo("AlignmentIORootBase") <<" Tree: " <<treeName(iter,treename);
85  tree = (TTree*)myFile->Get(treeName(iter,treename));
86  if (tree==nullptr) {
87  edm::LogError("AlignmentIORootBase") <<"Tree does not exist in file!";
88  return -1;
89  }
91  }
92 
93  return 0;
94 }
95 
96 // ----------------------------------------------------------------------------
97 // write tree and close file
98 
100 {
101  if (bWrite) { //writing
102  tree->Write();
103  }
104 
105  delete myFile;
106  myFile = nullptr;
107  tree = nullptr; // deleted with file
108 
109  return 0;
110 }
111 
112 // ----------------------------------------------------------------------------
113 // returns highest existing iteration in file
114 // if file does not exist: return -1
115 
116 int AlignmentIORootBase::testFile(const char* filename, const TString &tname)
117 {
118  FILE* testFILE;
119  testFILE = fopen(filename,"r");
120  if (testFILE == nullptr) {
121  return -1;
122  } else {
123  fclose(testFILE);
124  int ihighest=-2;
125  TFile *aFile = TFile::Open(filename,"read");
126  for (int iter=0; iter<itermax; iter++) {
127  if ((nullptr != (TTree*)aFile->Get(treeName(iter,tname)))
128  && (iter>ihighest)) ihighest=iter;
129  }
130  delete aFile;
131  return ihighest;
132  }
133 }
134 
135 // ----------------------------------------------------------------------------
136 // create tree name from stub+iteration
137 
138 TString AlignmentIORootBase::treeName(int iter, const TString &tname)
139 {
140  return TString(tname + Form("_%i",iter));
141 }
TString treeName(int iter, const TString &tname)
compose tree name
static char const * tname
Definition: GTSchema.h:13
int testFile(const char *filename, const TString &tname)
test if file is existing and if so, what the highest iteration is
static const int itermax
virtual ~AlignmentIORootBase()
destructor
virtual void setBranchAddresses(void)=0
set root branches
int closeRoot(void)
close IO
int openRoot(const char *filename, int iteration, bool writemode)
open IO
virtual void createBranches(void)=0
create root branches
Definition: tree.py:1
def write(self, setup)