CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  delete myFile; // tree is deleted automatically with file
11 }
12 
13 // ----------------------------------------------------------------------------
14 // open file/trees for write
15 
17  bWrite = write;
18  int iter;
19 
20  edm::LogInfo("AlignmentIORootBase") << "File: " << filename;
21 
22  if (bWrite) { // writing
23 
24  int iterfile = testFile(filename, treename);
25  if (iterfile == -1) {
26  iter = iteration;
27  edm::LogInfo("AlignmentIORootBase") << "Write to new file; first iteration: " << iter;
28  myFile = TFile::Open(filename, "recreate");
29  } else {
30  if (iteration == -1) {
31  iter = iterfile + 1;
32  edm::LogInfo("AlignmentIORootBase") << "Write to existing file; highest iteration: " << iter;
33  } else {
34  if (iteration <= iterfile) {
35  edm::LogError("AlignmentIORootBase")
36  << "Iteration " << iteration << " invalid or already exists for tree " << treename;
37  return -1;
38  }
39  iter = iteration;
40  edm::LogInfo("AlignmentIORootBase") << "Write to new iteration: " << iter;
41  }
42  myFile = TFile::Open(filename, "update");
43  }
44 
45  // create tree
46  myFile->cd();
47  edm::LogInfo("AlignmentIORootBase") << "Tree: " << treeName(iter, treename);
48  tree = new TTree(treeName(iter, treename), treetxt);
50 
51  } else { // reading
52 
53  int iterfile = testFile(filename, treename);
54  if (iterfile == -1) {
55  edm::LogError("AlignmentIORootBase") << "File does not exist!";
56  return -1;
57  } else if (iterfile == -2) {
58  edm::LogError("AlignmentIORootBase") << "Tree " << treename << " does not exist in file " << filename;
59  return -1;
60  } else {
61  if (iteration == -1) {
62  iter = iterfile;
63  edm::LogInfo("AlignmentIORootBase") << "Read from highest iteration: " << iter;
64  } else {
65  if (iteration > iterfile) {
66  edm::LogError("AlignmentIORootBase") << "Iteration " << iteration << " does not exist for tree " << treename;
67  return -1;
68  }
69  iter = iteration;
70  edm::LogInfo("AlignmentIORootBase") << "Read from specified iteration: " << iter;
71  }
72  myFile = TFile::Open(filename, "read");
73  }
74 
75  myFile->cd();
76  // set trees
77  edm::LogInfo("AlignmentIORootBase") << " Tree: " << treeName(iter, treename);
78  tree = (TTree*)myFile->Get(treeName(iter, treename));
79  if (tree == nullptr) {
80  edm::LogError("AlignmentIORootBase") << "Tree does not exist in file!";
81  return -1;
82  }
84  }
85 
86  return 0;
87 }
88 
89 // ----------------------------------------------------------------------------
90 // write tree and close file
91 
93  if (bWrite) { //writing
94  tree->Write();
95  }
96 
97  delete myFile;
98  myFile = nullptr;
99  tree = nullptr; // deleted with file
100 
101  return 0;
102 }
103 
104 // ----------------------------------------------------------------------------
105 // returns highest existing iteration in file
106 // if file does not exist: return -1
107 
108 int AlignmentIORootBase::testFile(const char* filename, const TString& tname) {
109  FILE* testFILE;
110  testFILE = fopen(filename, "r");
111  if (testFILE == nullptr) {
112  return -1;
113  } else {
114  fclose(testFILE);
115  int ihighest = -2;
116  TFile* aFile = TFile::Open(filename, "read");
117  for (int iter = 0; iter < itermax; iter++) {
118  if ((nullptr != (TTree*)aFile->Get(treeName(iter, tname))) && (iter > ihighest))
119  ihighest = iter;
120  }
121  delete aFile;
122  return ihighest;
123  }
124 }
125 
126 // ----------------------------------------------------------------------------
127 // create tree name from stub+iteration
128 
129 TString AlignmentIORootBase::treeName(int iter, const TString& tname) { return TString(tname + Form("_%i", iter)); }
TString treeName(int iter, const TString &tname)
compose tree name
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
Log< level::Error, false > LogError
virtual ~AlignmentIORootBase()
destructor
tuple iteration
Definition: align_cfg.py:5
virtual void setBranchAddresses(void)=0
set root branches
Log< level::Info, false > LogInfo
int closeRoot(void)
close IO
int openRoot(const char *filename, int iteration, bool writemode)
open IO
virtual void createBranches(void)=0
create root branches
tuple filename
Definition: lut2db_cfg.py:20
std::string tname(const std::string &tableName, const std::string &schemaVersion)