1 |
mocchiut |
1.1 |
// |
2 |
|
|
// Given a calibration and a data file this program create an ntuple with LEVEL2 calorimeter variables - Emiliano Mocchiutti |
3 |
|
|
// |
4 |
mocchiut |
1.3 |
// CaloCore.cxx version 3.05 (2006-05-30) |
5 |
mocchiut |
1.1 |
// |
6 |
|
|
// The only input needed is the path to the directory created by YODA for the data file you want to analyze. |
7 |
|
|
// |
8 |
|
|
// Changelog: |
9 |
mocchiut |
1.3 |
// |
10 |
mocchiut |
1.4 |
// 3.04 - 3.05 (2006-05-30): Qlast and nlast are now calculated using 4 (not 8) strips aournd the shower axis. Small bug fixed. |
11 |
mocchiut |
1.2 |
// |
12 |
|
|
// 3.03 - 3.04 (2006-05-23): Forgot to put impx and impy in the PAMELA reference system, fixed. |
13 |
mocchiut |
1.1 |
// |
14 |
|
|
// 3.02 - 3.03 (2006-05-18): updated to be called in DarthVader. Output dimension are now in cm and in the PAMELA reference system. |
15 |
|
|
// |
16 |
|
|
// 3.01 - 3.02 (2006-04-21): when copying entries get size of caclone and not of ca... (shouldn't matter). Fixed increasing file dimension bug when reprocessing. |
17 |
|
|
// Added variable planemax[2], plane of maximum energy release (x and y) in final output. Use ItoRunInfo instead of RunInfo. |
18 |
|
|
// |
19 |
|
|
// 3.00 - 3.01 (2006-04-14): fixed small bug in tagging the track used to determine track-related variables, put in caloprocessing the opening of parameters files, fixed |
20 |
|
|
// small bug in fortran routines |
21 |
|
|
// |
22 |
|
|
// 2.01 - 3.00 (2006-04-14): almost everything has changed. Now it can process one, all or some runs, introduced the final CaloLevel2 class+methods and the |
23 |
|
|
// working class "CaloProcessing", linked to the preliminary tracker flight software v0r00, reads YODA unique output files, |
24 |
|
|
// reduced the number of installed libraries, F77 programs splitted depending on the function contained, introduced the readout and |
25 |
|
|
// processing of self-trigger events, if the tracker provides more than one track all calorimeter track-related variables are saved |
26 |
|
|
// as many times as the number of tracks (via the TClonesArray object in the level2 rootple) and many other small changes. |
27 |
|
|
// |
28 |
|
|
// 2.00 - 2.01 (2006-01-26): bug: wrong calculation of baselines in some cases, fixed. |
29 |
|
|
// |
30 |
|
|
// 1.00 - 2.00 (2006-01-11): use TSQL ROOT classes instead of directly calling MySQL. |
31 |
|
|
// |
32 |
|
|
// 0.00 - 1.00 (2005-09-14): seems working. |
33 |
|
|
// |
34 |
|
|
// 0.00 (2005-09-09): clone of CaloLEVEL2.c . |
35 |
|
|
// |
36 |
|
|
// C/C++ headers |
37 |
|
|
// |
38 |
|
|
#include <fstream> |
39 |
|
|
#include <string.h> |
40 |
|
|
// |
41 |
|
|
// ROOT headers |
42 |
|
|
// |
43 |
|
|
#include <TTree.h> |
44 |
|
|
#include <TClassEdit.h> |
45 |
|
|
#include <TObject.h> |
46 |
|
|
#include <TList.h> |
47 |
mocchiut |
1.8 |
#include <TArrayI.h> |
48 |
mocchiut |
1.1 |
#include <TSystem.h> |
49 |
|
|
#include <TSystemDirectory.h> |
50 |
|
|
#include <TString.h> |
51 |
|
|
#include <TFile.h> |
52 |
|
|
#include <TClass.h> |
53 |
|
|
#include <TCanvas.h> |
54 |
|
|
#include <TH1.h> |
55 |
|
|
#include <TH1F.h> |
56 |
|
|
#include <TH2D.h> |
57 |
|
|
#include <TLatex.h> |
58 |
|
|
#include <TPad.h> |
59 |
|
|
#include <TSQLServer.h> |
60 |
|
|
#include <TSQLRow.h> |
61 |
|
|
#include <TSQLResult.h> |
62 |
|
|
#include <TClonesArray.h> |
63 |
|
|
#include <TStreamerInfo.h> |
64 |
|
|
// |
65 |
|
|
// YODA headers |
66 |
|
|
// |
67 |
|
|
#include <PamelaRun.h> |
68 |
|
|
#include <physics/trigger/TriggerEvent.h> |
69 |
|
|
// |
70 |
|
|
// This program headers |
71 |
|
|
// |
72 |
|
|
#include <RunInfo.h> |
73 |
|
|
#include <CaloCore.h> |
74 |
|
|
#include <CaloLevel2.h> |
75 |
|
|
#include <CaloProcessing.h> |
76 |
|
|
#include <CaloVerl2.h> |
77 |
|
|
// |
78 |
|
|
// Tracker classes headers and definitions |
79 |
|
|
// |
80 |
|
|
#include <TrkLevel2.h> |
81 |
|
|
// |
82 |
|
|
using namespace std; |
83 |
|
|
// |
84 |
|
|
// CORE ROUTINE |
85 |
|
|
// |
86 |
mocchiut |
1.8 |
int CaloCore(UInt_t run, TFile *file, TSQLServer *dbc, Int_t calargc, char *calargv[]){ |
87 |
mocchiut |
1.1 |
// |
88 |
|
|
// Set these to true to have a very verbose output. |
89 |
|
|
// |
90 |
|
|
Bool_t verbose = false; |
91 |
|
|
Bool_t debug = false; |
92 |
|
|
// |
93 |
|
|
// Output directory is the working directoy. |
94 |
|
|
// |
95 |
|
|
const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath())); |
96 |
|
|
// |
97 |
|
|
Int_t ri = 0; |
98 |
|
|
TString processFolder = "calorimeterFolder"; |
99 |
|
|
if ( calargc > 0 ){ |
100 |
|
|
ri = 0; |
101 |
|
|
while ( ri < calargc ){ |
102 |
|
|
if ( !strcmp(calargv[ri],"-processFolder") ) { |
103 |
|
|
if ( calargc < ri+1 ){ |
104 |
|
|
throw -3; |
105 |
|
|
}; |
106 |
|
|
processFolder = (TString)calargv[ri+1]; |
107 |
|
|
ri++; |
108 |
|
|
}; |
109 |
|
|
if ( !strcmp(calargv[ri],"-v") || !strcmp(calargv[ri],"--verbose") ) { |
110 |
|
|
verbose = true; |
111 |
|
|
}; |
112 |
|
|
if ( !strcmp(calargv[ri],"-g") || !strcmp(calargv[ri],"--debug") ) { |
113 |
|
|
debug = true; |
114 |
|
|
}; |
115 |
|
|
ri++; |
116 |
|
|
}; |
117 |
|
|
}; |
118 |
|
|
// |
119 |
|
|
// Working filename |
120 |
|
|
// |
121 |
|
|
TString outputfile; |
122 |
|
|
stringstream name; |
123 |
|
|
name.str(""); |
124 |
|
|
name << outdir << "/"; |
125 |
|
|
// |
126 |
|
|
// Variables. |
127 |
|
|
// |
128 |
|
|
// TFile *file = 0; |
129 |
|
|
TTree *tracker = 0; |
130 |
|
|
TTree *calo = 0; |
131 |
|
|
TTree *caloclone = 0; |
132 |
|
|
Bool_t reproc = false; |
133 |
|
|
Bool_t reprocall = false; |
134 |
mocchiut |
1.8 |
UInt_t nevents = 0; |
135 |
mocchiut |
1.1 |
UInt_t nobefrun = 0; |
136 |
|
|
UInt_t noaftrun = 0; |
137 |
|
|
UInt_t numbofrun = 0; |
138 |
mocchiut |
1.8 |
UInt_t totnorun = 0; |
139 |
mocchiut |
1.1 |
// |
140 |
|
|
Int_t code = 0; |
141 |
|
|
Int_t sgnl; |
142 |
|
|
// |
143 |
|
|
// calorimeter level2 classes |
144 |
|
|
// |
145 |
|
|
CaloLevel2 *ca = new CaloLevel2(); |
146 |
|
|
CaloLevel2 *caclone = new CaloLevel2(); |
147 |
|
|
// |
148 |
|
|
TrkLevel2 *trk = new TrkLevel2(); |
149 |
mocchiut |
1.8 |
Int_t nevtrkl2 = 0; |
150 |
mocchiut |
1.1 |
// |
151 |
|
|
UInt_t procev = 0; |
152 |
|
|
// |
153 |
mocchiut |
1.8 |
// define variables where to store the absolute run header and run trailer times (unsigned long long integers, when set to a number use to store the correct number). |
154 |
mocchiut |
1.1 |
// |
155 |
mocchiut |
1.8 |
UInt_t runheadtime = 0; |
156 |
|
|
UInt_t runtrailtime = 0; |
157 |
mocchiut |
1.1 |
UInt_t evfrom = 0; |
158 |
|
|
UInt_t evto = 0; |
159 |
mocchiut |
1.8 |
UInt_t totfileentries = 0; |
160 |
|
|
UInt_t idRun = 0; |
161 |
mocchiut |
1.1 |
Int_t id_reg_run=-1; |
162 |
|
|
stringstream ftmpname; |
163 |
|
|
TString fname; |
164 |
|
|
// |
165 |
|
|
// define variables for opening and reading level0 file |
166 |
|
|
// |
167 |
|
|
TFile *l0File = 0; |
168 |
|
|
TTree *l0tr = 0; |
169 |
|
|
TBranch *l0head = 0; |
170 |
|
|
TBranch *l0calo = 0; |
171 |
|
|
TBranch *l0trig = 0; |
172 |
mocchiut |
1.8 |
pamela::EventHeader *eh = 0; |
173 |
|
|
pamela::PscuHeader *ph = 0; |
174 |
mocchiut |
1.1 |
pamela::trigger::TriggerEvent *trig = 0; |
175 |
|
|
// |
176 |
|
|
// Define some basic variables |
177 |
|
|
// |
178 |
|
|
CaloProcessing *event = new CaloProcessing(); // NOTICE: very important to call here the constructor! |
179 |
|
|
stringstream file2; |
180 |
|
|
stringstream file3; |
181 |
|
|
stringstream qy; |
182 |
|
|
// Bool_t imtrack = false; |
183 |
|
|
Bool_t filled = false; |
184 |
|
|
// |
185 |
mocchiut |
1.8 |
UInt_t caloevents = 0; |
186 |
mocchiut |
1.1 |
stringstream calfile; |
187 |
|
|
stringstream aligfile; |
188 |
|
|
// |
189 |
|
|
Int_t i = -1; |
190 |
|
|
Int_t itr = -1; |
191 |
|
|
Int_t badevent = 0; |
192 |
|
|
Int_t totevent = 0; |
193 |
|
|
// |
194 |
mocchiut |
1.8 |
UInt_t atime = 0; |
195 |
mocchiut |
1.1 |
// |
196 |
|
|
Int_t S3 = 0; |
197 |
|
|
Int_t S2 = 0; |
198 |
|
|
Int_t S12 = 0; |
199 |
|
|
Int_t S11 = 0; |
200 |
|
|
UInt_t re = 0; |
201 |
|
|
// |
202 |
|
|
TString caloversion; |
203 |
|
|
ItoRunInfo *runinfo = 0; |
204 |
mocchiut |
1.8 |
TArrayI *runlist = 0; |
205 |
mocchiut |
1.1 |
// |
206 |
|
|
Float_t tmptrigty = -1.; |
207 |
|
|
Int_t ntrkentry = 0; |
208 |
|
|
GL_PARAM *q4 = new GL_PARAM(); |
209 |
mocchiut |
1.8 |
UInt_t tttrkpar1 = 0; |
210 |
mocchiut |
1.1 |
Bool_t trkpar1 = true; |
211 |
|
|
GL_ROOT *glroot = new GL_ROOT(); |
212 |
mocchiut |
1.8 |
GL_TIMESYNC *dbtime = 0; |
213 |
mocchiut |
1.1 |
// |
214 |
|
|
// |
215 |
|
|
// |
216 |
|
|
TFile *tempfile = 0; |
217 |
|
|
TTree *tempcalo = 0; |
218 |
|
|
stringstream tempname; |
219 |
|
|
stringstream calofolder; |
220 |
|
|
tempname.str(""); |
221 |
|
|
tempname << outdir; |
222 |
|
|
tempname << "/" << processFolder.Data(); |
223 |
|
|
calofolder.str(""); |
224 |
|
|
calofolder << tempname.str().c_str(); |
225 |
|
|
gSystem->MakeDirectory(calofolder.str().c_str()); |
226 |
|
|
tempname << "/calotree_run"; |
227 |
|
|
tempname << run << ".root"; |
228 |
|
|
// |
229 |
|
|
// As a first thing we must check what we have to do: if run = -1 we must process all events in the file has been passed |
230 |
|
|
// if run != -1 we must process only that run but first we have to check if the branch calorimeter already exist in the file |
231 |
|
|
// if it exists we are reprocessing data and we must delete that entries, if not we must create it. |
232 |
|
|
// |
233 |
mocchiut |
1.8 |
if ( run == 0 ) reproc = true; |
234 |
mocchiut |
1.1 |
// |
235 |
|
|
// |
236 |
|
|
// |
237 |
|
|
if ( !file->IsOpen() ){ |
238 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: cannot open file for writing\n"); |
239 |
|
|
throw -101; |
240 |
|
|
}; |
241 |
|
|
// |
242 |
|
|
// Does it contain the Tracker tree? |
243 |
|
|
// |
244 |
|
|
tracker = (TTree*)file->Get("Tracker"); |
245 |
|
|
if ( !tracker ) { |
246 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: no tracker tree\n"); |
247 |
|
|
code = -102; |
248 |
|
|
goto closeandexit; |
249 |
|
|
}; |
250 |
|
|
tracker->SetBranchAddress("TrkLevel2",&trk); |
251 |
|
|
nevtrkl2 = tracker->GetEntries(); |
252 |
|
|
// |
253 |
|
|
// Call runinfo |
254 |
|
|
// |
255 |
|
|
sgnl = 0; |
256 |
|
|
runinfo = new ItoRunInfo(file); |
257 |
|
|
// |
258 |
|
|
// update versioning information and retrieve informations about the run to be processed |
259 |
|
|
// |
260 |
|
|
caloversion = CaloInfo(false); |
261 |
|
|
sgnl = runinfo->Update(run,"CALO",caloversion); |
262 |
|
|
// |
263 |
|
|
if ( sgnl ){ |
264 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n"); |
265 |
|
|
code = sgnl; |
266 |
|
|
goto closeandexit; |
267 |
|
|
} else { |
268 |
|
|
sgnl = 0; |
269 |
|
|
}; |
270 |
|
|
// |
271 |
|
|
// number of events in the file BEFORE the first event of our run |
272 |
|
|
// |
273 |
|
|
nobefrun = runinfo->GetFirstEntry(); |
274 |
|
|
// |
275 |
|
|
// total number of events in the file |
276 |
|
|
// |
277 |
|
|
totfileentries = runinfo->GetFileEntries(); |
278 |
|
|
// |
279 |
|
|
// first file entry AFTER the last event of our run |
280 |
|
|
// |
281 |
|
|
noaftrun = runinfo->GetLastEntry() + 1; |
282 |
|
|
// |
283 |
|
|
// number of run to be processed |
284 |
|
|
// |
285 |
|
|
numbofrun = runinfo->GetNoRun(); |
286 |
|
|
// |
287 |
|
|
// number of runs in the file |
288 |
|
|
// |
289 |
|
|
totnorun = runinfo->GetRunEntries(); |
290 |
|
|
// |
291 |
|
|
// Does it contain already a Calorimeter branch? if so we are reprocessing data, if not we must create it. |
292 |
|
|
// |
293 |
|
|
caloclone = (TTree*)file->Get("Calorimeter"); |
294 |
|
|
// |
295 |
|
|
if ( !caloclone ){ |
296 |
|
|
reproc = false; |
297 |
mocchiut |
1.8 |
if ( run == 0 && verbose ) printf(" CALORIMETER - WARNING: you are reprocessing data but calorimeter tree does not exist!\n"); |
298 |
|
|
if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" CALORIMETER - WARNING: it seems you are not reprocessing data but calorimeter\n versioning information already exists in RunInfo.\n"); |
299 |
mocchiut |
1.1 |
// |
300 |
|
|
} else { |
301 |
|
|
// |
302 |
|
|
reproc = true; |
303 |
|
|
// |
304 |
|
|
if ( verbose ) printf("\n Preparing the pre-processing...\n"); |
305 |
|
|
// |
306 |
mocchiut |
1.8 |
if ( run == 0 ){ |
307 |
mocchiut |
1.1 |
// |
308 |
|
|
// if we are reprocessing everything we don't need to copy any old event and we can just create a new branch in the clone tree and jump steps 4/5/7. |
309 |
|
|
// |
310 |
|
|
if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing all runs in the file\n"); |
311 |
|
|
reprocall = true; |
312 |
|
|
// |
313 |
|
|
} else { |
314 |
|
|
// |
315 |
|
|
// we are reprocessing a single run |
316 |
|
|
// |
317 |
mocchiut |
1.8 |
if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing run number %u \n",run); |
318 |
mocchiut |
1.1 |
reprocall = false; |
319 |
|
|
// |
320 |
|
|
// |
321 |
|
|
// |
322 |
|
|
tempfile = new TFile(tempname.str().c_str(),"RECREATE"); |
323 |
|
|
tempcalo = caloclone->CloneTree(-1,"fast"); |
324 |
|
|
tempcalo->SetName("Calorimeter-old"); |
325 |
|
|
tempfile->Write(); |
326 |
|
|
tempfile->Close(); |
327 |
|
|
}; |
328 |
|
|
// |
329 |
|
|
// delete old tree |
330 |
|
|
// |
331 |
|
|
caloclone->Delete("all"); |
332 |
|
|
// |
333 |
|
|
if ( verbose ) printf("\n ...done!\n"); |
334 |
|
|
// |
335 |
|
|
}; |
336 |
|
|
// |
337 |
|
|
// create calorimeter tree calo |
338 |
|
|
// |
339 |
|
|
file->cd(); |
340 |
|
|
calo = new TTree("Calorimeter-new","PAMELA Level2 calorimeter data"); |
341 |
|
|
calo->Branch("CaloLevel2","CaloLevel2",&ca); |
342 |
|
|
// |
343 |
|
|
if ( reproc && !reprocall ){ |
344 |
|
|
// |
345 |
|
|
// |
346 |
|
|
// |
347 |
|
|
tempfile = new TFile(tempname.str().c_str(),"READ"); |
348 |
|
|
caloclone = (TTree*)tempfile->Get("Calorimeter-old"); |
349 |
|
|
caloclone->SetBranchAddress("CaloLevel2",&caclone); |
350 |
|
|
// |
351 |
|
|
if ( nobefrun > 0 ){ |
352 |
|
|
if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n"); |
353 |
mocchiut |
1.8 |
if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run); |
354 |
mocchiut |
1.1 |
if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun); |
355 |
|
|
for (UInt_t j = 0; j < nobefrun; j++){ |
356 |
|
|
// |
357 |
|
|
caloclone->GetEntry(j); |
358 |
|
|
// |
359 |
|
|
// copy caclone to ca |
360 |
|
|
// |
361 |
|
|
memcpy(&ca,&caclone,sizeof(caclone)); |
362 |
|
|
// |
363 |
|
|
// Fill entry in the new tree |
364 |
|
|
// |
365 |
|
|
calo->Fill(); |
366 |
|
|
// |
367 |
mocchiut |
1.6 |
ca->Clear(); |
368 |
|
|
// |
369 |
mocchiut |
1.1 |
}; |
370 |
|
|
if ( verbose ) printf(" Finished successful copying!\n"); |
371 |
|
|
}; |
372 |
|
|
}; |
373 |
|
|
// |
374 |
|
|
// Get the list of run to be processed |
375 |
|
|
// |
376 |
|
|
runlist = runinfo->GetRunList(); |
377 |
|
|
// |
378 |
|
|
// Loop over the run to be processed |
379 |
|
|
// |
380 |
|
|
for (UInt_t irun=0; irun < numbofrun; irun++){ |
381 |
|
|
// |
382 |
|
|
badevent = 0; |
383 |
|
|
// |
384 |
|
|
idRun = runlist->At(irun); |
385 |
|
|
if ( verbose ) printf("\n\n\n ####################################################################### \n"); |
386 |
mocchiut |
1.8 |
if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun); |
387 |
mocchiut |
1.1 |
if ( verbose ) printf(" ####################################################################### \n\n\n"); |
388 |
|
|
// |
389 |
|
|
sgnl = runinfo->GetRunInfo(idRun); |
390 |
|
|
if ( sgnl ){ |
391 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n"); |
392 |
|
|
code = sgnl; |
393 |
|
|
goto closeandexit; |
394 |
|
|
} else { |
395 |
|
|
sgnl = 0; |
396 |
|
|
}; |
397 |
mocchiut |
1.8 |
id_reg_run = runinfo->ID_ROOT_L0; |
398 |
mocchiut |
1.1 |
runheadtime = runinfo->RUNHEADER_TIME; |
399 |
|
|
runtrailtime = runinfo->RUNTRAILER_TIME; |
400 |
mocchiut |
1.8 |
evfrom = runinfo->EV_FROM; |
401 |
|
|
evto = runinfo->EV_TO; |
402 |
mocchiut |
1.1 |
// |
403 |
|
|
if ( id_reg_run == -1 ){ |
404 |
mocchiut |
1.8 |
if ( verbose ) printf("\n CALORIMETER - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun); |
405 |
mocchiut |
1.1 |
code = -5; |
406 |
|
|
goto closeandexit; |
407 |
|
|
}; |
408 |
|
|
// |
409 |
mocchiut |
1.8 |
// prepare the timesync for the db |
410 |
|
|
// |
411 |
|
|
dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc); |
412 |
|
|
// |
413 |
mocchiut |
1.1 |
// Search in the DB the path and name of the LEVEL0 file to be processed. |
414 |
|
|
// |
415 |
mocchiut |
1.8 |
glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc); |
416 |
mocchiut |
1.1 |
// |
417 |
|
|
ftmpname.str(""); |
418 |
|
|
ftmpname << glroot->PATH.Data() << "/"; |
419 |
|
|
ftmpname << glroot->NAME.Data(); |
420 |
|
|
fname = ftmpname.str().c_str(); |
421 |
|
|
// |
422 |
|
|
// print out informations |
423 |
|
|
// |
424 |
mocchiut |
1.8 |
totevent = runinfo->NEVENTS; |
425 |
mocchiut |
1.1 |
if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data()); |
426 |
mocchiut |
1.8 |
if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runheadtime); |
427 |
|
|
if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runtrailtime); |
428 |
|
|
if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evfrom+totevent); |
429 |
mocchiut |
1.1 |
// |
430 |
|
|
// Open Level0 file |
431 |
|
|
// |
432 |
|
|
l0File = new TFile(fname.Data()); |
433 |
|
|
if ( !l0File ) { |
434 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: problems opening Level0 file\n"); |
435 |
|
|
code = -6; |
436 |
|
|
goto closeandexit; |
437 |
|
|
}; |
438 |
|
|
l0tr = (TTree*)l0File->Get("Physics"); |
439 |
|
|
if ( !l0tr ) { |
440 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: no Physics tree in Level0 file\n"); |
441 |
|
|
l0File->Close(); |
442 |
|
|
code = -7; |
443 |
|
|
goto closeandexit; |
444 |
|
|
}; |
445 |
|
|
l0head = l0tr->GetBranch("Header"); |
446 |
|
|
if ( !l0head ) { |
447 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: no Header branch in Level0 tree\n"); |
448 |
|
|
l0File->Close(); |
449 |
|
|
code = -8; |
450 |
|
|
goto closeandexit; |
451 |
|
|
}; |
452 |
|
|
l0calo = l0tr->GetBranch("Calorimeter"); |
453 |
|
|
if ( !l0calo ) { |
454 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: no Calorimeter branch in Level0 tree\n"); |
455 |
|
|
l0File->Close(); |
456 |
|
|
code = -103; |
457 |
|
|
goto closeandexit; |
458 |
|
|
}; |
459 |
|
|
l0trig = l0tr->GetBranch("Trigger"); |
460 |
|
|
if ( !l0trig ) { |
461 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: no Trigger branch in Level0 tree\n"); |
462 |
|
|
l0File->Close(); |
463 |
|
|
code = -104; |
464 |
|
|
goto closeandexit; |
465 |
|
|
}; |
466 |
|
|
// |
467 |
|
|
l0tr->SetBranchAddress("Trigger", &trig); |
468 |
mocchiut |
1.8 |
l0tr->SetBranchAddress("Header", &eh); |
469 |
mocchiut |
1.1 |
// |
470 |
|
|
// Construct the event object, look for the calibration which include the first header |
471 |
|
|
// |
472 |
|
|
sgnl = 0; |
473 |
|
|
if ( verbose ) printf(" Check for calorimeter calibrations and initialize event object \n"); |
474 |
|
|
event->ProcessingInit(dbc,runheadtime,sgnl,l0tr,debug,verbose); |
475 |
|
|
if ( verbose ) printf("\n"); |
476 |
|
|
if ( sgnl == 100 ) { |
477 |
|
|
code = sgnl; |
478 |
|
|
if ( verbose ) printf(" CALORIMETER - WARNING: run header not included in any calibration interval\n"); |
479 |
|
|
sgnl = 0; |
480 |
|
|
}; |
481 |
|
|
if ( sgnl ){ |
482 |
|
|
l0File->Close(); |
483 |
|
|
code = sgnl; |
484 |
|
|
goto closeandexit; |
485 |
|
|
}; |
486 |
|
|
// |
487 |
|
|
qy.str(""); |
488 |
|
|
// |
489 |
mocchiut |
1.8 |
nevents = l0head->GetEntries(); |
490 |
mocchiut |
1.1 |
caloevents = l0calo->GetEntries(); |
491 |
|
|
// |
492 |
|
|
if ( nevents < 1 ) { |
493 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: Level0 file is empty\n\n"); |
494 |
|
|
l0File->Close(); |
495 |
|
|
code = -11; |
496 |
|
|
goto closeandexit; |
497 |
|
|
}; |
498 |
|
|
// |
499 |
|
|
if ( evto > nevents-1 ) { |
500 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: too few entries in the registry tree\n"); |
501 |
|
|
l0File->Close(); |
502 |
|
|
code = -12; |
503 |
|
|
goto closeandexit; |
504 |
|
|
}; |
505 |
|
|
// |
506 |
|
|
// Check if we have to load parameter files |
507 |
|
|
// |
508 |
|
|
sgnl = 0; |
509 |
|
|
sgnl = event->ChkParam(dbc,runheadtime); // calorimeter parameter files |
510 |
|
|
if ( sgnl < 0 ){ |
511 |
|
|
code = sgnl; |
512 |
|
|
l0File->Close(); |
513 |
|
|
goto closeandexit; |
514 |
|
|
}; |
515 |
|
|
// |
516 |
|
|
if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){ |
517 |
|
|
trkpar1 = false; |
518 |
mocchiut |
1.8 |
Int_t glpar = q4->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc); |
519 |
mocchiut |
1.4 |
if ( glpar < 0 ){ |
520 |
|
|
code = glpar; |
521 |
|
|
goto closeandexit; |
522 |
|
|
}; |
523 |
mocchiut |
1.1 |
tttrkpar1 = q4->TO_TIME; |
524 |
|
|
// ---------------------------- |
525 |
|
|
// Read the magnetic field |
526 |
|
|
// ---------------------------- |
527 |
|
|
if ( verbose ) printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data()); |
528 |
|
|
trk->LoadField(q4->PATH+q4->NAME); |
529 |
|
|
if ( verbose ) printf("\n"); |
530 |
|
|
}; |
531 |
|
|
// |
532 |
|
|
// run over all the events of the run |
533 |
|
|
// |
534 |
|
|
if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n"); |
535 |
|
|
// |
536 |
mocchiut |
1.8 |
// |
537 |
|
|
for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){ |
538 |
mocchiut |
1.1 |
// |
539 |
|
|
if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000); |
540 |
|
|
// |
541 |
mocchiut |
1.8 |
l0head->GetEntry(re); |
542 |
mocchiut |
1.1 |
// |
543 |
|
|
// absolute time of this event |
544 |
|
|
// |
545 |
mocchiut |
1.8 |
ph = eh->GetPscuHeader(); |
546 |
|
|
atime = dbtime->DBabsTime(ph->GetOrbitalTime()); |
547 |
mocchiut |
1.1 |
// |
548 |
|
|
// |
549 |
|
|
// |
550 |
mocchiut |
1.8 |
if ( re > caloevents-1 ){ |
551 |
mocchiut |
1.1 |
if ( verbose ) printf(" CALORIMETER - ERROR: no physics events with entry = %i in Level0 file\n",i); |
552 |
|
|
l0File->Close(); |
553 |
|
|
code = -112; |
554 |
|
|
goto closeandexit; |
555 |
|
|
}; |
556 |
|
|
// |
557 |
|
|
if ( atime > runtrailtime || atime < runheadtime ) { |
558 |
|
|
if ( verbose ) printf(" CALORIMETER - WARNING: event at time outside the run time window, skipping it\n"); |
559 |
|
|
goto jumpev; |
560 |
|
|
}; |
561 |
|
|
// |
562 |
|
|
// retrieve tracker informations |
563 |
|
|
// |
564 |
|
|
if ( !reprocall ){ |
565 |
|
|
itr = nobefrun + (re - evfrom); |
566 |
|
|
} else { |
567 |
|
|
itr = runinfo->GetFirstEntry() + (re - evfrom); |
568 |
|
|
}; |
569 |
|
|
if ( itr > nevtrkl2 ){ |
570 |
|
|
if ( verbose ) printf(" CALORIMETER - ERROR: no tracker events with entry = %i in Level2 file\n",itr); |
571 |
|
|
l0File->Close(); |
572 |
|
|
code = -113; |
573 |
|
|
goto closeandexit; |
574 |
|
|
}; |
575 |
|
|
tracker->GetEntry(itr); |
576 |
|
|
// |
577 |
|
|
procev++; |
578 |
|
|
// |
579 |
|
|
// start processing |
580 |
|
|
// |
581 |
mocchiut |
1.6 |
ca->Clear(); |
582 |
mocchiut |
1.1 |
// |
583 |
|
|
// determine who generate the trigger for this event (TOF, S4/PULSER, CALO) |
584 |
|
|
// |
585 |
mocchiut |
1.8 |
l0trig->GetEntry(re); |
586 |
mocchiut |
1.1 |
S3 = 0; |
587 |
|
|
S2 = 0; |
588 |
|
|
S12 = 0; |
589 |
|
|
S11 = 0; |
590 |
|
|
S3 = trig->patterntrig[2]; |
591 |
|
|
S2 = trig->patterntrig[3]; |
592 |
|
|
S12 = trig->patterntrig[4]; |
593 |
|
|
S11 = trig->patterntrig[5]; |
594 |
|
|
if ( trig->patterntrig[0] ) tmptrigty = 2.; |
595 |
|
|
if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.; |
596 |
|
|
if ( trig->patterntrig[1] & (1<<0) || (!trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11) ) tmptrigty = 1.; |
597 |
|
|
event->clevel2->trigty = tmptrigty; |
598 |
|
|
// |
599 |
|
|
// check if the calibration we are using is still good, if not load another calibration |
600 |
|
|
// |
601 |
|
|
sgnl = 0; |
602 |
|
|
sgnl = event->ChkCalib(dbc,atime); |
603 |
|
|
if ( sgnl < 0 ){ |
604 |
|
|
code = sgnl; |
605 |
|
|
goto closeandexit; |
606 |
|
|
}; |
607 |
|
|
if ( sgnl == 100 ){ |
608 |
|
|
code = sgnl; |
609 |
|
|
if ( verbose ) printf(" CALORIMETER - WARNING: data not associated to any calibration interval\n"); |
610 |
|
|
badevent++; |
611 |
|
|
sgnl = 0; |
612 |
|
|
}; |
613 |
|
|
// |
614 |
|
|
// do we have at least one track from the tracker? |
615 |
|
|
// |
616 |
|
|
event->clevel1->good2 = 1; |
617 |
mocchiut |
1.8 |
// |
618 |
mocchiut |
1.5 |
if ( trk->ntrk() > 0 ) event->clevel1->good2 = 1; |
619 |
mocchiut |
1.1 |
// |
620 |
mocchiut |
1.8 |
// Calibrate calorimeter event "re" and store output in the two structures that will be passed to fortran routine |
621 |
mocchiut |
1.1 |
// |
622 |
mocchiut |
1.8 |
event->Calibrate(re); |
623 |
mocchiut |
1.1 |
// |
624 |
|
|
// Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract topological variables. |
625 |
|
|
// |
626 |
|
|
// |
627 |
|
|
// Calculate variables common to all tracks (qtot, nstrip, etc.) |
628 |
|
|
// |
629 |
|
|
event->GetCommonVar(); |
630 |
|
|
// |
631 |
|
|
// Fill common variables |
632 |
|
|
// |
633 |
|
|
event->FillCommonVar(ca); |
634 |
|
|
// |
635 |
|
|
// Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker) |
636 |
|
|
// |
637 |
|
|
ntrkentry = 0; |
638 |
|
|
// |
639 |
|
|
filled = false; |
640 |
|
|
// |
641 |
|
|
if ( event->clevel1->good2 == 1 || event->clevel2->trigty > 1. ){ |
642 |
|
|
// |
643 |
|
|
if ( event->clevel1->good2 == 1 ){ |
644 |
|
|
// |
645 |
|
|
// We have at least one track |
646 |
|
|
// |
647 |
|
|
// |
648 |
|
|
// Run over tracks |
649 |
|
|
// |
650 |
|
|
for(Int_t nt=0; nt < trk->ntrk(); nt++){ |
651 |
|
|
// |
652 |
|
|
event->clevel1->good2 = 1; |
653 |
|
|
// |
654 |
|
|
TrkTrack *ptt = trk->GetStoredTrack(nt); |
655 |
|
|
// |
656 |
|
|
event->clevel1->trkchi2 = 0; |
657 |
|
|
// |
658 |
|
|
// Copy the alpha vector in the input structure |
659 |
|
|
// |
660 |
|
|
for (Int_t e = 0; e < 5 ; e++){ |
661 |
|
|
event->clevel1->al_p[e][0] = ptt->al[e]; |
662 |
|
|
}; |
663 |
|
|
// |
664 |
|
|
// Get tracker related variables for this track |
665 |
|
|
// |
666 |
|
|
event->GetTrkVar(); |
667 |
|
|
// |
668 |
|
|
// Save tracker track sequence number |
669 |
|
|
// |
670 |
|
|
|
671 |
|
|
event->trkseqno = nt; |
672 |
|
|
// |
673 |
|
|
// Copy values in the class ca from the structure clevel2 |
674 |
|
|
// |
675 |
|
|
event->FillTrkVar(ca,ntrkentry); |
676 |
|
|
ntrkentry++; |
677 |
|
|
filled = true; |
678 |
|
|
// |
679 |
|
|
}; // loop on all the tracks |
680 |
|
|
}; |
681 |
|
|
if ( event->clevel2->trigty >= 2. ){ |
682 |
|
|
if ( verbose ) printf(" Selftrigger event at entry %i \n",itr); |
683 |
|
|
// |
684 |
|
|
// Disable "track mode" in the fortran routine |
685 |
|
|
// |
686 |
|
|
event->clevel1->good2 = 0; |
687 |
|
|
// |
688 |
|
|
// We have a selftrigger event to analyze. |
689 |
|
|
// |
690 |
|
|
for (Int_t e = 0; e < 5 ; e++){ |
691 |
|
|
event->clevel1->al_p[e][0] = 0.; |
692 |
|
|
event->clevel1->al_p[e][1] = 0.; |
693 |
|
|
}; |
694 |
|
|
event->clevel1->trkchi2 = 0; |
695 |
|
|
// |
696 |
|
|
event->GetTrkVar(); |
697 |
|
|
// |
698 |
|
|
// if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on |
699 |
|
|
// |
700 |
|
|
if ( event->clevel1->good2 == 0 ) { |
701 |
|
|
// |
702 |
|
|
// In selftrigger mode the trkentry variable is set to -1 |
703 |
|
|
// |
704 |
|
|
event->trkseqno = -1; |
705 |
|
|
// |
706 |
|
|
// Copy values in the class ca from the structure clevel2 |
707 |
|
|
// |
708 |
|
|
event->FillTrkVar(ca,ntrkentry); |
709 |
|
|
ntrkentry++; |
710 |
|
|
filled = true; |
711 |
|
|
// |
712 |
|
|
} else { |
713 |
|
|
if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr); |
714 |
|
|
}; |
715 |
|
|
}; |
716 |
|
|
}; |
717 |
|
|
// |
718 |
|
|
if ( !filled ) badevent++; |
719 |
|
|
// |
720 |
|
|
// Clear structures used to communicate with fortran |
721 |
|
|
// |
722 |
|
|
event->ClearStructs(); |
723 |
|
|
// |
724 |
|
|
// Fill the rootple |
725 |
|
|
// |
726 |
|
|
calo->Fill(); |
727 |
|
|
// |
728 |
mocchiut |
1.6 |
// delete ca; |
729 |
mocchiut |
1.1 |
// |
730 |
|
|
jumpev: |
731 |
|
|
debug = false; |
732 |
|
|
// |
733 |
|
|
}; |
734 |
|
|
// |
735 |
|
|
if ( verbose ) printf("\n SUMMARY:\n"); |
736 |
|
|
if ( verbose ) printf(" Total number of events: %i \n",totevent); |
737 |
|
|
if ( verbose ) printf(" Events with at least one track: %i \n",totevent-badevent); |
738 |
|
|
if ( verbose ) printf(" Events without tracks: %i \n",badevent); |
739 |
|
|
// |
740 |
|
|
if ( badevent == totevent ){ |
741 |
mocchiut |
1.8 |
if ( verbose ) printf("\n CALORIMETER - WARNING no tracks or good events in run %u \n",idRun); |
742 |
mocchiut |
1.1 |
code = 101; |
743 |
|
|
}; |
744 |
|
|
// |
745 |
mocchiut |
1.8 |
delete dbtime; |
746 |
mocchiut |
1.1 |
// |
747 |
|
|
// Clear variables before processing another run (needed to have always the same result when reprocessing data with the same software). |
748 |
|
|
// |
749 |
|
|
event->RunClose(); |
750 |
|
|
if ( l0File ) l0File->Close(); |
751 |
|
|
// |
752 |
|
|
}; // process all the runs |
753 |
|
|
// |
754 |
|
|
if ( verbose ) printf("\n Finished processing data \n"); |
755 |
|
|
// |
756 |
|
|
closeandexit: |
757 |
|
|
// |
758 |
|
|
if ( !reprocall && reproc && code >= 0 ){ |
759 |
|
|
if ( totfileentries > noaftrun ){ |
760 |
|
|
if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n"); |
761 |
mocchiut |
1.8 |
if ( verbose ) printf(" Copying %u events in the file which are after the end of the run %u \n",(totfileentries-noaftrun),run); |
762 |
|
|
if ( verbose ) printf(" Start copying at event number %u end copying at event number %u \n",noaftrun,totfileentries); |
763 |
mocchiut |
1.1 |
for (UInt_t j = noaftrun; j < totfileentries; j++ ){ |
764 |
|
|
// |
765 |
|
|
// Get entry from old tree |
766 |
|
|
// |
767 |
|
|
caloclone->GetEntry(j); |
768 |
|
|
// |
769 |
|
|
// copy caclone to ca |
770 |
|
|
// |
771 |
mocchiut |
1.6 |
ca->Clear(); |
772 |
mocchiut |
1.8 |
// |
773 |
mocchiut |
1.1 |
memcpy(&ca,&caclone,sizeof(caclone)); |
774 |
|
|
// |
775 |
|
|
// Fill entry in the new tree |
776 |
|
|
// |
777 |
|
|
calo->Fill(); |
778 |
|
|
// |
779 |
|
|
}; |
780 |
|
|
if ( verbose ) printf(" Finished successful copying!\n"); |
781 |
|
|
}; |
782 |
|
|
}; |
783 |
|
|
// |
784 |
|
|
// Case of no errors: close files, delete old tree(s), write and close level2 file |
785 |
|
|
// |
786 |
|
|
if ( l0File ) l0File->Close(); |
787 |
|
|
if ( tempfile ) tempfile->Close(); |
788 |
|
|
gSystem->Unlink(tempname.str().c_str()); |
789 |
|
|
if ( tracker ) tracker->Delete(); |
790 |
|
|
// |
791 |
|
|
if ( code < 0 ) printf("\n CALORIMETER - ERROR: an error occurred, try to save anyway...\n"); |
792 |
|
|
if ( verbose ) printf("\n Writing and closing rootple\n"); |
793 |
|
|
if ( runinfo ) runinfo->Close(); |
794 |
|
|
if ( calo ) calo->SetName("Calorimeter"); |
795 |
|
|
if ( file ){ |
796 |
|
|
file->cd(); |
797 |
|
|
file->Write(); |
798 |
|
|
}; |
799 |
|
|
if ( calo ) calo->Delete(); |
800 |
|
|
// |
801 |
|
|
gSystem->Unlink(calofolder.str().c_str()); |
802 |
|
|
// |
803 |
mocchiut |
1.7 |
if ( ca ) delete ca; |
804 |
|
|
if ( caclone ) delete caclone; |
805 |
|
|
if ( trk ) delete trk; |
806 |
|
|
if ( q4 ) delete q4; |
807 |
|
|
if ( glroot ) delete glroot; |
808 |
|
|
if ( runinfo ) delete runinfo; |
809 |
|
|
// |
810 |
mocchiut |
1.1 |
// the end |
811 |
|
|
// |
812 |
|
|
if ( verbose ) printf("\n Exiting...\n"); |
813 |
|
|
if ( code < 0 ) throw code; |
814 |
|
|
return(code); |
815 |
|
|
} |
816 |
|
|
|
817 |
|
|
|
818 |
|
|
|