/[PAMELA software]/calo/ground/LEVEL2/macros/CaloLEVEL2.cra
ViewVC logotype

Annotation of /calo/ground/LEVEL2/macros/CaloLEVEL2.cra

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Thu Mar 9 15:55:29 2006 UTC (18 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.2: +1264 -1251 lines
Last ground software main release, the code is freezed only major bugs will be fixed

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 // CaloLEVEL2.c version 4.03 (2006-03-09)
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     //
10 mocchiut 1.3 // 4.02 - 4.03 (2006-03-09): Adapted to work with tracker software release r3v02.
11     //
12     // 4.01 - 4.02 (2006-01-26): Bug: wrong baselines calculation in some cases, fixed.
13     //
14 mocchiut 1.2 // 4.00 - 4.01 (2006-01-11): Bugs: in Makefile (nothing to worry about) and here clevel1.trkchi2 never filled! fixed. Not really processing self trigger events! fixed.
15     // Bug: not setting small values to zero after applying the cross-talk correction! fixed.
16     //
17 mocchiut 1.1 // 3.17 - 4.00 (2005-11-29): preparing for the final release, small changes in the makefile.
18     //
19     // 3.16 - 3.17 (2005-10-13): do not exclude all strips marked "bad" but only the ones with RMS>6.
20     //
21     // 3.15 - 3.16 (2005-10-04): now it reads Tracker Data version 2.00. Process self-trigger events assuming R=1000 GV and using the calorimeter fit of the track;
22     // self-trigger events are still marked as "bad" and there is no energy info, nor any angular correction on the track.
23     //
24     // 3.14 - 3.15 (2005-09-20): QX22 and NX22 are not related to the last plane but the 11th! fixed!
25     //
26     // 3.13 - 3.14 (2005-09-09): Still some bugs in 64 bit arch, fixed.
27     //
28     // 3.12 - 3.13 (2005-09-08): Bug: variable cbar is filled incorrectly. Fixed.
29     //
30     // 3.11 - 3.12 (2005-08-31): Added two variables in the ntuple (thex, they sin of x/y angles as measured by the calorimeter)
31     //
32     // 3.10 - 3.11 (2005-08-30): Don't run in MySQL standalone mode even if in FORCE mode.
33     //
34     // 3.09 - 3.10 (2005-08-05): 64 bit arch bugs fixed.
35     //
36     // 3.08 - 3.09 (2005-07-27): x and y view were inverted when fitting the track using calorimeter information, fixed. (affect variables cbar, cibar, npcfit and varcfit).
37     //
38     // 3.07 - 3.08 (2005-07-25): planetot and qmean always empty in rootple, fixed. Added variables on goodness of the calorimeter fit (npcfit, varcfit).
39     //
40     // 3.06 - 3.07 (2005-07-21): If not in FORCE mode abort processing if no calibration file is found.
41     //
42     // 3.05 - 3.06 (2005-07-19): Infinite loop in case of OBT jumps, fixed. NaN in the ntuple and rootple in some cases, fixed (lowered optimization level in
43     // the compilation of the ntuple and check for NaN from the tracking routine). Added variables in the ntuple/rootple: planetot, qmean.
44     //
45     // 3.04 - 3.05 (2005-07-18): No more class CalorimeterLevel2 since it makes very slow the filling of the rootple (unknown reason). Now the tree is filled
46     // with variables from structure CaLevel2.
47     //
48     // 3.03 - 3.04 (2005-07-15): More tuning, now it can compile and it works! Not loaded anymore yodaUtility.c, use clone routines instead.
49     //
50     // 3.02 - 3.03 (2005-07-13): Start the tuning for compilation and speed optimization.
51     //
52     // 3.01 - 3.02 (2005-07-11): Correction for high energy deposit. Correction in the calorimeter planes position.
53     //
54     // 3.00 - 3.01 (2005-07-11): Infinite loop in some cases of lost of sync, fixed. Cannot write ntuples bigger than 3 MB, fixed.
55     //
56     // 2.10 - 3.00 (2005-07-05): Include track information.
57     //
58     // 2.09 - 2.10 (2005-07-05): Started the tuning for the compilation of the macro. Added include files of ROOT subroutines.
59     //
60     // 2.08 - 2.09 (2005-06-29): Simplification in the check of goodness for the events. Integer convention for software code.
61     //
62     // 2.07 - 2.08 (2005-06-24): Changed the structure of the release and added the enviromental variables.
63     //
64     // 2.06 - 2.07 (2005-06-23): Added boolean variables to ntuple: good, crc, perr, swerr.
65     //
66     // 2.05 - 2.06 (2005-06-22): Trigger flag set as default to -1. It will remain -1 if the program is not able to determine who gave the trigger.
67     // Added: new tree (Software)/ntuple (number 2) with one entry only which contains the information about the software used to
68     // produce the LEVEL2 data. Changed names in commons/struct/ntuples/rootples: evfile -> pro_num, headc -> pkt_num .
69     // Disabled MySQL input variable (still there for testing purpouse).
70     //
71     // 2.04 - 2.05 (2005-06-17): Trigger flag is not always correct for S4 triggers, fixed with a workaround.
72     //
73     // 2.03 - 2.04 (2005-06-16): Bug, when casting from integer to float the resulting float is WRONG! changed in the rootple OBT, HEADC, EVFILE from real to integer.
74     // Changes also in the fortran libraries and passing structures.
75     //
76     // 2.02 - 2.03 (2005-06-15): Bug, in PAW and FORCE mode the existence of file is not checked, finally fixed.
77     //
78     // 2.01 - 2.02 (2005-06-07): Bug, the calibration used could be incorrect for some events. Fixed: check that the event time is in the calibration window,
79     // if not search for another calibration.
80     //
81     // 2.00 - 2.01 (2005-06-07): Check if file is older than 050515_007.
82     //
83     // 1.04 - 2.00 (2005-06-06): Added support to MySQL Trieste database.
84     //
85     // 1.03 - 1.04 (2005-05-31): Now it can unload all the libraries and you can run this script as many times you want without quitting ROOT.
86     //
87     // 1.02 - 1.03 (2005-05-27): cannot find libraries and calibration file when running twice in the same ROOT section, fixed. KNOWN BUG: cannot unload cfillcalol2_C!!
88     //
89     // 1.01 - 1.02 (2005-05-18): filename corruption in some cases. Workaround for the moment...
90     //
91     // 1.00 - 1.01 (2005-05-17): problems when starting with a possible backup calibration file. Fixed.
92     //
93     // 0.01 - 1.00 (2005-05-06): first time it works. evfile must be i+1 not i.
94     //
95     // 0.01 (2005-05-04): created.
96     //
97     #include <fstream>
98     #include <TTree.h>
99     #include <TClassEdit.h>
100     #include <TObject.h>
101     #include <TList.h>
102     #include <TSystem.h>
103     #include <TSystemDirectory.h>
104     #include <TString.h>
105     #include <TFile.h>
106     #include <TClass.h>
107     #include <TCanvas.h>
108     #include <TH1.h>
109     #include <TH1F.h>
110     #include <TH2D.h>
111     #include <TLatex.h>
112     #include <TPad.h>
113     //
114     #include <calomysqlstruct.h>
115     #include <ccal2struct.h>
116     #include <ctrkstruct.h>
117     #include <ctrkinclude.h>
118     //
119     #if !defined (__CINT__)
120     extern short int creadmysql(char *, const char *, struct Mystruct &);
121     extern void cfillcalol2(struct CaLevel1 &, struct CaLevel2 &);
122     extern void creadB(const char []);
123     extern void coptrklev2(const char [], struct Tracklev2 &, int &);
124     extern void copencalol2(char [], Int_t &, Int_t &, int &);
125     extern void cretrklev2(int &, Tracklev2 &);
126     extern void cclosecalol2(char []);
127     extern void ccltrklev2(struct Tracklev2 &);
128     extern const char *pathtocalibration();
129     #include <event/PamelaRun.h>
130     #include <event/physics/calorimeter/CalorimeterEvent.h>
131     #include <event/physics/trigger/TriggerEvent.h>
132     #include <event/CalibCalPedEvent.h>
133     #endif
134     //
135     #include <caloclasses.h>
136     #include <CaloFunctions.h>
137     //
138     #if !defined (__CINT__)
139     void unloadf77lib(TString Framework, Int_t MySQL){}
140     #endif
141     #if defined (__CINT__)
142     void unloadf77lib(TString Framework, Int_t MySQL){
143 mocchiut 1.3 const char *franame = Framework;
144     //
145     char *pam_lib=gSystem->Getenv("PAM_LIB");
146     if ( MySQL == 1 || MySQL == -1 ){
147 mocchiut 1.1 stringstream libload;
148     libload.str("");
149 mocchiut 1.3 libload << pam_lib << "/libreadmy";
150 mocchiut 1.1 gSystem->Unload(libload.str().c_str());
151 mocchiut 1.3 libload.str("");
152     libload << pam_lib << "/libreadmy_C";
153     gSystem->Unload(libload.str().c_str());
154     };
155     if ( !strcmp(franame,"paw") ) {
156 mocchiut 1.1 stringstream libload;
157     libload.str("");
158 mocchiut 1.3 libload << pam_lib << "/libclcalol2_C";
159 mocchiut 1.1 gSystem->Unload(libload.str().c_str());
160     libload.str("");
161 mocchiut 1.3 libload << pam_lib << "/libclcalol2";
162 mocchiut 1.1 gSystem->Unload(libload.str().c_str());
163     libload.str("");
164 mocchiut 1.3 libload << pam_lib << "/libopcalol2_C";
165 mocchiut 1.1 gSystem->Unload(libload.str().c_str());
166     libload.str("");
167 mocchiut 1.3 libload << pam_lib << "/libopcalol2";
168 mocchiut 1.1 gSystem->Unload(libload.str().c_str());
169 mocchiut 1.3 };
170     stringstream libload;
171     libload.str("");
172     libload << pam_lib << "/libcrcalol2_C";
173     gSystem->Unload(libload.str().c_str());
174     char *pam_lib=gSystem->Getenv("PAM_LIB");
175     stringstream libload;
176     libload.str("");
177     libload << pam_lib << "/libcrcalol2";
178     gSystem->Unload(libload.str().c_str());
179     char *pam_lib=gSystem->Getenv("PAM_LIB");
180     stringstream libload;
181     libload.str("");
182     libload << pam_lib << "/libreadb2maps_C";
183     gSystem->Unload(libload.str().c_str());
184     char *pam_lib=gSystem->Getenv("PAM_LIB");
185     stringstream libload;
186     libload.str("");
187     libload << pam_lib << "/libreadb2maps";
188     gSystem->Unload(libload.str().c_str());
189     char *pam_lib=gSystem->Getenv("PAM_LIB");
190     stringstream libload;
191     libload.str("");
192     libload << pam_lib << "/libtrack";
193     gSystem->Unload(libload.str().c_str());
194 mocchiut 1.1 }
195     #endif
196    
197     short int calolevel2core(Int_t ei, Int_t b[4] , TTree *otr, TTree *tree, CaLevel2 & clevel2, Evento & evento, Calib & calib, TString calcalibfile){
198 mocchiut 1.3 //
199     // Define variables
200     //
201     struct CaLevel1 clevel1;
202     clevel1.xalig = 120.4;
203     clevel1.yalig = 118.6;
204     clevel1.zalig = -260.;
205     struct Mystruct mystruct;
206     clevel1.paw = calib.ispaw;
207     Int_t done = 0;
208     Int_t pre = -1;
209     Int_t etime;
210     Int_t S3 = 0;
211     Int_t S2 = 0;
212     Int_t S12 = 0;
213     Int_t S11 = 0;
214     Int_t se = 5;
215     bool isCOMP = 0;
216     bool isFULL = 0;
217     bool isRAW = 0;
218     Float_t ener;
219     Float_t estripnull[96][22][2];
220     //
221     Int_t doneb = 0;
222     Int_t donec = 0;
223     Int_t ck = 0;
224     Int_t ipre = 0;
225     Int_t ip[3] = {0};
226     Float_t base0, base1, base2;
227     base0 = 0.;
228     base1 = 0.;
229     base2 = 0.;
230     Float_t qpre[6] = {0};
231     Float_t ene[96] = {0};
232     //
233     pamela::calorimeter::CalorimeterEvent *de = 0;
234     pamela::PscuHeader *ph = 0;
235     pamela::EventHeader *eh = 0;
236     pamela::trigger::TriggerEvent *trig = 0;
237     otr->SetBranchAddress("Header", &eh);
238     otr->SetBranchAddress("Calorimeter.Event", &de);
239     otr->SetBranchAddress("Trigger.Event", &trig);
240     //
241     // other variables
242     //
243     char *yodala2;
244     Int_t chdone[4] = {0,0,0,0};
245     Int_t pe = 0;
246     Float_t tmptrigty = -1.;
247     //
248     // start processing
249     //
250     //
251     // read from the header of the event the OBT and event number
252     //
253     otr->GetEntry(ei);
254     ph = eh->GetPscuHeader();
255     clevel2.pkt_num = ph->GetCounter();
256     etime = ph->GetOrbitalTime();
257     clevel2.OBT = etime;
258     clevel2.pro_num = (ei+1);
259     //
260     // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
261     //
262     S3 = 0;
263     S2 = 0;
264     S12 = 0;
265     S11 = 0;
266     S3 = trig->patterntrig[2];
267     S2 = trig->patterntrig[3];
268     S12 = trig->patterntrig[4];
269     S11 = trig->patterntrig[5];
270     if ( trig->patterntrig[0] ) tmptrigty = 2.;
271     if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
272     if ( trig->patterntrig[1] & (1<<0) || (!trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11) ) tmptrigty = 1.;
273     clevel2.trigty = tmptrigty;
274     //
275     yodala2 = "";
276     if ( calib.mysql ){
277     if ( !calib.obtjump ){
278     for (Int_t s = 0; s < 4; s++){
279     if ( etime > calib.time[s][0] ){
280     tryagain:
281     printf(" ** SECTION %i **\n",s);
282     stringstream qy;
283     qy.str("");
284     Int_t chkmysql = 0;
285     stringstream myfile;
286     const char *tabula = calib.tablename;
287     qy << "select * from " << tabula;
288     qy << " where section = " << s;
289     qy << " and fromtime >= " << calib.time[s][0];
290     qy << " limit 1";
291     chkmysql = creadmysql(calib.db,(char *)qy.str().c_str(),mystruct);
292     if ( chkmysql ) {
293     printf("- ERROR: problems querying MySQL database! -\n- ERROR: Empty table? -\n\n");
294     return(1);
295     } else {
296     if ( mystruct.totime == -1 ){
297     printf("- ERROR: problems querying MySQL database! -\n- ERROR: Corrupted table? -\n\n");
298     return(2);
299     };
300     calib.time[s][0] = mystruct.totime;
301     calib.time[s][1] = mystruct.fromtime;
302     calib.ttime[s][0] = mystruct.calibtime;
303     const char *caliba = calib.basepath;
304     const char *yodala = calib.yodalev;
305     yodala2 = (char *)yodala;
306     const char *fila = mystruct.calcalibfile;
307     myfile.str("");
308     myfile << caliba;
309     if ( !calib.DW ) {
310     myfile << fila;
311     } else {
312     myfile << "DW";
313     string myfil = (const char*)fila;
314     Int_t myposiz = myfil.find("dw_");
315     TString mytmp;
316     Int_t mspos = myposiz+2;
317     Int_t mepos = myposiz+13;
318     stringcopy(mytmp,fila,mspos,mepos);
319     const char *myf = mytmp;
320     myfile << mytmp;
321     };
322     myfile << yodala << "/";
323     calib.obtjump = mystruct.obtjump;
324     printf(" - event at time %i. From time %i to time %i \n use calibration at time %i, file %s\n",etime,calib.time[s][1],calib.time[s][0],calib.ttime[s][0],myfile.str().c_str());
325     if ( calib.obtjump ) printf("\n WARNING: jump in the OBT! this calibration might not be the best! \n");
326     if ( etime > calib.time[s][0] && !calib.obtjump ){
327     printf("\n WARNING: event at time greater than the upper limit for this calibration!\nSearch for the correct calibration\n");
328     goto tryagain;
329     };
330     };
331     Int_t pedeerr = 0;
332     pedeerr = CaloPede(myfile.str().c_str(),s,calib.ttime[s][0],calib);
333     if ( pedeerr ) {
334     printf("\n\nERROR: problems opening calibration file! \n\nERROR: YODA version of the calibration file is not %s? \n",yodala2);
335     return(1);
336     };
337     printf("\n");
338     };
339     };
340     };
341     } else {
342 mocchiut 1.1 //
343 mocchiut 1.3 // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration
344 mocchiut 1.1 //
345 mocchiut 1.3 if ( !calib.obtjump ) {
346     for (Int_t s = 0; s < 4; s++){
347     if ( calib.ttime[s][b[s]] ){
348     while ( etime > calib.time[s][b[s]+1] ){
349     printf(" CALORIMETER: \n" );
350     printf(" - Section %i, event at time %i while old calibration time limit at %i. Use new calibration at time %i -\n",s,etime,calib.time[s][b[s]+1],calib.ttime[s][b[s]+1]);
351     printf(" END CALORIMETER. \n\n" );
352     b[s]++;
353     TString pfile;
354     if ( calib.fcode[s][b[s]] != 10 ){
355     TString file2f = "";
356     stringcopy(file2f,calcalibfile);
357     TString pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);
358     } else {
359     pfile = (TString)calcalibfile;
360 mocchiut 1.1 };
361 mocchiut 1.3 CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
362     };
363     };
364     };
365     };
366     };
367     //
368     // run over views and planes
369     //
370     for (Int_t l = 0; l < 2; l++){
371     for (Int_t m = 0; m < 22; m++){
372     //
373     // determine the section number
374     //
375     se = 5;
376     if (l == 0 && m%2 == 0) se = 3;
377     if (l == 0 && m%2 != 0) se = 2;
378     if (l == 1 && m%2 == 0) se = 1;
379     if (l == 1 && m%2 != 0) se = 0;
380     //
381     // determine what kind of event we are going to analyze
382     //
383     isCOMP = 0;
384     isFULL = 0;
385     isRAW = 0;
386     if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;
387     if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;
388     if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;
389     if ( !chdone[se] ){
390     //
391     // check for any error in the event
392     //
393     clevel2.crc[se] = 0;
394     if ( de->perror[se] == 132 ){
395     clevel2.crc[se] = 1;
396     pe++;
397     };
398     clevel2.perr[se] = 0;
399     if ( de->perror[se] != 0 ){
400     clevel2.perr[se] = 1;
401     pe++;
402     };
403     clevel2.swerr[se] = 0;
404     for (Int_t j = 0; j < 7 ; j++){
405     if ( (j != 3) && (de->stwerr[se] & (1 << j)) ){
406     clevel2.swerr[se] = 1;
407     pe++;
408     };
409     };
410     chdone[se] = 1;
411     };
412     if ( clevel2.crc[se] == 0 && (calib.good2 == 1 || clevel2.trigty == 2)){
413     pre = -1;
414     if ( isRAW ){
415     for (Int_t nn = 0; nn < 96; nn++){
416     evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;
417     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
418     };
419 mocchiut 1.1 };
420     //
421 mocchiut 1.3 // run over preamplifiers
422 mocchiut 1.1 //
423 mocchiut 1.3 pre = -1;
424     for (Int_t i = 0; i < 3; i++){
425     for (Int_t j = 0; j < 2; j++){
426     pre = j + i*2;
427     //
428     // baseline check and calculation
429     //
430     if ( !isRAW ) {
431     evento.base[l][m][pre] = de->base[l][m][pre] ;
432     } else {
433     //
434     // if it is a raw event and we haven't checked
435     // yet, calculate the baseline.
436     //
437     CaloFindBaseRaw(calib,evento,l,m,pre);
438 mocchiut 1.1 };
439 mocchiut 1.3 };
440 mocchiut 1.1 };
441 mocchiut 1.3 //
442     // run over strips
443     //
444     pre = -1;
445     for (Int_t i = 0 ; i < 3 ; i++){
446     // ck = 0;
447     ip[i] = 0;
448     for (Int_t n = i*32 ; n < (i+1)*32 ; n++){
449     if (n%16 == 0) {
450     done = 0;
451     doneb = 0;
452     donec = 0;
453     ck = 0;
454     pre++;
455     qpre[pre] = 0;
456     };
457     //
458     // baseline check and calculation
459 mocchiut 1.1 //
460 mocchiut 1.3 if ( !isRAW ) {
461     evento.dexy[l][m][n] = de->dexy[l][m][n] ;
462     evento.dexyc[l][m][n] = de->dexyc[l][m][n] ;
463     };
464 mocchiut 1.1 //
465 mocchiut 1.3 // no suitable new baseline, use old ones and compress data!
466 mocchiut 1.1 //
467 mocchiut 1.3 if ( !done ){
468     if ( (evento.base[l][m][pre] == 31000. || evento.base[l][m][pre] == 0.) ){
469     ck = 1;
470     if (pre%2 == 0) {
471     ip[i] = pre + 1;
472     } else {
473     ip[i] = pre - 1;
474 mocchiut 1.1 };
475 mocchiut 1.3 if ( (evento.base[l][m][ip[i]] == 31000. || evento.base[l][m][ip[i]] == 0.) ){
476     //
477     ck = 2;
478     if ( calib.sbase[l][m][pre] == 31000. || calib.sbase[l][m][pre] == 0. ) {
479     ck = 3;
480     };
481 mocchiut 1.1 };
482 mocchiut 1.3 // upnn = n+16;
483     //if ( upnn > 96 ) nn = 96;
484     //for ( Int_t nn = n; nn<upnn; nn++ ){
485     // evento.dexyc[l][m][nn] = de->dexyc[l][m][nn];
486     //};
487     //CaloCompressData(calib,evento,l,m,pre);
488     done = 1;
489     };
490     };
491     //
492     // CALIBRATION ALGORITHM
493     //
494     if ( !doneb ){
495     switch (ck) {
496     case 0:
497     base0 = evento.base[l][m][pre];
498     base2 = calib.calbase[l][m][pre];
499     break;
500     case 1:
501     base0 = evento.base[l][m][ip[i]];
502     base2 = calib.calbase[l][m][ip[i]];
503     break;
504     case 2:
505     base0 = calib.sbase[l][m][pre];
506     base2 = calib.calbase[l][m][pre];
507     break;
508     case 3:
509     base0 = calib.calbase[l][m][pre];
510     base2 = calib.calbase[l][m][pre];
511     break;
512     };
513     base1 = calib.calbase[l][m][pre];
514     doneb = 1;
515 mocchiut 1.1 };
516 mocchiut 1.3 ener = evento.dexyc[l][m][n];
517     clevel1.estrip[n][m][l] = 0.;
518     if ( base0>0 && base0 < 30000. ){
519     if ( !donec && (base0 - base1 + base2) != 0. ){
520     calib.sbase[l][m][pre] = base0 - base1 + base2;
521     donec = 1;
522     };
523     if ( ener > 0. ){
524     clevel1.estrip[n][m][l] = (ener - calib.calped[l][m][n] - base0 - base1 + base2)/calib.mip[l][m][n] ;
525 mocchiut 1.1 //
526 mocchiut 1.3 // OK, now in estrip we have the energy deposit in MIP of all the strips for this event (at the end of loops of course)
527 mocchiut 1.1 //
528 mocchiut 1.3 qpre[pre] += clevel1.estrip[n][m][l];
529     };
530     };
531     };
532     if (ck == 1){
533     if (ip[i]%2 == 0)
534     ipre = ip[i] + 1;
535     else
536     ipre = ip[i] - 1;
537     for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
538     clevel1.estrip[j][m][l] = clevel1.estrip[j][m][l] + (qpre[ipre] - qpre[ip[i]]) * 0.00478;
539     };
540     };
541     if (ck == 2){
542     for (Int_t j = i*32 ; j < (i+1)*32 ; j++){
543     ipre = j/16 + 1;
544     clevel1.estrip[j][m][l] = clevel1.estrip[j][m][l] + qpre[ipre] * 0.00478;
545     };
546     };
547     };
548     //
549     Int_t j4 = -4;
550     Int_t jjj = -3;
551     Int_t jj = -2;
552     for (Int_t j = 0 ; j < 100 ; j++){
553     jj++;
554     jjj++;
555     j4++;
556     if ( j < 96 ) ene[j] = clevel1.estrip[j][m][l];
557     if ( jj >= 0 && jj < 96 ){
558     if ( jj != 0 && jj != 32 && jj != 64 ) ene[jj-1] = ene[jj-1] - clevel1.estrip[jj][m][l] * 0.01581;
559     if ( jj != 31 && jj != 63 && jj != 95 ) ene[jj+1] = ene[jj+1] - clevel1.estrip[jj][m][l] * 0.01581;
560     };
561     if ( jjj >= 0 && jjj < 96 ){
562     if ( jjj != 0 && jjj != 32 && jjj != 64 ) clevel1.estrip[jjj-1][m][l] = clevel1.estrip[jjj-1][m][l] - ene[jjj] * 0.01581;
563     if ( jjj != 31 && jjj != 63 && jjj != 95 ) clevel1.estrip[jjj+1][m][l] = clevel1.estrip[jjj+1][m][l] - ene[jjj] * 0.01581;
564     };
565     if ( j4 >= 0 && j4 < 96 ){
566     if ( clevel1.estrip[j4][m][l]!=0. && ( clevel1.estrip[j4][m][l] < evento.emin || calib.calrms[l][m][j4] > 26 )){
567     clevel1.estrip[j4][m][l] = 0.;
568 mocchiut 1.1 };
569 mocchiut 1.3 };
570     };
571     //
572     } else {
573     for (Int_t nn = 0; nn < 96; nn++){
574     clevel1.estrip[nn][m][l] = 0.;
575 mocchiut 1.1 };
576     };
577     };
578 mocchiut 1.3 };
579     //
580     //
581     //
582     if ( !pe && calib.good2 ){
583     clevel2.good = 1;
584     } else {
585     clevel2.good = 0;
586     };
587     //
588     // all we need from the tracker is the state vector al_p and the boolean for good events:
589     //
590     clevel1.trkchi2 = calib.trkchi2;
591     if ( calib.good2 ){
592     for (Int_t e = 0; e<5 ; e++){
593     clevel1.al_p[e][0] = calib.al_p[e][0];
594     clevel1.al_p[e][1] = calib.al_p[e][1];
595     };
596     clevel1.good2 = 1;
597     } else {
598     for (Int_t e = 0; e<5 ; e++){
599     clevel1.al_p[e][0] = 0.;
600     clevel1.al_p[e][1] = 0.;
601     };
602     clevel1.good2 = 0;
603     };
604     //
605     // for each event generate level2 data (clevel2) calling the fortran routine and passing calibrated data (clevel1):
606     //
607     cfillcalol2(clevel1,clevel2);
608     //
609     // save in the class the level2 calorimeter informations
610     //
611     //
612     // fill the rootple if not in paw mode
613     //
614     if ( clevel1.paw == 0. ) tree->Fill();
615     //
616     // clear estrip values.
617     //
618     memcpy(clevel1.estrip, estripnull, sizeof(estripnull));
619     //
620     return(0);
621 mocchiut 1.1 }
622     //
623     short int CaloLEVEL2(TString filename, TString TrackerDir="", TString outDir ="", TString Framework = "root", Int_t FORCE = 0){
624 mocchiut 1.3 //
625     // define variables
626     //
627     Int_t swcode = 04;
628     Int_t swtrkcode = 0;
629     Int_t MySQL = 1;
630     Int_t debug = 0;
631     //
632 mocchiut 1.1 #if defined(__CINT__)
633 mocchiut 1.3 if ( !gSystem->Getenv("PAM_INC") || !gSystem->Getenv("PAM_LIB") || !gSystem->Getenv("PAM_CALIB") ){
634     printf("\n ERROR: you first must set the PAMELA enviromental variables\n read the README file \n");
635     printf("\n Exiting... \n");
636     return(11);
637     };
638     emicheckLib();
639     #endif
640     Int_t trkdir = 1;
641     TString trackerdir;
642     if ( TrackerDir=="" ){
643     trackerdir = filename;
644     trkdir = 0;
645     } else {
646     trackerdir = TrackerDir;
647     };
648     TString calcalibfile;
649     //
650     // other variables
651     //
652     TFile *headerFile;
653     TFile *caloFile;
654     TFile *triggerFile;
655     TString filety;
656     stringstream file2;
657     stringstream file3;
658     const char *file4;
659     Int_t okcalo = 0;
660     TTree *ctree = 0;
661     TFile *chfile;
662     Long64_t cnevents=0;
663     stringstream qy;
664     qy.str("");
665     Int_t chkmysql = 0;
666     Int_t imtrack;
667     //
668     // load libraries to determine the track information.
669     //
670     #if defined (__CINT__)
671     const char *pam_lib=gSystem->Getenv("PAM_LIB");
672     stringstream libload;
673     libload.str("");
674     libload << pam_lib << "/libtrack";
675     gSystem->Load(libload.str().c_str());
676     libload.str("");
677     libload << pam_lib << "/libreadb2maps";
678     gSystem->Load(libload.str().c_str());
679     libload.str("");
680     libload << pam_lib << "/libreadb2maps_C";
681     gSystem->Load(libload.str().c_str());
682     //
683     // open libraries to extract calorimeter level2 data
684     //
685     libload.str("");
686     libload << pam_lib << "/libcrcalol2";
687     gSystem->Load(libload.str().c_str());
688     libload.str("");
689     libload << pam_lib << "/libcrcalol2_C";
690     gSystem->Load(libload.str().c_str());
691 mocchiut 1.1 #endif
692 mocchiut 1.3 const char *franame = Framework;
693 mocchiut 1.1 #if defined (__CINT__)
694 mocchiut 1.3 if ( !strcmp(franame,"paw") ) {
695 mocchiut 1.1 libload.str("");
696 mocchiut 1.3 libload << pam_lib << "/libopcalol2";
697 mocchiut 1.1 gSystem->Load(libload.str().c_str());
698     libload.str("");
699 mocchiut 1.3 libload << pam_lib << "/libopcalol2_C";
700 mocchiut 1.1 gSystem->Load(libload.str().c_str());
701     libload.str("");
702 mocchiut 1.3 libload << pam_lib << "/libclcalol2";
703 mocchiut 1.1 gSystem->Load(libload.str().c_str());
704     libload.str("");
705 mocchiut 1.3 libload << pam_lib << "/libclcalol2_C";
706 mocchiut 1.1 gSystem->Load(libload.str().c_str());
707 mocchiut 1.3 };
708 mocchiut 1.1 #endif
709 mocchiut 1.3 if ( MySQL != 1 ){
710     printf("\nWARNING: you MUST run this script using the MySQL database. Reverting MySQL to 1 \n\n");
711     MySQL = 1;
712     };
713     //MySQL = 0;
714     gDirectory->GetList()->Delete();
715     const char* startingdir = gSystem->WorkingDirectory();
716     TString path;
717     stringcopy(path,startingdir);
718     //
719     #if defined (__CINT__)
720     Int_t MySQLload = 0;
721 mocchiut 1.1 #endif
722 mocchiut 1.3 //
723     // Open files
724     //
725     headerFile = emigetFile(filename, "Physics", "Header");
726     if ( !headerFile ){
727     printf("\n\n ERROR: no header file, exiting...\n\n");
728     #if defined (__CINT__)
729     gSystem->ChangeDirectory(path);
730     unloadf77lib(Framework,MySQLload);
731 mocchiut 1.1 #endif
732 mocchiut 1.3 return(1);
733     };
734     caloFile = emigetFile(filename, "Calorimeter");
735     if ( !caloFile ){
736     printf("\n\n ERROR: no calorimeter file! \n\n");
737 mocchiut 1.1 #if defined (__CINT__)
738 mocchiut 1.3 gSystem->ChangeDirectory(path);
739     unloadf77lib(Framework,MySQLload);
740 mocchiut 1.1 #endif
741 mocchiut 1.3 return(2);
742     };
743     //
744     TTree *otr = (TTree*)headerFile->Get("Pscu");
745     otr->AddFriend("Calorimeter",caloFile);
746     triggerFile = emigetFile(filename, "Trigger");
747     if ( !triggerFile ){
748     printf("\n\n ERROR: No trigger file! \n");
749 mocchiut 1.1 #if defined (__CINT__)
750 mocchiut 1.3 gSystem->ChangeDirectory(path);
751     unloadf77lib(Framework,MySQLload);
752 mocchiut 1.1 #endif
753 mocchiut 1.3 return(1);
754     } else {
755     otr->AddFriend("Trigger",triggerFile);
756     };
757     //
758     // Define some basic variables
759     //
760     struct Evento evento;
761     struct Calib calib;
762     struct Mystruct mystruct;
763     evento.emin = 0.7;
764     Long64_t nevents = otr->GetEntries();
765     Int_t snevents = (Int_t)nevents;
766     if ( nevents < 1 ) {
767     printf(" ERROR: the file is empty!\n\n");
768 mocchiut 1.1 #if defined (__CINT__)
769 mocchiut 1.3 gSystem->ChangeDirectory(path);
770     unloadf77lib(Framework,MySQLload);
771 mocchiut 1.1 #endif
772 mocchiut 1.3 return(7);
773     };
774     Int_t b[4];
775     Int_t paw = 0;
776     //
777     const string myfil = (const char *)filename;
778     Int_t myposiz = myfil.find("dw_");
779     calib.DW = 0;
780     if ( myposiz == -1 ) {
781     myposiz = myfil.find("DW_");
782     calib.DW = 1;
783     };
784     if ( myposiz == -1 ) return(6);
785     stringcopy(calib.basepath,filename,0,myposiz);
786     stringcopy(calib.yodalev,filename,myposiz+13,myposiz+15);
787     //
788     // calibration filename
789     //
790     calcalibfile = filename;
791     //
792     // create the ntuple level2 name depending on framework and starting from the YODA name
793     //
794     if ( !strcmp(franame,"paw") ) paw = 1;
795     //
796     if ( paw ){
797     calib.ispaw = 1.;
798     filety = "rz";
799     } else {
800     calib.ispaw = 0.;
801     filety = "root";
802     };
803     //
804     TString numb("2"); // level
805     TString detc("Calorimeter"); // detector
806     char *file = getLEVname(filename,detc,numb,filety);
807     stringstream truefile;
808     truefile.str("");
809     truefile << file;
810     //
811     if ( outDir == "" ){
812     file4 = filename;
813     file3.str("");
814     file3 << file4 << "/Physics/Level2";
815     } else {
816     file4 = outDir;
817     file3.str("");
818     file3 << file4;
819     }
820     //
821     file2.str("");
822     file2 << file3.str().c_str() << "/";
823     file2 << truefile.str().c_str();
824     printf("\n Filename will be: \n %s \n\n",file2.str().c_str());
825     //
826     // check if Level2 directory exists, if not create it.
827     //
828     Int_t ERR = gSystem->MakeDirectory(file3.str().c_str());
829     //
830     if ( !ERR ) {
831     printf(" LEVEL2 directory doesn't exist! Creating LEVEL2 directory \n\n");
832     } else {
833     //
834     // directory exists, check if file exists (if we are not in the force mode)
835     //
836     if ( !FORCE ) {
837     printf(" Not in FORCE mode, check the existence of LEVEL2 data: \n\n");
838     Int_t nofile = 0;
839     if ( paw ){
840     ifstream mypawfile;
841     mypawfile.open(file2.str().c_str());
842     if ( mypawfile ){
843     nofile = 1;
844     } else {
845     printf("Error in opening file: file %s does not exist \n",file2.str().c_str());
846     };
847     } else {
848     TFile tfile(file2.str().c_str());
849     if ( !tfile.IsZombie() ) nofile = 1;
850     };
851     if ( nofile ){
852     printf(" ERROR: file already exists! Use FORCE = 1 to override \n\n");
853 mocchiut 1.1 #if defined (__CINT__)
854     gSystem->ChangeDirectory(path);
855     unloadf77lib(Framework,MySQLload);
856     #endif
857 mocchiut 1.3 return(3);
858     } else {
859     printf("\n OK, I will create it!\n\n");
860     };
861 mocchiut 1.1 };
862 mocchiut 1.3 };
863     //
864     // clear calibration time intervals
865     //
866     for (Int_t s=0; s<4;s++){
867     for (Int_t d = 0; d<51; d++){
868     calib.ttime[s][d] = 0 ;
869     calib.time[s][d] = 0 ;
870     };
871     };
872     //
873     // open and read the rootple containing the ADC to MIP conversion values
874     //
875     stringstream calfile;
876     calfile.str("");
877     const char *pam_calib = pathtocalibration();
878     calfile << pam_calib << "/CaloADC2MIP.root";
879     //
880     printf(" Using calorimeter calibration file: \n %s \n",calfile.str().c_str());
881     chfile = new TFile(calfile.str().c_str(),"READ","Calorimeter CALIBRATION data");
882     CalorimeterCalibration *ccalo = 0;
883     if ( chfile->IsZombie() ){
884     if ( !FORCE ){
885     printf("\n ERROR: no calorimeter calibration file! \n If you want to proceed using 26 as conversion factor for all strips use FORCE = 1. \n\n");
886     return(10);
887 mocchiut 1.1 } else {
888 mocchiut 1.3 printf("\n WARNING: no calorimeter calibration file! \n WARNING: using 26 as conversion factor for all strips. \n");
889     okcalo = 0;
890 mocchiut 1.1 };
891 mocchiut 1.3 } else {
892     okcalo = 1;
893     ctree = (TTree*)chfile->Get("CaloADC");
894     ctree->SetBranchAddress("Event", &ccalo);
895     //
896     cnevents = ctree->GetEntries();
897     ctree->GetEntry(cnevents-1);
898     };
899     //
900     if ( okcalo ) {
901     for (Int_t m = 0; m < 2 ; m++ ){
902     for (Int_t k = 0; k < 22; k++ ){
903     for (Int_t l = 0; l < 96; l++ ){
904     if ( (ccalo->fp[1][m][k][l] > 20. && ccalo->fp[1][m][k][l] < 32.) || ccalo->mask[m][k][l] == 1. ) {
905     calib.mip[m][k][l] = ccalo->mip[m][k][l];
906     if ( calib.mip[m][k][l] == 0 ) calib.mip[m][k][l] = 26. ;
907     } else {
908     calib.mip[m][k][l] = 26. ;
909     };
910     calib.calped[m][k][l] = 0. ;
911     evento.dexy[m][k][l] = 0. ;
912     evento.dexyc[m][k][l] = 0. ;
913 mocchiut 1.1 };
914 mocchiut 1.3 };
915 mocchiut 1.1 };
916 mocchiut 1.3 } else {
917     for (Int_t m = 0; m < 2 ; m++ ){
918     for (Int_t k = 0; k < 22; k++ ){
919     for (Int_t l = 0; l < 96; l++ ){
920     calib.mip[m][k][l] = 26. ;
921     calib.calped[m][k][l] = 0. ;
922     evento.dexy[m][k][l] = 0. ;
923     evento.dexyc[m][k][l] = 0. ;
924 mocchiut 1.1 };
925 mocchiut 1.3 };
926 mocchiut 1.1 };
927 mocchiut 1.3 };
928     chfile->Close();
929     //
930     // Are we able to connect to the MySQL server?
931     //
932     if ( MySQL ){
933     calib.mysql = 1;
934     gSystem->ChangeDirectory(path);
935     #if defined (__CINT__)
936     libload.str("");
937     libload << pam_lib << "/libreadmy";
938     gSystem->Load(libload.str().c_str());
939     libload.str("");
940     libload << pam_lib << "/libreadmy_C";
941     gSystem->Load(libload.str().c_str());
942     #endif
943     calib.db = "romemuons";
944     string myfil = (const char*)filename;
945     Int_t myposiz = myfil.find("dw_");
946     if ( myposiz == -1 ) myposiz = myfil.find("DW_");
947     if ( myposiz == -1 ) return(6);
948 mocchiut 1.1 //
949 mocchiut 1.3 TString mytmp;
950     Int_t mspos = myposiz+2;
951     Int_t mepos = myposiz+13;
952     stringcopy(mytmp,filename,mspos,mepos);
953     const char *myfile = mytmp;
954     //
955     stringstream tablename;
956     tablename.str("");
957     tablename << "calocalib_dw" << myfile;
958     TString tabula;
959     tabula = tablename.str().c_str();
960 mocchiut 1.1 //
961 mocchiut 1.3 printf("\n Try the connection to the MySQL database in Trieste...\n");
962     qy.str("");
963     qy << "select * from " << tablename.str().c_str();
964     chkmysql = creadmysql(calib.db,(char *)qy.str().c_str(),mystruct);
965     if ( chkmysql || mystruct.totime == -1 ) {
966     printf("\n- ERROR: problems querying MySQL database! -\n\n- ERROR: Cannot use the MySQL database! -\n\n");
967     if ( mystruct.totime == -1 ) printf("- ERROR: it seems there are no data in table called %s -\n\n",tablename.str().c_str());
968     if ( chkmysql == 1 ) printf("- ERROR: problems in MySQL initialization -\n\n");
969     if ( chkmysql == 2 ) printf("- ERROR: problems in MySQL login, check username and password -\n\n");
970     if ( chkmysql == 3 ) printf("- ERROR: it seems there is no table called %s -\n\n",tablename.str().c_str());
971     // if ( !FORCE ){
972     // printf(" Exiting! Use FORCE = 1 or MySQL = 0 to run in standalone mode \n\n");
973     printf(" Exiting! Check your installation or contact calorimeter developers \n\n");
974     #if defined (__CINT__)
975     gSystem->ChangeDirectory(path);
976     unloadf77lib(Framework,MySQL);
977     #endif
978     return(5);
979     //};
980     // printf("WARNING: running in standalone mode! \n\n");
981     // printf("WARNING: I will search for the best calibration\n\n");
982     // calib.mysql = 0;
983     // MySQL = -1;
984 mocchiut 1.1 } else {
985 mocchiut 1.3 printf(" ...OK, the connection is fine! \n\n Using database \"%s\", table \"%s\"\n\n",calib.db,tablename.str().c_str());
986 mocchiut 1.1 };
987 mocchiut 1.3 calib.tablename = tablename.str().c_str();
988     } else {
989     calib.tablename = "";
990     calib.mysql = 0;
991     };
992     if ( MySQL < 1 ){
993     //
994     // first of all find the best calibrations for this file
995     //
996     Int_t wused = 0;
997     TString nfilen;
998     stringcopy(nfilen,file2.str().c_str());
999     CaloFindCalibs(filename, calcalibfile, wused, calib);
1000     if ( wused == 1 ) calcalibfile = filename;
1001     //
1002     // print on the screen the results:
1003     //
1004     const char *ffile = filename;
1005     printf(" ------ %s ------- \n \n",ffile);
1006     Int_t calibex = 0;
1007     for (Int_t s=0; s<4;s++){
1008     printf(" ** SECTION %i **\n",s);
1009     for (Int_t d = 0; d<51; d++){
1010     TString pfile;
1011     if ( calib.ttime[s][d] != 0 ) {
1012     calibex++;
1013     if ( calib.fcode[s][d] != 10 ){
1014     TString file2f = "";
1015     stringcopy(file2f,calcalibfile);
1016     pfile = whatnamewith(file2f,calib.fcode[s][d]);
1017     } else {
1018     pfile = (TString)calcalibfile;
1019     };
1020     const char *ffile = pfile;
1021     printf(" - from time %i to time %i use calibration at\n time %i, file: %s \n",calib.time[s][d],calib.time[s][d+1],calib.ttime[s][d],ffile);
1022     if ( !strcmp(ffile,"wrong") ) calibex--;
1023 mocchiut 1.1 };
1024 mocchiut 1.3 };
1025     printf("\n");
1026 mocchiut 1.1 };
1027 mocchiut 1.3 printf(" ----------------------------------------------------------------------- \n \n");
1028    
1029 mocchiut 1.1 //
1030 mocchiut 1.3 // const char *file2 = nfilen; // the full path and name of the level1 ntuple
1031     file2.str("");
1032     file2 << nfilen;
1033     if ( calibex < 4 ) {
1034     printf("No full calibration data in this file, sorry!\n\n ");
1035     //
1036     // remove the empty file!
1037     //
1038     stringstream remfile;
1039     remfile.str("");
1040     remfile << "rm -f " << file2.str().c_str();
1041     gSystem->Exec(remfile.str().c_str());
1042 mocchiut 1.1 #if defined (__CINT__)
1043 mocchiut 1.3 gSystem->ChangeDirectory(path);
1044     unloadf77lib(Framework,MySQL);
1045 mocchiut 1.1 #endif
1046 mocchiut 1.3 return(4);
1047 mocchiut 1.1 };
1048    
1049 mocchiut 1.3 //
1050     // fill with the first calibration values the common containing calibration informations
1051     //
1052     for (Int_t s = 0; s < 4; s++){
1053     b[s]=0;
1054     TString pfile;
1055     if ( calib.fcode[s][b[s]] != 10 ){
1056     TString file2f = "";
1057     stringcopy(file2f,calcalibfile);
1058     pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);
1059     } else {
1060     pfile = (TString)calcalibfile;
1061     };
1062     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
1063     };
1064     } else {
1065     calib.obtjump = 0;
1066     calib.time[0][0] = -1;
1067     calib.time[1][0] = -1;
1068     calib.time[2][0] = -1;
1069     calib.time[3][0] = -1;
1070     };
1071    
1072     //
1073     // Open file to write depending on the framework
1074     //
1075     //
1076     // In any case we must create a tree
1077     //
1078     TTree *tree = 0;
1079     //
1080     // Open tracker ntuple or rootples and load magnetic field maps.
1081     //
1082     //
1083     printf(" TRACKER: loading the magnetic field maps...\n\n\n");
1084     pam_calib = pathtocalibration();
1085     stringstream bdir;
1086     bdir.str("");
1087     bdir << pam_calib << ".";
1088     creadB(bdir.str().c_str());
1089     //
1090     printf(" ...done! \n");
1091     //
1092     printf("\n Check the existence of tracker data... \n");
1093     Int_t isrootple = 1;
1094     struct Tracklev2 trk;
1095     TFile *trfile = emigetFile(trackerdir,"Physics.Level2","Tracker");
1096     TTree *tr = 0;
1097     Int_t trkpaw = 0;
1098     Int_t trnevents = 0;
1099     stringstream filetrk;
1100     if ( !trfile ) {
1101     const char *file0 = filename;
1102     string fil = (const char *)filename;
1103     Int_t posiz = fil.find("dw_");
1104     if ( posiz == -1 ) posiz = fil.find("DW_");
1105     if ( posiz == -1 ) return(5);
1106     //
1107     TString trntmp;
1108     Int_t spos = posiz+3;
1109     Int_t epos = posiz+13;
1110     stringcopy(trntmp,filename,spos,epos);
1111     const char *trname = trntmp;
1112     if ( !trkdir ){
1113     filetrk.str("");
1114     filetrk << file0 << "/Physics/Level2/DW_";
1115     filetrk << trname << "_level2.rz";
1116 mocchiut 1.1 } else {
1117 mocchiut 1.3 const char *trackdir = trackerdir;
1118     filetrk.str("");
1119     filetrk << trackdir << "/DW_";
1120     filetrk << trname << "_level2.rz";
1121 mocchiut 1.1 };
1122     //
1123 mocchiut 1.3 ifstream mypawfile;
1124     mypawfile.open(filetrk.str().c_str());
1125     if ( mypawfile ){
1126     trkpaw = 1;
1127     isrootple = 0;
1128     printf(" ...found tracker level2 NTUPLE:\n %s \n\n",filetrk.str().c_str());
1129 mocchiut 1.1 #if defined (__CINT__)
1130 mocchiut 1.3 const char *sdir=gSystem->Getenv("PAM_LIB");
1131     bdir.str("");
1132     bdir << sdir << "/liboptrklev2.so";
1133     gSystem->Load(bdir.str().c_str());
1134     bdir.str("");
1135     bdir << sdir << "/liboptrklev2_C.so";
1136     gSystem->Load(bdir.str().c_str());
1137     bdir.str("");
1138     bdir << sdir << "/libretrklev2.so";
1139     gSystem->Load(bdir.str().c_str());
1140     bdir.str("");
1141     bdir << sdir << "/libretrklev2_C.so";
1142     gSystem->Load(bdir.str().c_str());
1143     bdir.str("");
1144     bdir << sdir << "/libcltrklev2.so";
1145     gSystem->Load(bdir.str().c_str());
1146     bdir.str("");
1147     bdir << sdir << "/libcltrklev2_C.so";
1148     gSystem->Load(bdir.str().c_str());
1149 mocchiut 1.1 #endif
1150 mocchiut 1.3 trnevents = 0;
1151     coptrklev2((char *)filetrk.str().c_str(),trk,trnevents);
1152 mocchiut 1.1 } else {
1153 mocchiut 1.3 printf("Error in opening file: file %s does not exist \n",file2.str().c_str());
1154     printf("\n No tracker data! You must first process data with tracker programs\n and then you can run CaloLEVEL2. \n Exiting... \n\n");
1155     return(8);
1156     };
1157     } else {
1158     //
1159     const char *file0 = filename;
1160     string fil = (const char *)filename;
1161     Int_t posiz = fil.find("dw_");
1162     if ( posiz == -1 ) posiz = fil.find("DW_");
1163     if ( posiz == -1 ) return(5);
1164     //
1165     TString trntmp;
1166     Int_t spos = posiz;
1167     Int_t epos = posiz+15;
1168     stringcopy(trntmp,filename,spos,epos);
1169     const char *trname = trntmp;
1170     //
1171     if ( !trkdir ){
1172     filetrk.str("");
1173     filetrk << file0 << "/Physics/Level2/";
1174     filetrk << trname << ".Physics.Level2.Tracker.Event.root";
1175 mocchiut 1.1 } else {
1176 mocchiut 1.3 const char *trkdirname = trackerdir;
1177     filetrk.str("");
1178     filetrk << trkdirname << "/";
1179     filetrk << trname << ".Physics.Level2.Tracker.Event.root";
1180     };
1181     //
1182     printf(" ...found tracker level2 ROOTPLE:\n %s \n",filetrk.str().c_str());
1183     tr = (TTree*) trfile->Get("TrkLevel2");
1184     settrklev2(tr,trk);
1185     trnevents = tr->GetEntries();
1186     };
1187     if ( trnevents != nevents ){
1188     printf("\n WARNING: different length for tracker and calorimeter ntuples!\n");
1189     printf(" calorimeter: %i events \n",(int)nevents);
1190     printf(" tracker : %i events \n\n",(int)trnevents);
1191     };
1192     //
1193     swtrkcode = trk.swcode;
1194     //
1195     stringstream name;
1196     TString tmpfile2;
1197     stringcopy(tmpfile2,file2.str().c_str());
1198     const char *tmpname = tmpfile2;
1199     TFile *hfile = 0;
1200     struct CaLevel2 clevel2;
1201     if ( paw ){
1202     name.str("");
1203     name << tmpname;
1204     copencalol2((char *)name.str().c_str(),trkpaw,swcode,swtrkcode);
1205     } else {
1206     char *type;
1207     type = "NEW";
1208     if ( FORCE ) type = "RECREATE";
1209     hfile = new TFile(file2.str().c_str(),type,"Calorimeter LEVEL2 data");
1210     //
1211     // hfile = new TFile(file2,type,"Calorimeter LEVEL2 data",0);
1212     //
1213     tree = new TTree("CaloLevel2","PAMELA Level2 calorimeter data");
1214     tree->Branch("OBT",&clevel2.OBT,"OBT/I");
1215     tree->Branch("pkt_num",&clevel2.pkt_num,"pkt_num/I");
1216     tree->Branch("pro_num",&clevel2.pro_num,"pro_num/I");
1217     tree->Branch("trigty",&clevel2.trigty,"trigty/F");
1218     tree->Branch("good",&clevel2.good,"good/I");
1219     tree->Branch("perr",clevel2.perr,"perr[4]/I");
1220     tree->Branch("swerr",clevel2.swerr,"swerr[4]/I");
1221     tree->Branch("crc",clevel2.crc,"crc[4]/I");
1222     tree->Branch("nstrip",&clevel2.nstrip,"nstrip/F");
1223     tree->Branch("qtot",&clevel2.qtot,"qtot/F");
1224     tree->Branch("ncore",&clevel2.ncore,"ncore/F");
1225     tree->Branch("qcore",&clevel2.qcore,"qcore/F");
1226     tree->Branch("impx",&clevel2.impx,"impx/F");
1227     tree->Branch("impy",&clevel2.impy,"impy/F");
1228     tree->Branch("tanx",&clevel2.tanx,"tanx/F");
1229     tree->Branch("tany",&clevel2.tany,"tany/F");
1230     tree->Branch("nint",&clevel2.nint,"nint/F");
1231     tree->Branch("ncyl",&clevel2.ncyl,"ncyl/F");
1232     tree->Branch("qcyl",&clevel2.qcyl,"qcyl/F");
1233     tree->Branch("qtrack",&clevel2.qtrack,"qtrack/F");
1234     tree->Branch("qmax",&clevel2.qmax,"qmax/F");
1235     tree->Branch("nx22",&clevel2.nx22,"nx22/F");
1236     tree->Branch("qx22",&clevel2.nx22,"qx22/F");
1237     tree->Branch("qq",clevel2.qq,"qq[4]/F");
1238     tree->Branch("qtrackx",&clevel2.qtrackx,"qtrackx/F");
1239     tree->Branch("qtracky",&clevel2.qtracky,"qtracky/F");
1240     tree->Branch("dxtrack",&clevel2.dxtrack,"dxtrack/F");
1241     tree->Branch("dytrack",&clevel2.dytrack,"dytrack/F");
1242     tree->Branch("qlast",&clevel2.qlast,"qlast/F");
1243     tree->Branch("nlast",&clevel2.nlast,"nlast/F");
1244     tree->Branch("qpre",&clevel2.qpre,"qpre/F");
1245     tree->Branch("npre",&clevel2.npre,"npre/F");
1246     tree->Branch("qpresh",&clevel2.qpresh,"qpresh/F");
1247     tree->Branch("npresh",&clevel2.npresh,"npresh/F");
1248     tree->Branch("qlow",&clevel2.qlow,"qlow/F");
1249     tree->Branch("nlow",&clevel2.nlow,"nlow/F");
1250     tree->Branch("qtr",&clevel2.qtr,"qtr/F");
1251     tree->Branch("ntr",&clevel2.ntr,"ntr/F");
1252     tree->Branch("planetot",&clevel2.planetot,"planetot/F");
1253     tree->Branch("qmean",&clevel2.qmean,"qmean/F");
1254     tree->Branch("varcfit",clevel2.varcfit,"varcfit[2]/F");
1255     tree->Branch("npcfit",clevel2.npcfit,"npcfit[2]/I");
1256     tree->Branch("thex",&clevel2.thex,"thex/F");
1257     tree->Branch("they",&clevel2.they,"they/F");
1258     tree->Branch("cibar",clevel2.cibar,"cibar[22][2]/I");
1259     tree->Branch("tibar",clevel2.tibar,"tibar[22][2]/I");
1260     tree->Branch("cbar",clevel2.cbar,"cbar[22][2]/F");
1261     tree->Branch("tbar",clevel2.tbar,"tbar[22][2]/F");
1262     //
1263     TTree *software = 0;
1264     software = new TTree("Software","Software used to generate data");
1265     software->Branch("swcode",&swcode,"swcode/I");
1266     software->Branch("swtrkcode",&swtrkcode,"swtrkcode/I");
1267     software->Fill();
1268     };
1269     //
1270     // run over all the events
1271     //
1272     printf("\n Processed events: \n\n");
1273     //
1274     pamela::PscuHeader *ph = 0;
1275     pamela::EventHeader *eh = 0;
1276     Int_t caloerr = 0;
1277     Int_t i = -1;
1278     Int_t itr;
1279     if ( isrootple ){
1280     itr = -1;
1281     } else {
1282     itr = 0;
1283     };
1284     Int_t retval = 0;
1285     Int_t syncro = 1;
1286     Int_t trklost = 0;
1287     Int_t calolost = 0;
1288     Int_t pktnum = 0;
1289     Int_t obt = 0;
1290     //
1291     //
1292     while ( i < (nevents-1) ){
1293     //
1294     if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);
1295     // printf(" %i \n",i);
1296     //
1297     // look for tracker data
1298     //
1299     itr++;
1300     i++;
1301     syncro = 1;
1302     #if !defined (__CINT__)
1303     trkcalosync:
1304     #endif
1305     #if defined (__CINT__)
1306     trkcalosync: printf("");
1307     #endif
1308     //
1309     // check if we have tracker data
1310 mocchiut 1.1 //
1311 mocchiut 1.3 if ( i >= nevents ){
1312     printf(" WARNING: no more calorimeter data.\n");
1313     if ( nevents > trnevents ) calolost += (snevents-trnevents);
1314     if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1315     retval = 9;
1316     goto closeandexit;
1317     };
1318     if ( itr > trnevents && isrootple ){
1319     printf(" WARNING: no more tracker data.\n");
1320     if ( nevents > trnevents ) calolost += (snevents-trnevents);
1321     if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1322     retval = 9;
1323     goto closeandexit;
1324     };
1325     if ( itr >= trnevents+1 && !isrootple ){
1326     printf(" WARNING: no more tracker data.\n");
1327     if ( nevents > trnevents ) calolost += (snevents-trnevents);
1328     if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1329     retval = 9;
1330     goto closeandexit;
1331     };
1332 mocchiut 1.1 //
1333 mocchiut 1.3 // retrieve tracker informations
1334 mocchiut 1.1 //
1335     if ( isrootple ){
1336 mocchiut 1.3 if ( debug ) printf(" itr %i i %i nevents %i trnevents %i \n",itr,i,(int)nevents,trnevents);
1337     tr->GetEntry(itr);
1338 mocchiut 1.1 } else {
1339 mocchiut 1.3 if ( debug ) printf(" itr %i i %i nevents %i trnevents %i \n",itr,i,(int)nevents,trnevents);
1340     cretrklev2(itr,trk);
1341 mocchiut 1.1 };
1342     //
1343 mocchiut 1.3 // check synchronization tracker and calorimeter informations:
1344     //
1345     otr->SetBranchAddress("Header", &eh);
1346     otr->GetEntry(i);
1347     ph = eh->GetPscuHeader();
1348     pktnum = ph->GetCounter();
1349     obt = ph->GetOrbitalTime();
1350     if ( pktnum != trk.pkt_num || obt != trk.obt ){
1351     if ( pktnum > trk.pkt_num || obt > trk.obt ){
1352     if ( debug ) printf("itr++ %i pktnum calo %i trk %i obt calo %i trk %i \n",itr,pktnum,trk.pkt_num,obt,trk.obt);
1353 mocchiut 1.1 itr++;
1354 mocchiut 1.3 trklost++;
1355     if ( syncro ) printf(" WARNING: lost sync! try to recover... \n");
1356     syncro = 0;
1357     goto trkcalosync;
1358     };
1359     if ( pktnum < trk.pkt_num || obt < trk.obt ){
1360     if ( debug ) printf("i++ %i pktnum calo %i trk %i obt calo %i trk %i \n",i,pktnum,trk.pkt_num,obt,trk.obt);
1361 mocchiut 1.1 i++;
1362 mocchiut 1.3 calolost++;
1363     if ( syncro ) printf(" WARNING: lost sync! try to recover... \n");
1364     syncro = 0;
1365     goto trkcalosync;
1366     };
1367     };
1368     //
1369     // here we have synchronized data
1370     //
1371     if ( !syncro ) {
1372     printf(" ...synchronization recovered! \n");
1373     printf(" Sync info: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n\n",trklost,calolost);
1374     syncro = 1;
1375     if ( debug ) printf("pktnum calo %i trk %i obt calo %i trk %i \n",pktnum,trk.pkt_num,obt,trk.obt);
1376     };
1377     //
1378     // store track information in the calib structure
1379     //
1380     if ( trk.ntrk > 0 ){
1381     imtrack = 0;
1382     for (Int_t e = 0; e<trk.ntrk ; e++){
1383     if ( trk.image[e] != 0 ) imtrack++;
1384     };
1385     if ( (trk.ntrk-(imtrack/2)) == 1 ){
1386     calib.good2 = trk.good2;
1387     for (Int_t e = 0; e<5 ; e++){
1388     calib.al_p[e][0] = trk.al[0][e];
1389     if ( imtrack != 0 ) calib.al_p[e][1] = trk.al[1][e];
1390 mocchiut 1.1 };
1391 mocchiut 1.3 if ( imtrack != 0 ) {
1392     if ( trk.chi2[0]>trk.chi2[1] ) {
1393     calib.trkchi2 = 1;
1394 mocchiut 1.1 } else {
1395 mocchiut 1.3 calib.trkchi2 = 2;
1396 mocchiut 1.1 };
1397     } else {
1398 mocchiut 1.3 calib.trkchi2 = 1;
1399 mocchiut 1.1 };
1400 mocchiut 1.3 } else {
1401     calib.good2 = false;
1402     for (Int_t e = 0; e<5 ; e++){
1403     calib.al_p[e][0] = 0.;
1404     calib.al_p[e][1] = 0.;
1405     };
1406     };
1407     } else {
1408     calib.good2 = false;
1409     for (Int_t e = 0; e<5 ; e++){
1410     calib.al_p[e][0] = 0.;
1411     calib.al_p[e][1] = 0.;
1412     };
1413 mocchiut 1.1 };
1414     //
1415 mocchiut 1.3 // calibrate calorimeter data and retrieve level2 informations
1416     //
1417     // ====> NOTICE: in the case of no tracks or not good tracker events the program will fill the ntuple with zero values; <====
1418     // ====> in case of multiple tracks the program will calculate variables using the state vector and rigidity of the first track stored <====
1419     //
1420     caloerr = calolevel2core(i,b,otr,tree,clevel2,evento,calib,calcalibfile);
1421     //
1422     //
1423 mocchiut 1.1 //
1424 mocchiut 1.3 if ( caloerr ){
1425     if ( i > 0 ){
1426     printf("\nTrying to close the file anyway...\n");
1427     if ( paw ){
1428     name.str("");
1429     name << tmpname;
1430     cclosecalol2((char *)name.str().c_str());
1431     } else {
1432     hfile->Write();
1433     hfile->Close();
1434     };
1435     printf("...done!\n");
1436     } else {
1437     printf("\nERROR: output file is empty! \n");
1438     stringstream remfile;
1439     remfile.str("");
1440     remfile << "rm -f " << file2.str().c_str();
1441     gSystem->Exec(remfile.str().c_str());
1442     };
1443     printf("\nERROR: processed %i out of %i events\n",i,(int)nevents);
1444     printf("\nERROR: an error occurred during processing!\n\n Exiting...\n\n");
1445     goto theend;
1446     };
1447     Int_t ciccio;
1448     ciccio = 1;
1449     };
1450     if ( (i+1) < trnevents ){
1451     printf(" WARNING: no more calorimeter data. \n");
1452     if ( nevents > trnevents ) calolost += (snevents-trnevents);
1453     if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1454     printf(" Sync info: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n",trklost,calolost);
1455     retval = 9;
1456     };
1457     //
1458     // close rootple/ntuple files
1459     //
1460 mocchiut 1.1 closeandexit:
1461 mocchiut 1.3 if ( paw ){
1462     name.str("");
1463     name << tmpname;
1464     cclosecalol2((char *)name.str().c_str());
1465     } else {
1466     hfile->Write();
1467     hfile->Close();
1468     };
1469     if ( isrootple ){
1470     trfile->Close();
1471     } else {
1472     ccltrklev2(trk);
1473     };
1474     printf("\n");
1475     printf(" Finished, exiting... \n");
1476     printf("\n");
1477     if ( trklost || calolost ){
1478     printf("\n Sync info over the whole file: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n",trklost,calolost);
1479     };
1480     printf("\n");
1481 mocchiut 1.1 theend:
1482 mocchiut 1.3 //
1483     // go back to the starting path and unload fortran libraries
1484     //
1485     gSystem->ChangeDirectory(path);
1486 mocchiut 1.1 #if defined (__CINT__)
1487 mocchiut 1.3 if ( !isrootple ){
1488     const char *sdir=gSystem->Getenv("PAM_LIB");
1489     bdir.str("");
1490     bdir << sdir << "/libcltrklev2_C.so";
1491     gSystem->Unload(bdir.str().c_str());
1492     bdir.str("");
1493     bdir << sdir << "/libcltrklev2.so";
1494     gSystem->Unload(bdir.str().c_str());
1495     bdir.str("");
1496     bdir << sdir << "/libretrklev2_C.so";
1497     gSystem->Unload(bdir.str().c_str());
1498     bdir.str("");
1499     bdir << sdir << "/libretrklev2.so";
1500     gSystem->Unload(bdir.str().c_str());
1501     bdir.str("");
1502     bdir << sdir << "/liboptrklev2_C.so";
1503     gSystem->Unload(bdir.str().c_str());
1504     bdir.str("");
1505     bdir << sdir << "/liboptrklev2.so";
1506     gSystem->Unload(bdir.str().c_str());
1507     };
1508     // unloadf77lib(Framework,MySQL);
1509 mocchiut 1.1 #endif
1510 mocchiut 1.3 return(retval);
1511 mocchiut 1.1 }
1512    

  ViewVC Help
Powered by ViewVC 1.1.23