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  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)); }
AlignmentIORootBase::closeRoot
int closeRoot(void)
close IO
Definition: AlignmentIORootBase.cc:92
MessageLogger.h
tree
Definition: tree.py:1
AlignmentIORootBase::treename
TString treename
Definition: AlignmentIORootBase.h:39
AlignmentIORootBase::myFile
TFile * myFile
Definition: AlignmentIORootBase.h:47
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
AlignmentIORootBase::treetxt
TString treetxt
Definition: AlignmentIORootBase.h:40
AlignmentIORootBase::treeName
TString treeName(int iter, const TString &tname)
compose tree name
Definition: AlignmentIORootBase.cc:129
AlignmentIORootBase.h
AlignmentIORootBase::setBranchAddresses
virtual void setBranchAddresses(void)=0
set root branches
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
AlignmentIORootBase::bWrite
bool bWrite
Definition: AlignmentIORootBase.h:41
AlignmentIORootBase::testFile
int testFile(const char *filename, const TString &tname)
test if file is existing and if so, what the highest iteration is
Definition: AlignmentIORootBase.cc:108
AlignmentIORootBase::createBranches
virtual void createBranches(void)=0
create root branches
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
tname
std::string tname(const std::string &tableName, const std::string &schemaVersion)
Definition: CredentialStore.cc:150
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
AlignmentIORootBase::openRoot
int openRoot(const char *filename, int iteration, bool writemode)
open IO
Definition: AlignmentIORootBase.cc:16
AlignmentIORootBase::~AlignmentIORootBase
virtual ~AlignmentIORootBase()
destructor
Definition: AlignmentIORootBase.cc:9
align_cfg.iteration
iteration
Definition: align_cfg.py:5
AlignmentIORootBase::itermax
const static int itermax
Definition: AlignmentIORootBase.h:44