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