1 |
fronga |
1.1 |
// this class's header
|
2 |
|
|
#include "FWCore/MessageLogger/interface/MessageLogger.h"
|
3 |
|
|
|
4 |
|
|
#include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentIORootBase.h"
|
5 |
|
|
|
6 |
|
|
// ----------------------------------------------------------------------------
|
7 |
|
|
// open file/trees for write
|
8 |
|
|
|
9 |
flucke |
1.3 |
int AlignmentIORootBase::openRoot(const char* filename, int iteration, bool write)
|
10 |
fronga |
1.1 |
{
|
11 |
|
|
bWrite=write;
|
12 |
|
|
int iter;
|
13 |
|
|
|
14 |
|
|
edm::LogInfo("AlignmentIORootBase") << "File: " << filename ;
|
15 |
|
|
|
16 |
|
|
if (bWrite) { // writing
|
17 |
|
|
|
18 |
|
|
int iterfile = testFile(filename,treename);
|
19 |
|
|
if (iterfile == -1) {
|
20 |
|
|
iter=iteration;
|
21 |
|
|
edm::LogInfo("AlignmentIORootBase") << "Write to new file; first iteration: " << iter ;
|
22 |
|
|
IORoot = new TFile(filename,"recreate");
|
23 |
|
|
} else {
|
24 |
|
|
if (iteration == -1) {
|
25 |
|
|
iter=iterfile+1;
|
26 |
|
|
edm::LogInfo("AlignmentIORootBase")
|
27 |
|
|
<< "Write to existing file; highest iteration: " << iter;
|
28 |
|
|
}
|
29 |
|
|
else {
|
30 |
fpschill |
1.2 |
if (iteration<=iterfile) {
|
31 |
fronga |
1.1 |
edm::LogError("AlignmentIORootBase")
|
32 |
|
|
<< "Iteration " << iteration
|
33 |
|
|
<<" invalid or already exists for tree " << treename;
|
34 |
|
|
return -1;
|
35 |
|
|
}
|
36 |
|
|
iter = iteration;
|
37 |
|
|
edm::LogInfo("AlignmentIORootBase") << "Write to new iteration: " << iter;
|
38 |
|
|
}
|
39 |
|
|
IORoot = new TFile(filename,"update");
|
40 |
|
|
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
IORoot->cd();
|
44 |
|
|
|
45 |
|
|
// create tree
|
46 |
|
|
edm::LogInfo("AlignmentIORootBase") << "Tree: " << treeName(iter,treename);
|
47 |
|
|
tree = new TTree(treeName(iter,treename),treetxt);
|
48 |
|
|
createBranches();
|
49 |
|
|
|
50 |
|
|
} else { // reading
|
51 |
|
|
|
52 |
|
|
int iterfile = testFile(filename,treename);
|
53 |
|
|
if ( iterfile == -1 ) {
|
54 |
|
|
edm::LogError("AlignmentIORootBase") << "File does not exist!";
|
55 |
|
|
return -1;
|
56 |
fpschill |
1.2 |
} else if ( iterfile == -2 ) {
|
57 |
fronga |
1.1 |
edm::LogError("AlignmentIORootBase") << "Tree " << treename
|
58 |
|
|
<< " 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 |
fpschill |
1.2 |
if (iteration>iterfile) {
|
66 |
fronga |
1.1 |
edm::LogError("AlignmentIORootBase")
|
67 |
|
|
<<"Iteration " << iteration << " does not exist for tree " << treename;
|
68 |
|
|
return -1;
|
69 |
|
|
}
|
70 |
|
|
iter = iteration;
|
71 |
|
|
edm::LogInfo("AlignmentIORootBase") << "Read from specified iteration: " << iter;
|
72 |
|
|
}
|
73 |
|
|
IORoot = new TFile(filename,"update");
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
IORoot->cd();
|
77 |
|
|
// set trees
|
78 |
|
|
edm::LogInfo("AlignmentIORootBase") <<" Tree: " <<treeName(iter,treename);
|
79 |
|
|
tree = (TTree*)IORoot->Get(treeName(iter,treename));
|
80 |
|
|
if (tree==NULL) {
|
81 |
|
|
edm::LogError("AlignmentIORootBase") <<"Tree does not exist in file!";
|
82 |
|
|
return -1;
|
83 |
|
|
}
|
84 |
|
|
setBranchAddresses();
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
return 0;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
// ----------------------------------------------------------------------------
|
91 |
|
|
// write tree and close file
|
92 |
|
|
|
93 |
|
|
int AlignmentIORootBase::closeRoot(void)
|
94 |
|
|
{
|
95 |
|
|
if (bWrite) { //writing
|
96 |
|
|
IORoot->cd();
|
97 |
|
|
tree->Write();
|
98 |
|
|
tree->Delete();
|
99 |
|
|
IORoot->Close();
|
100 |
|
|
}
|
101 |
|
|
else { // reading
|
102 |
|
|
IORoot->cd();
|
103 |
|
|
tree->Delete();
|
104 |
|
|
IORoot->Close();
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
return 0;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
// ----------------------------------------------------------------------------
|
111 |
|
|
// returns highest existing iteration in file
|
112 |
|
|
// if file does not exist: return -1
|
113 |
|
|
|
114 |
flucke |
1.3 |
int AlignmentIORootBase::testFile(const char* filename, const TString &tname)
|
115 |
fronga |
1.1 |
{
|
116 |
|
|
FILE* testFILE;
|
117 |
|
|
testFILE = fopen(filename,"r");
|
118 |
|
|
if (testFILE == NULL) {
|
119 |
|
|
return -1;
|
120 |
|
|
}
|
121 |
|
|
else {
|
122 |
|
|
fclose(testFILE);
|
123 |
fpschill |
1.2 |
int ihighest=-2;
|
124 |
fronga |
1.1 |
TFile IORoot(filename,"update");
|
125 |
fpschill |
1.2 |
for (int iter=0; iter<itermax; iter++) {
|
126 |
fronga |
1.1 |
if ((0 != (TTree*)IORoot.Get(treeName(iter,tname)))
|
127 |
|
|
&& (iter>ihighest)) ihighest=iter;
|
128 |
|
|
}
|
129 |
|
|
IORoot.Close();
|
130 |
|
|
return ihighest;
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
// ----------------------------------------------------------------------------
|
135 |
|
|
// create tree name from stub+iteration
|
136 |
|
|
|
137 |
flucke |
1.3 |
TString AlignmentIORootBase::treeName(int iter, const TString &tname)
|
138 |
fronga |
1.1 |
{
|
139 |
flucke |
1.3 |
return TString(tname + Form(":%i",iter));
|
140 |
fronga |
1.1 |
}
|