-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootalias.C
323 lines (265 loc) · 7.78 KB
/
rootalias.C
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// The paths to store ROOT files
vector<string> Paths;
void GenPathList()
{
TString rootdir = gSystem->Getenv("ROOTFILEDIR");
if (rootdir.Length()==0) {
Info("rootalias.C","The path to ROOT files is not set.");
return;
}
Paths.push_back(rootdir.Data());
Int_t curr_dir = 0;
while (curr_dir<Paths.size()) {
TSystemDirectory folder(Paths[curr_dir].c_str(), Paths[curr_dir].c_str());
TList* files = folder.GetListOfFiles();
if (files) {
files->Sort();
TSystemFile* file = 0;
TIter next(files);
while ( (file = (TSystemFile*)next()) ) {
if ( file->IsDirectory() && strcmp(file->GetName(),".")!=0 && strcmp(file->GetName(),"..")!=0 ) {
Paths.push_back(file->GetTitle());
}
}
}
curr_dir++;
//if (Paths.size()>10) return;
}
}
// Chain all the ROOT files for a given run number and return the TChain
// run --- run number
// path --- Directory to look for the ROOT files
// tree --- Name of the tree to be loaded from the ROOT files. Can be "T", "E", "LEFT", ...
// debug --- Debug level
TChain* LoadGmpRun(Int_t run, const char* path, const char* tree, Int_t debug = 1)
{
TChain* T = new TChain(tree);
TString basename = Form("gmp_%d",run);
TString rootfile = basename + ".root";
TString dir = path;
if (!dir.EndsWith("/")) dir.Append("/");
rootfile.Prepend(dir.Data());
Long_t split = 0;
while ( !gSystem->AccessPathName(rootfile.Data()) ) {
T->Add(rootfile.Data());
cout << "ROOT file " << rootfile << " added to TChain." << endl;
split++;
rootfile = basename + "_" + split + ".root";
rootfile.Prepend(dir.Data());
}
if (split<=0) {
if (debug>0) cerr << "Can not find ROOT file for run " << run << endl;
delete T;
T = 0;
}
return T;
}
// Load TTree "tree" form all the ROOT files generated from a specific run
// The ROOT files are searched for from the given "Paths" array given at the beginning of the file
// run --- run number
// tree --- The name of the tree to be loaded
TChain* LoadGmpRun(Int_t run, const char* tree = "T")
{
Int_t i=0;
TChain* T = 0;
if (Paths.size()==0) GenPathList();
while (i<Paths.size()) {
T = LoadGmpRun(run,Paths[i++].c_str(),tree,0);
if (T) break;
}
if (!T)
cerr << "Can not find ROOT file for run " << run << endl;
return T;
}
// Return run number of a given run
Int_t GetRunNumber(TTree* T)
{
Int_t runnumber = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
runnumber = run->GetNumber();
delete run;
}
return runnumber;
}
// Return date of a given run
TDatime GetRunDate(TTree* T)
{
TDatime run_time("2018-01-01 00:00:00");
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
run_time = run->GetDate();
delete run;
}
return run_time;
}
// Load a variable from the run database
Double_t GetValueFromDB(TTree* T, const char* varname)
{
Double_t variable = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
Int_t run_number = run->GetNumber();
TDatime run_time = run->GetDate();
TString name = varname;
FILE* file = THaAnalysisObject::OpenFile("run",run_time,"","r",0);
if (THaAnalysisObject::LoadDBvalue(file, run_time, name, variable)!=0) {
if (run_number<20000)
name.Prepend("L.");
else
name.Prepend("R.");
if (THaAnalysisObject::LoadDBvalue(file, run_time, name, variable)!=0)
variable = 0;
}
fclose(file);
delete run;
}
return variable;
}
/*
Double_t GetCentralMom(TTree* T)
{
Double_t mom = 0;
THaRun* run = 0;
THaHRS* hrs = 0;
T->GetDirectory()->GetObject("Run_Data",run);
if (run) {
Int_t run_number = run->GetNumber();
TDatime run_time = run->GetDate();
if (run_number<20000)
hrs = new THaHRS("L","Left arm HRS");
else
hrs = new THaHRS("R","Right arm HRS");
hrs->Init(run_time);
mom = hrs->GetPcentral();
delete hrs;
delete run;
}
return mom;
}
*/
/*
// Return central momentum of a given run
Double_t GetCentralMom(TTree* T)
{
Double_t mom = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
Int_t run_number = run->GetNumber();
TDatime run_time = run->GetDate();
FILE* file = THaAnalysisObject::OpenFile("run",run_time,"","r",0);
TString name = ".pcentral";
if (run_number<20000)
name.Prepend("L");
else
name.Prepend("R");
THaAnalysisObject::LoadDBvalue(file, run_time, name, mom);
fclose(file);
delete run;
}
return mom;
}
*/
// Return central momentum of a given run
Double_t GetCentralMom(TTree* T)
{
return GetValueFromDB(T, "pcentral");
}
// Return prescale factor for a given trigger
Int_t GetPrescale(TTree* T, Int_t trig)
{
if (trig<=0 || trig>8) return 0;
Int_t prescale = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
prescale = run->GetParameters()->GetPrescales()[trig-1];
delete run;
}
return prescale;
}
/*
// Return spectrometer angle of a given run in degree
// Negative for right HRS
// Positive for left HRS
Double_t GetSpecAngle(TTree* T)
{
Double_t angle = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
Int_t run_number = run->GetNumber();
TDatime run_time = run->GetDate();
FILE* file = THaAnalysisObject::OpenFile("run",run_time,"","r",0);
TString name = ".theta";
if (run_number<20000)
name.Prepend("L");
else
name.Prepend("R");
THaAnalysisObject::LoadDBvalue(file, run_time, name, angle);
fclose(file);
delete run;
}
return angle;
}
*/
// Return spectrometer angle of a given run in degree
// Negative for right HRS
// Positive for left HRS
Double_t GetSpecAngle(TTree* T)
{
return GetValueFromDB(T, "theta");
}
/*
// Return beam energy in a given run
Double_t GetBeamEnergy(TTree* T)
{
Double_t ebeam = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
ebeam = run->GetParameters()->GetBeamE();
delete run;
}
return ebeam;
}
*/
// Return beam energy in a given run
Double_t GetBeamEnergy(TTree* T)
{
return GetValueFromDB(T, "ebeam");
}
/*
// Return target mass in a given run
Double_t GetTgtM(TTree* T)
{
Double_t tmass = 0;
THaRun* run = 0;
if (!T->GetDirectory()) T->LoadTree(0); // Necessary if T is a TChain
TDirectory* fDir = T->GetDirectory();
if (fDir) fDir->GetObject("Run_Data",run);
if (run) {
tmass = run->GetParameters()->GetTgtM();
delete run;
}
return tmass;
}
*/