/[PAMELA software]/DarthVader/CalorimeterLevel2/src/CaloLevel0.cpp
ViewVC logotype

Annotation of /DarthVader/CalorimeterLevel2/src/CaloLevel0.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Tue May 22 06:56:12 2007 UTC (17 years, 6 months ago) by mocchiut
Branch: MAIN
Changes since 1.3: +1 -0 lines
AGH ADC to MIP calorimeter parameter file is wrong, fixed

1 mocchiut 1.1 /**
2     * \file src/CaloLevel0.cpp
3     * \author Emiliano Mocchiutti
4     **/
5     //
6     // C/C++ headers
7     //
8     #include <sstream>
9     #include <fstream>
10     //
11     // ROOT headers
12     //
13     #include <TTree.h>
14     #include <TBranch.h>
15     #include <TFile.h>
16     #include <TObject.h>
17     //
18     // YODA headers
19     //
20     #include <PamelaRun.h>
21     #include <physics/calorimeter/CalorimeterEvent.h>
22     #include <CalibCalPedEvent.h>
23     //
24     //
25     //
26     #include <GLTables.h>
27     //
28     // this package headers
29     //
30     #include <delay.h>
31     #include <CaloLevel0.h>
32     //
33     //
34     // Declaration of the core fortran routines
35     //
36     #define calol2cm calol2cm_
37     extern "C" int calol2cm();
38     #define calol2tr calol2tr_
39     extern "C" int calol2tr();
40     //
41     using namespace std;
42     //
43     //
44     // Public methods
45     //
46    
47     CaloLevel0::~CaloLevel0(){
48     if ( de ) delete de;
49     delete this;
50     }
51    
52     CaloLevel0::CaloLevel0(){
53     //
54     extern struct FlCaLevel1 clevel1_;
55     extern struct FlCaLevel2 clevel2_;
56     clevel1 = &clevel1_;
57     clevel2 = &clevel2_;
58     //
59     trkseqno = 0;
60     ClearStructs();
61     //
62     memset(dexy, 0, 2*22*96*sizeof(Float_t));
63     memset(dexyc, 0, 2*22*96*sizeof(Float_t));
64     memset(mip, 0, 2*22*96*sizeof(Float_t));
65     memset(base, 0, 2*22*6*sizeof(Float_t));
66     memset(sbase, 0, 2*22*6*sizeof(Float_t));
67     calopar1 = true;
68     calopar2 = true;
69     calopar3 = true;
70     crosst = true;
71     ftcalopar1 = 0;
72     ttcalopar1 = 0;
73     ftcalopar2 = 0;
74     ttcalopar2 = 0;
75     ftcalopar3 = 0;
76     ttcalopar3 = 0;
77     }
78    
79     void CaloLevel0::SetCrossTalk(Bool_t ct){
80     crosst = ct;
81     };
82    
83     void CaloLevel0::SetVerbose(Bool_t ct){
84     verbose = ct;
85     };
86    
87     /**
88     * Initialize CaloLevel0 object
89     **/
90     void CaloLevel0::ProcessingInit(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
91     //
92     debug = isdeb;
93     verbose = isverb;
94     //
95     l0tr=(TTree*)l0tree;
96     de = new pamela::calorimeter::CalorimeterEvent();
97     l0calo = (TBranch*)l0tr->GetBranch("Calorimeter");
98     l0tr->SetBranchAddress("Calorimeter", &de);
99     //
100     trkseqno = 0;
101     ClearStructs();
102     //
103     GL_CALO_CALIB *glcalo = new GL_CALO_CALIB();
104     //
105     sgnl = 0;
106     UInt_t uptime = 0;
107     //
108     for (Int_t s = 0; s < 4; s++){
109     idcalib[s] = 0;
110     fromtime[s] = 0;
111     totime[s] = 0;
112     calibno[s] = 0;
113     ClearCalibVals(s);
114     //
115     sgnl = glcalo->Query_GL_CALO_CALIB(hs,uptime,s,dbc);
116     if ( sgnl < 0 ){
117     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
118     return;
119     };
120     //
121     idcalib[s] = glcalo->ID_ROOT_L0;
122     fromtime[s] = glcalo->FROM_TIME;
123     if ( glcalo->TO_TIME < hs ){ // calibration is corrupted and we are using the one that preceed the good one
124     totime[s] = uptime;
125     } else {
126     totime[s] = glcalo->TO_TIME;
127     };
128     calibno[s] = glcalo->EV_ROOT;
129     //
130     if ( totime[s] == 0 ){
131     if ( verbose ) printf(" CALORIMETER - WARNING: data with no associated calibration\n");
132     ClearCalibVals(s);
133     sgnl = 100;
134     };
135     };
136     //
137     // determine path and name and entry of the calibration file
138     //
139     GL_ROOT *glroot = new GL_ROOT();
140     if ( verbose ) printf("\n");
141     for (Int_t s = 0; s < 4; s++){
142     if ( verbose ) printf(" ** SECTION %i **\n",s);
143     if ( totime[s] > 0 ){
144     //
145     sgnl = glroot->Query_GL_ROOT(idcalib[s],dbc);
146     if ( sgnl < 0 ){
147     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
148     return;
149     };
150     //
151     stringstream name;
152     name.str("");
153     name << glroot->PATH.Data() << "/";
154     name << glroot->NAME.Data();
155     //
156     fcalname[s] = (TString)name.str().c_str();
157     if ( verbose ) printf(" - runheader at time %u. From time %u to time %u \n use file %s \n calibration at entry %i \n\n",hs,fromtime[s],totime[s],fcalname[s].Data(),calibno[s]);
158     } else {
159     if ( verbose ) printf(" - runheader at time %u. NO CALIBRATION INCLUDE THE RUNHEADER! ",hs);
160     };
161     sgnl = LoadCalib(s);
162     if ( sgnl ) break;
163     };
164     //
165     delete glcalo;
166     delete glroot;
167     //
168     return;
169     //
170     }
171    
172     Int_t CaloLevel0::ChkCalib(TSQLServer *dbc, UInt_t atime){
173     Int_t sgnl = 0;
174     for ( Int_t s = 0; s < 4; s++){
175     if ( atime > totime[s] ){
176     if ( !dbc->IsConnected() ) throw -116;
177     sgnl = Update(dbc,atime,s);
178     if ( sgnl < 0 ) return(sgnl);
179     };
180     };
181     return(sgnl);
182     }
183    
184 mocchiut 1.2 Int_t CaloLevel0::ChkParam(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){
185 mocchiut 1.1 stringstream calfile;
186     stringstream bmfile;
187     stringstream aligfile;
188     Int_t error = 0;
189     FILE *f = 0;
190     ifstream badfile;
191     GL_PARAM *glparam = new GL_PARAM();
192     //
193     if ( calopar1 || ( ttcalopar1 != 0 && ttcalopar1 < runheader ) ){
194 mocchiut 1.2 //
195     //
196     //
197     if ( debug ) printf(" calopar1 %i ftcalopar1 %u ttcalopar1 %u runheader %u \n",calopar1,ftcalopar1,ttcalopar1,runheader);
198     //
199 mocchiut 1.1 calopar1 = false;
200     //
201     // determine where I can find calorimeter ADC to MIP conversion file
202     //
203     if ( verbose ) printf(" Querying DB for calorimeter parameters files...\n");
204     //
205     error = 0;
206     error = glparam->Query_GL_PARAM(runheader,101,dbc);
207     if ( error < 0 ) return(error);
208     //
209     calfile.str("");
210     calfile << glparam->PATH.Data() << "/";
211     calfile << glparam->NAME.Data();
212     ftcalopar1 = glparam->FROM_TIME;
213     ttcalopar1 = glparam->TO_TIME;
214     //
215     if ( verbose ) printf("\n Using ADC to MIP conversion file: \n %s \n",calfile.str().c_str());
216     f = fopen(calfile.str().c_str(),"rb");
217     if ( !f ){
218     if ( verbose ) printf(" CALORIMETER - ERROR: no ADC to MIP file!\n");
219     return(-105);
220     };
221     //
222     for (Int_t m = 0; m < 2 ; m++ ){
223     for (Int_t k = 0; k < 22; k++ ){
224     for (Int_t l = 0; l < 96; l++ ){
225     fread(&mip[m][k][l],sizeof(mip[m][k][l]),1,f);
226 mocchiut 1.4 if ( verbose ) printf(" %f \n",mip[m][k][l]);
227 mocchiut 1.1 };
228     };
229     };
230     fclose(f);
231     };
232     //
233     if ( calopar2 || ( ttcalopar2 != 0 && ttcalopar2 < runheader ) ){
234 mocchiut 1.2 if ( debug ) printf(" calopar2 %i ftcalopar2 %u ttcalopar2 %u runheader %u \n",calopar2,ftcalopar2,ttcalopar2,runheader);
235 mocchiut 1.1 calopar2 = false;
236     //
237     // determine where I can find calorimeter alignment file
238     //
239     //
240     error = 0;
241     error = glparam->Query_GL_PARAM(runheader,102,dbc);
242     if ( error < 0 ) return(error);
243     //
244     aligfile.str("");
245     aligfile << glparam->PATH.Data() << "/";
246     aligfile << glparam->NAME.Data();
247     ftcalopar2 = glparam->FROM_TIME;
248     ttcalopar2 = glparam->TO_TIME;
249     //
250 mocchiut 1.2 if ( verbose ) printf("\n Using parameter file: \n %s \n",aligfile.str().c_str());
251 mocchiut 1.1 f = fopen(aligfile.str().c_str(),"rb");
252     if ( !f ){
253 mocchiut 1.2 if ( verbose ) printf(" CALORIMETER - ERROR: no parameter file!\n");
254 mocchiut 1.1 return(-106);
255     };
256     //
257 mocchiut 1.2 if ( !mechal ){
258     //
259     fread(&clevel1->xalig,sizeof(clevel1->xalig),1,f);
260     if ( debug ) printf(" xalig = %f \n",clevel1->xalig);
261     fread(&clevel1->yalig,sizeof(clevel1->yalig),1,f);
262     if ( debug ) printf(" yalig = %f \n",clevel1->yalig);
263     fread(&clevel1->zalig,sizeof(clevel1->zalig),1,f);
264     if ( debug ) printf(" zalig = %f \n",clevel1->zalig);
265     } else {
266     if ( verbose ) printf("\n Using MECHANICAL alignement parameters \n");
267     //
268     CaloStrip cs = CaloStrip();
269     cs.UseMechanicalAlig();
270     clevel1->xalig = cs.GetXalig();
271     if ( debug ) printf(" xalig = %f \n",clevel1->xalig);
272     clevel1->yalig = cs.GetYalig();
273     if ( debug ) printf(" yalig = %f \n",clevel1->yalig);
274     clevel1->zalig = cs.GetZalig();
275     if ( debug ) printf(" zalig = %f \n",clevel1->zalig);
276     //
277     Float_t tmp = 0;
278     fread(&tmp,sizeof(clevel1->xalig),1,f);
279     fread(&tmp,sizeof(clevel1->yalig),1,f);
280     fread(&tmp,sizeof(clevel1->zalig),1,f);
281     //
282     };
283 mocchiut 1.1 fread(&clevel1->emin,sizeof(clevel1->emin),1,f);
284     if ( debug ) printf(" signal threshold = %f \n",clevel1->emin);
285     //
286     fclose(f);
287     };
288     //
289     // Load offline bad strip mask
290     //
291     if ( calopar3 || ( ttcalopar3 != 0 && ttcalopar3 < runheader ) ){
292 mocchiut 1.2 if ( debug ) printf(" calopar3 %i ftcalopar3 %u ttcalopar3 %u runheader %u \n",calopar3,ftcalopar3,ttcalopar3,runheader);
293 mocchiut 1.1 calopar3 = false;
294     //
295     // determine where I can find calorimeter alignment file
296     //
297     //
298     error = 0;
299     error = glparam->Query_GL_PARAM(runheader,103,dbc);
300     if ( error < 0 ) return(error);
301     //
302     bmfile.str("");
303     bmfile << glparam->PATH.Data() << "/";
304     bmfile << glparam->NAME.Data();
305     ftcalopar3 = glparam->FROM_TIME;
306     ttcalopar3 = glparam->TO_TIME;
307     //
308     if ( verbose ) printf("\n Using bad strip offline mask file: \n %s \n\n",bmfile.str().c_str());
309     badfile.open(bmfile.str().c_str());
310     if ( !badfile ){
311     if ( verbose ) printf(" CALORIMETER - ERROR: no bad strip offline mask file!\n");
312     return(-115);
313     };
314     //
315     Bool_t isdone = false;
316     Int_t bad = 0;
317     Int_t view = 1;
318     Int_t strip = 0;
319     Int_t plane = 21;
320     while ( !isdone ) {
321     badfile >> bad;
322     obadmask[view][plane][strip] = bad;
323     if ( debug && bad ) printf(" SETTING view %i plane %i strip %i BAD = %i \n",view,plane,strip,bad);
324     strip++;
325     if ( strip > 95 ){
326     strip = 0;
327     plane--;
328     if ( plane < 0 ){
329     plane = 21;
330     view--;
331     };
332     if ( view < 0 ) isdone = true;
333     };
334     };
335     //
336     badfile.close();
337     };
338     //
339     delete glparam;
340     //
341     return(0);
342     }
343    
344    
345    
346     void CaloLevel0::FindBaseRaw(Int_t l, Int_t m, Int_t pre){
347     Float_t minstrip = 100000.;
348     Float_t rms = 0.;
349     base[l][m][pre] = 0.;
350     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
351     if ( calgood[l][m][e] == 0. && obadmask[l][m][e] == 0 && dexy[l][m][e]-calped[l][m][e] < minstrip && dexy[l][m][e] > 0.) {
352     minstrip = dexy[l][m][e]-calped[l][m][e];
353     rms = calthr[l][m][pre];
354     };
355     };
356     if ( minstrip != 100000. ) {
357     Float_t strip6s = 0.;
358     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
359     if ( (dexy[l][m][e]-calped[l][m][e]) >= minstrip && (dexy[l][m][e]-calped[l][m][e]) <= (minstrip+rms) ) {
360     strip6s += 1.;
361     base[l][m][pre] += (dexy[l][m][e] - calped[l][m][e]);
362     };
363     //
364     // compression
365     //
366     if ( abs((int)(dexy[l][m][e]-calped[l][m][e])) <= (minstrip+rms) ) {
367     dexyc[l][m][e] = 0.;
368     } else {
369     dexyc[l][m][e] = dexy[l][m][e];
370     };
371     };
372     if ( strip6s >= 9. ){
373     Double_t arro = base[l][m][pre]/strip6s;
374     Float_t deci = 1000.*((float)arro - float(int(arro)));
375     if ( deci < 500. ) {
376     arro = double(int(arro));
377     } else {
378     arro = 1. + double(int(arro));
379     };
380     base[l][m][pre] = arro;
381     } else {
382     base[l][m][pre] = 31000.;
383     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
384     dexyc[l][m][e] = dexy[l][m][e];
385     };
386     };
387     } else {
388     base[l][m][pre] = 31000.;
389     };
390     }
391    
392     Int_t CaloLevel0::Calibrate(Int_t ei){
393     //
394     // get entry ei
395     //
396     l0calo->GetEntry(ei);
397     //
398     // if it was not a selftrigger event, could it ever been a selftrigger event? if so trigty = 3.
399     //
400     Int_t val = 0;
401     Int_t del = 1100;
402     if ( clevel2->trigty != 2. ){
403     Bool_t ck = false;
404     for (Int_t sec = 0; sec < 4; sec++){
405     val = (Int_t)de->calselftrig[sec][6];
406     del = delay(val);
407     if ( del < 1100 ){
408     clevel2->wartrig = 0.;
409     clevel2->trigty = 3.;
410     ck = true;
411     break;
412     };
413     };
414     if ( !ck ) clevel2->wartrig = 100.;
415     } else {
416     Bool_t ck = false;
417     for (Int_t sec = 0; sec < 4; sec++){
418     val = (Int_t)de->calselftrig[sec][6];
419     del = delay(val);
420     if ( del < 1100 ){
421     clevel2->wartrig = 0.;
422     ck = true;
423     };
424     };
425     if ( !ck ) clevel2->wartrig = 100.;
426     };
427     //
428     Int_t se = 5;
429     Int_t done = 0;
430     Int_t pre = -1;
431     Bool_t isCOMP = false;
432     Bool_t isFULL = false;
433     Bool_t isRAW = false;
434     Float_t ener;
435     Int_t doneb = 0;
436     Int_t donec = 0;
437     Int_t ck = 0;
438     Int_t ipre = 0;
439     Int_t ip[3] = {0};
440     Float_t base0, base1, base2;
441     base0 = 0.;
442     base1 = 0.;
443     base2 = 0.;
444     Float_t qpre[6] = {0.,0.,0.,0.,0.,0.};
445     Float_t ene[96];
446     Int_t chdone[4] = {0,0,0,0};
447     Int_t pe = 0;
448     //
449     Float_t ener0 = 0.;
450     Float_t cbase0 = 0.;
451     Bool_t pproblem = false;
452     //
453     Float_t tim = 0.;
454     Int_t plo = 0;
455     Int_t fbi = 0;
456     Int_t cle = 0;
457     //
458     // run over views and planes
459     //
460     for (Int_t l = 0; l < 2; l++){
461     for (Int_t m = 0; m < 22; m++){
462     //
463 mocchiut 1.2 // determine the section number
464 mocchiut 1.1 //
465     se = 5;
466 mocchiut 1.2 if (l == 0 && m%2 == 0) se = 3;
467 mocchiut 1.1 if (l == 0 && m%2 != 0) se = 2;
468 mocchiut 1.2 if (l == 1 && m%2 != 0) se = 1;
469     if (l == 1 && m%2 == 0) se = 0;
470 mocchiut 1.1 //
471     // determine what kind of event we are going to analyze
472     //
473     isCOMP = false;
474     isFULL = false;
475     isRAW = false;
476     if ( de->stwerr[se] & (1 << 16) ) isCOMP = true;
477     if ( de->stwerr[se] & (1 << 17) ) isFULL = true;
478     if ( de->stwerr[se] & (1 << 3) ) isRAW = true;
479     if ( !chdone[se] ){
480     //
481     // check for any error in the event
482     //
483     clevel2->crc[se] = 0;
484     if ( de->perror[se] == 132 ){
485     clevel2->crc[se] = 1;
486     pe++;
487     };
488     clevel2->perr[se] = 0;
489     if ( de->perror[se] != 0 ){
490     clevel2->perr[se] = 1;
491     pe++;
492     };
493     clevel2->swerr[se] = 0;
494     for (Int_t j = 0; j < 7 ; j++){
495     if ( (j != 3) && (de->stwerr[se] & (1 << j)) ){
496     clevel2->swerr[se] = 1;
497     pe++;
498     };
499     };
500     chdone[se] = 1;
501     };
502     if ( clevel2->crc[se] == 0 && (clevel1->good2 == 1 || clevel2->trigty >= 2) ){
503     pre = -1;
504     //
505     for (Int_t nn = 0; nn < 96; nn++){
506     ene[nn] = 0.;
507     dexy[l][m][nn] = de->dexy[l][m][nn] ;
508     dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
509     };
510     //
511     // run over preamplifiers
512     //
513     pre = -1;
514     cbase0 = 0.;
515     for (Int_t i = 0; i < 3; i++){
516     for (Int_t j = 0; j < 2; j++){
517     pre = j + i*2;
518     //
519     // baseline check and calculation
520     //
521     if ( !isRAW ) {
522     base[l][m][pre] = de->base[l][m][pre] ;
523     cbase0 += base[l][m][pre];
524     } else {
525     //
526     // if it is a raw event and we haven't checked
527     // yet, calculate the baseline.
528     //
529     FindBaseRaw(l,m,pre);
530     cbase0 += base[l][m][pre];
531     };
532     };
533     };
534     //
535     // run over strips
536     //
537     pre = -1;
538     ener0 = 0.;
539     for (Int_t i = 0 ; i < 3 ; i++){
540     ip[i] = 0;
541     for (Int_t n = i*32 ; n < (i+1)*32 ; n++){
542     if (n%16 == 0) {
543     ck = 0;
544     done = 0;
545     doneb = 0;
546     donec = 0;
547     pre++;
548     qpre[pre] = 0.;
549     };
550     //
551     // baseline check and calculation
552     //
553     // no suitable new baseline, use old ones!
554     //
555     if ( !done ){
556     if ( (base[l][m][pre] == 31000. || base[l][m][pre] == 0.) ){
557     ck = 1;
558     if (pre%2 == 0) {
559     ip[i] = pre + 1;
560     } else {
561     ip[i] = pre - 1;
562     };
563 mocchiut 1.3 if ( (base[l][m][ip[i]] == 31000. || base[l][m][ip[i]] == 0. || !crosst ) ){
564 mocchiut 1.1 //
565     ck = 2;
566     if ( sbase[l][m][pre] == 31000. || sbase[l][m][pre] == 0. ) {
567     ck = 3;
568     };
569     };
570     done = 1;
571     };
572     };
573     //
574     // CALIBRATION ALGORITHM
575     //
576     if ( !doneb ){
577 mocchiut 1.3 if ( debug ) printf(" ck is %i \n",ck);
578 mocchiut 1.1 switch (ck) {
579     case 0:
580     base0 = base[l][m][pre];
581     base2 = calbase[l][m][pre];
582 mocchiut 1.3 if ( debug ) printf(" base0 = base l m pre = %f base2 = calbase l m pre = %f \n",base[l][m][pre],calbase[l][m][pre]);
583 mocchiut 1.1 break;
584     case 1:
585     base0 = base[l][m][ip[i]];
586     base2 = calbase[l][m][ip[i]];
587 mocchiut 1.3 if ( debug ) printf(" base0 = base l m ip(i) = %f base2 = calbase l m ip(i) = %f \n",base[l][m][ip[i]],calbase[l][m][ip[i]]);
588 mocchiut 1.1 break;
589     case 2:
590     base0 = sbase[l][m][pre];
591 mocchiut 1.3 base2 = calbase[l][m][pre];
592     if ( debug ) printf(" base0 = sbase l m pre = %f base2 = calbase l m pre = %f \n",sbase[l][m][pre],calbase[l][m][pre]);
593 mocchiut 1.1 break;
594     case 3:
595     base0 = calbase[l][m][pre];
596     base2 = calbase[l][m][pre];
597 mocchiut 1.3 if ( debug ) printf(" base0 = calbase l m pre = %f base2 = calbase l m pre = %f \n",calbase[l][m][pre],calbase[l][m][pre]);
598 mocchiut 1.1 break;
599     };
600     base1 = calbase[l][m][pre];
601     doneb = 1;
602     };
603     ener = dexyc[l][m][n];
604     ener0 += ener;
605     clevel1->estrip[n][m][l] = 0.;
606     if ( base0>0 && base0 < 30000. ){
607 mocchiut 1.3 // if ( !donec && (base0 - base1 + base2) != 0. ){
608     // sbase[l][m][pre] = base0 - base1 + base2;
609     if ( !donec && (base0 + base1 - base2) != 0. ){
610     sbase[l][m][pre] = base0 + base1 - base2;
611 mocchiut 1.1 donec = 1;
612     };
613     if ( ener > 0. ){
614     clevel1->estrip[n][m][l] = (ener - calped[l][m][n] - base0 - base1 + base2)/mip[l][m][n] ;
615     //
616     // 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)
617     //
618     qpre[pre] += clevel1->estrip[n][m][l];
619 mocchiut 1.3 //
620     //
621 mocchiut 1.1 };
622     };
623     };
624     if ( crosst ){
625     if (ck == 1){
626     if (ip[i]%2 == 0) {
627     ipre = ip[i] + 1;
628     } else {
629     ipre = ip[i] - 1;
630     };
631     for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
632     clevel1->estrip[j][m][l] += (qpre[ipre] - qpre[ip[i]]) * 0.00478;
633     };
634     };
635     if (ck == 2){
636     for (Int_t j = i*32 ; j < (i+1)*32 ; j++){
637     ipre = j/16 + 1;
638     clevel1->estrip[j][m][l] += qpre[ipre] * 0.00478;
639     };
640     };
641     };
642     };
643     //
644     if ( ener0 == 0. && cbase0 == 0. && !pproblem && clevel2->perr[se] == 0){
645     if ( verbose ) printf(" L0 entry %i : calorimeter power problems! event marked as bad perr %f swerr %X view %i plane %i \n",ei,de->perror[se],de->stwerr[se],l,m);
646     pproblem = true;
647     pe++;
648     };
649     //
650     Int_t j4 = -4;
651     Int_t jjj = -3;
652     Int_t jj = -2;
653     for (Int_t j = 0 ; j < 100 ; j++){
654     jj++;
655     jjj++;
656     j4++;
657     if ( j < 96 ) ene[j] = clevel1->estrip[j][m][l];
658     if ( crosst ){
659     if ( jj >= 0 && jj < 96 ){
660     if ( jj != 0 && jj != 32 && jj != 64 ) ene[jj-1] += -clevel1->estrip[jj][m][l] * 0.01581;
661     if ( jj != 31 && jj != 63 && jj != 95 ) ene[jj+1] += -clevel1->estrip[jj][m][l] * 0.01581;
662     };
663     if ( jjj >= 0 && jjj < 96 ){
664     if ( jjj != 0 && jjj != 32 && jjj != 64 ) clevel1->estrip[jjj-1][m][l] += -ene[jjj] * 0.01581;
665     if ( jjj != 31 && jjj != 63 && jjj != 95 ) clevel1->estrip[jjj+1][m][l] += -ene[jjj] * 0.01581;
666     };
667     };
668     if ( j4 >= 0 && j4 < 96 ){
669     //
670     // NOTICE: THE FOLLOWING LINE EXCLUDE ALL STRIPS FOR WHICH THE RMS*4 IS GREATER THAN 26 !!! <=============== IMPORTANT! =================>
671     //
672     if ( obadmask[l][m][j4] == 1 || clevel1->estrip[j4][m][l] <= clevel1->emin || calrms[l][m][j4] > 26 ){
673     clevel1->estrip[j4][m][l] = 0.;
674     };
675     //
676     // code and save the energy for each strip in svstrip
677     //
678     if ( clevel1->estrip[j4][m][l] > clevel1->emin ){
679     //
680     tim = 100000.;
681     plo = m;
682     fbi = 0;
683     if ( clevel1->estrip[j4][m][l] > 0.99995 ){
684     tim = 10000.;
685     plo = m;
686     fbi = 1;
687     };
688     if ( clevel1->estrip[j4][m][l] > 9.9995 ){
689     tim = 1000.;
690     plo = 22 + m;
691     fbi = 1;
692     };
693     if ( clevel1->estrip[j4][m][l] > 99.995 ){
694     tim = 100.;
695     plo = 22 + m;
696     fbi = 0;
697     };
698     if ( clevel1->estrip[j4][m][l] > 999.95 ){
699     tim = 10.;
700     plo = 44 + m;
701     fbi = 0;
702     };
703     if ( clevel1->estrip[j4][m][l] > 9999.5 ){
704     tim = 1.;
705     plo = 66 + m;
706     fbi = 0;
707     };
708     //
709     cle = (Int_t)lroundf(tim*clevel1->estrip[j4][m][l]);
710     //
711     if ( l == 0 ){
712     //
713     // +-PPSSmmmm.mmmm
714     //
715     svstrip[istrip] = fbi*1000000000 + plo*10000000 + j4*100000 + cle;
716     } else {
717     svstrip[istrip] = -(fbi*1000000000 + plo*10000000 + j4*100000 + cle);
718     };
719     //
720     // if ( ei >= -770 ) printf(" j %i l %i m %i estrip %f \n",j4,l,m,clevel1->estrip[j4][m][l]);
721     // if ( ei >= -770 ) printf(" num lim %i fbi %i tim %f plo %i cle %i \n",numeric_limits<Int_t>::max(),fbi,tim,plo,cle);
722     // if ( ei >= -770 ) printf(" svstrip %i \n",svstrip[istrip]);
723     //
724     istrip++;
725     };
726     };
727     };
728     //
729     } else {
730     for (Int_t nn = 0; nn < 96; nn++){
731     clevel1->estrip[nn][m][l] = 0.;
732     };
733     };
734     };
735     };
736     if ( !pe ){
737     clevel2->good = 1;
738     } else {
739     clevel2->good = 0;
740     };
741     return(0);
742     }
743    
744     void CaloLevel0::GetTrkVar(){
745     calol2tr();
746     }
747    
748     void CaloLevel0::FillTrkVar(CaloLevel2 *ca, Int_t nutrk){
749     //
750     CaloTrkVar *t_ca = new CaloTrkVar();
751     //
752     t_ca->trkseqno = trkseqno;
753     t_ca->ncore = (Int_t)clevel2->ncore;
754     t_ca->qcore = clevel2->qcore;
755     t_ca->noint = (Int_t)clevel2->noint;
756     t_ca->ncyl = (Int_t)clevel2->ncyl;
757     t_ca->qcyl = clevel2->qcyl;
758     t_ca->qtrack = clevel2->qtrack;
759     t_ca->qtrackx = clevel2->qtrackx;
760     t_ca->qtracky = clevel2->qtracky;
761     t_ca->dxtrack = clevel2->dxtrack;
762     t_ca->dytrack = clevel2->dytrack;
763     t_ca->qlast = clevel2->qlast;
764     t_ca->nlast = (Int_t)clevel2->nlast;
765     t_ca->qpre = clevel2->qpre;
766     t_ca->npre = (Int_t)clevel2->npre;
767     t_ca->qpresh = clevel2->qpresh;
768     t_ca->npresh = (Int_t)clevel2->npresh;
769     t_ca->qtr = clevel2->qtr;
770     t_ca->ntr = (Int_t)clevel2->ntr;
771     t_ca->planetot = (Int_t)clevel2->planetot;
772     t_ca->qmean = clevel2->qmean;
773     t_ca->dX0l = clevel2->dX0l;
774     t_ca->qlow = clevel2->qlow;
775     t_ca->nlow = (Int_t)clevel2->nlow;
776     //
777     if ( trkseqno == -1 ){
778     // ca->impx = clevel2->impx;
779     // ca->impy = clevel2->impy;
780     ca->tanx[1] = clevel2->tanx;
781     ca->tany[1] = clevel2->tany;
782     ca->elen = clevel2->elen;
783     ca->selen = clevel2->selen;
784     // memcpy(ca->cibar,clevel2->cibar,sizeof(clevel2->cibar));
785     // memcpy(ca->cbar,clevel2->cbar,sizeof(clevel2->cbar));
786     memcpy(t_ca->tibar,clevel2->cibar,sizeof(clevel2->cibar));
787     memcpy(t_ca->tbar,clevel2->cbar,sizeof(clevel2->cbar));
788     memcpy(ca->planemax,clevel2->planemax,sizeof(clevel2->planemax));
789     ca->varcfit[2] = clevel2->varcfit[0];
790     ca->varcfit[3] = clevel2->varcfit[1];
791     ca->npcfit[2] = clevel2->npcfit[0];
792     ca->npcfit[3] = clevel2->npcfit[1];
793     // memcpy(ca->varcfit,clevel2->varcfit,sizeof(clevel2->varcfit));
794     // memcpy(ca->npcfit,clevel2->npcfit,sizeof(clevel2->npcfit));
795     } else {
796     memcpy(t_ca->tibar,clevel2->tibar,sizeof(clevel2->tibar));
797     memcpy(t_ca->tbar,clevel2->tbar,sizeof(clevel2->tbar));
798     };
799     //
800     //
801     if ( !(ca->CaloTrk) ) ca->CaloTrk = new TClonesArray("CaloTrkVar",1); //ELENA
802     TClonesArray &t = *ca->CaloTrk;
803     new(t[nutrk]) CaloTrkVar(*t_ca);
804     //
805     delete t_ca;
806     //
807     ClearTrkVar();
808     }
809    
810     void CaloLevel0::GetCommonVar(){
811     calol2cm();
812     }
813    
814     void CaloLevel0::FillCommonVar(CaloLevel1 *c1, CaloLevel2 *ca){
815     //
816     ca->good = clevel2->good;
817     if ( clevel2->trigty == 2. ){
818     ca->selftrigger = 1;
819     } else {
820     ca->selftrigger = 0;
821     };
822     //
823     ca->selftrigger += (Int_t)clevel2->wartrig;
824     //
825     memcpy(ca->perr,clevel2->perr,sizeof(clevel2->perr));
826     memcpy(ca->swerr,clevel2->swerr,sizeof(clevel2->swerr));
827     memcpy(ca->crc,clevel2->crc,sizeof(clevel2->crc));
828     ca->nstrip = (Int_t)clevel2->nstrip;
829     ca->qtot = clevel2->qtot;
830     // ca->impx = clevel2->impx;
831     // ca->impy = clevel2->impy;
832     ca->tanx[0] = clevel2->tanx;
833     ca->tany[0] = clevel2->tany;
834     ca->nx22 = (Int_t)clevel2->nx22;
835     ca->qx22 = clevel2->qx22;
836     ca->qmax = clevel2->qmax;
837     ca->elen = clevel2->elen;
838     ca->selen = clevel2->selen;
839     memcpy(ca->qq,clevel2->qq,sizeof(clevel2->qq));
840     memcpy(ca->planemax,clevel2->planemax,sizeof(clevel2->planemax));
841     ca->varcfit[0] = clevel2->varcfit[0];
842     ca->varcfit[1] = clevel2->varcfit[1];
843     ca->npcfit[0] = clevel2->npcfit[0];
844     ca->npcfit[1] = clevel2->npcfit[1];
845     ca->fitmode[0] = clevel2->fmode[0];
846     ca->fitmode[1] = clevel2->fmode[1];
847     // memcpy(ca->varcfit,clevel2->varcfit,sizeof(clevel2->varcfit));
848     // memcpy(ca->npcfit,clevel2->npcfit,sizeof(clevel2->npcfit));
849     memcpy(ca->cibar,clevel2->cibar,sizeof(clevel2->cibar));
850     memcpy(ca->cbar,clevel2->cbar,sizeof(clevel2->cbar));
851     //
852     if ( c1 ){
853     c1->istrip = istrip;
854     c1->estrip = TArrayI(istrip,svstrip);
855     };
856     //
857     }
858    
859     void CaloLevel0::ClearStructs(){
860     ClearTrkVar();
861     ClearCommonVar();
862     }
863    
864     void CaloLevel0::RunClose(){
865     l0tr->Delete();
866     ClearStructs();
867     //
868     memset(dexy, 0, 2*22*96*sizeof(Float_t));
869     memset(dexyc, 0, 2*22*96*sizeof(Float_t));
870     memset(base, 0, 2*22*6*sizeof(Float_t));
871     memset(sbase, 0, 2*22*6*sizeof(Float_t));
872     //
873     }
874    
875     //
876     // Private methods
877     //
878    
879     void CaloLevel0::ClearTrkVar(){
880     clevel2->ncore = 0;
881     clevel2->qcore = 0.;
882     clevel2->noint = 0.;
883     clevel2->ncyl = 0.;
884     clevel2->qcyl = 0.;
885     clevel2->qtrack = 0.;
886     clevel2->qtrackx = 0.;
887     clevel2->qtracky = 0.;
888     clevel2->dxtrack = 0.;
889     clevel2->dytrack = 0.;
890     clevel2->qlast = 0.;
891     clevel2->nlast = 0.;
892     clevel2->qpre = 0.;
893     clevel2->npre = 0.;
894     clevel2->qpresh = 0.;
895     clevel2->npresh = 0.;
896     clevel2->qlow = 0.;
897     clevel2->nlow = 0.;
898     clevel2->qtr = 0.;
899     clevel2->ntr = 0.;
900     clevel2->planetot = 0.;
901     clevel2->qmean = 0.;
902     clevel2->dX0l = 0.;
903     clevel2->elen = 0.;
904     clevel2->selen = 0.;
905     memset(clevel1->al_p, 0, 5*2*sizeof(Double_t));
906     memset(clevel2->tibar, 0, 2*22*sizeof(Int_t));
907     memset(clevel2->tbar, 0, 2*22*sizeof(Float_t));
908     }
909    
910     void CaloLevel0::ClearCommonVar(){
911     istrip = 0;
912     clevel2->trigty = -1.;
913     clevel2->wartrig = 0.;
914     clevel2->good = 0;
915     clevel2->nstrip = 0.;
916     clevel2->qtot = 0.;
917     // clevel2->impx = 0.;
918     // clevel2->impy = 0.;
919     clevel2->tanx = 0.; // this is correct since it refers to the fortran structure
920     clevel2->tany = 0.; // this is correct since it refers to the fortran structure
921     clevel2->qmax = 0.;
922     clevel2->nx22 = 0.;
923     clevel2->qx22 = 0.;
924     memset(clevel2->perr, 0, 4*sizeof(Int_t));
925     memset(clevel2->swerr, 0, 4*sizeof(Int_t));
926     memset(clevel2->crc, 0, 4*sizeof(Int_t));
927     memset(clevel2->qq, 0, 4*sizeof(Int_t));
928     memset(clevel2->varcfit, 0, 4*sizeof(Float_t));
929     memset(clevel2->npcfit, 0, 4*sizeof(Int_t));
930     memset(clevel2->planemax, 0, 2*sizeof(Int_t));
931     memset(clevel2->fmode, 0, 2*sizeof(Int_t));
932     memset(clevel2->cibar, 0, 2*22*sizeof(Int_t));
933     memset(clevel2->cbar, 0, 2*22*sizeof(Float_t));
934     }
935    
936     void CaloLevel0::ClearCalibVals(Int_t s){
937     //
938     for ( Int_t d=0 ; d<11 ;d++ ){
939     Int_t pre = -1;
940     for ( Int_t j=0; j<96 ;j++){
941     if ( j%16 == 0 ) pre++;
942     if ( s == 2 ){
943     calped[0][2*d+1][j] = 0.;
944     cstwerr[3] = 0.;
945     cperror[3] = 0.;
946     calgood[0][2*d+1][j] = 0.;
947     calthr[0][2*d+1][pre] = 0.;
948     calrms[0][2*d+1][j] = 0.;
949     calbase[0][2*d+1][pre] = 0.;
950     calvar[0][2*d+1][pre] = 0.;
951     };
952     if ( s == 3 ){
953     calped[0][2*d][j] = 0.;
954     cstwerr[1] = 0.;
955     cperror[1] = 0.;
956     calgood[0][2*d][j] = 0.;
957     calthr[0][2*d][pre] = 0.;
958     calrms[0][2*d][j] = 0.;
959     calbase[0][2*d][pre] = 0.;
960     calvar[0][2*d][pre] = 0.;
961     };
962     if ( s == 0 ){
963     calped[1][2*d][j] = 0.;
964     cstwerr[0] = 0.;
965     cperror[0] = 0.;
966     calgood[1][2*d][j] = 0.;
967     calthr[1][2*d][pre] = 0.;
968     calrms[1][2*d][j] = 0.;
969     calbase[1][2*d][pre] = 0.;
970     calvar[1][2*d][pre] = 0.;
971     };
972     if ( s == 1 ){
973     calped[1][2*d+1][j] = 0.;
974     cstwerr[2] = 0.;
975     cperror[2] = 0.;
976     calgood[1][2*d+1][j] = 0.;
977     calthr[1][2*d+1][pre] = 0.;
978     calrms[1][2*d+1][j] = 0.;
979     calbase[1][2*d+1][pre] = 0.;
980     calvar[1][2*d+1][pre] = 0.;
981     };
982     };
983     };
984     return;
985     }
986    
987     Int_t CaloLevel0::Update(TSQLServer *dbc, UInt_t atime, Int_t s){
988     //
989     Int_t sgnl = 0;
990     //
991     GL_CALO_CALIB *glcalo = new GL_CALO_CALIB();
992     //
993     sgnl = 0;
994     //
995     idcalib[s] = 0;
996     fromtime[s] = 0;
997     totime[s] = 0;
998     calibno[s] = 0;
999     ClearCalibVals(s);
1000     //
1001     UInt_t uptime = 0;
1002     //
1003     sgnl = glcalo->Query_GL_CALO_CALIB(atime,uptime,s,dbc);
1004     if ( sgnl < 0 ){
1005     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
1006     return(sgnl);
1007     };
1008     //
1009     idcalib[s] = glcalo->ID_ROOT_L0;
1010     fromtime[s] = glcalo->FROM_TIME;
1011     if ( glcalo->TO_TIME < atime ){ // calibration is corrupted and we are using the one that preceed the good one
1012     totime[s] = uptime;
1013     } else {
1014     totime[s] = glcalo->TO_TIME;
1015     };
1016     // totime[s] = glcalo->TO_TIME;
1017     calibno[s] = glcalo->EV_ROOT;
1018     //
1019     if ( totime[s] == 0 ){
1020     if ( verbose ) printf(" CALORIMETER - WARNING: data with no associated calibration\n");
1021     ClearCalibVals(s);
1022     sgnl = 100;
1023     };
1024     //
1025     // determine path and name and entry of the calibration file
1026     //
1027     GL_ROOT *glroot = new GL_ROOT();
1028     if ( verbose ) printf("\n");
1029     if ( verbose ) printf(" ** SECTION %i **\n",s);
1030     //
1031     sgnl = glroot->Query_GL_ROOT(idcalib[s],dbc);
1032     if ( sgnl < 0 ){
1033     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
1034     return(sgnl);
1035     };
1036     //
1037     stringstream name;
1038     name.str("");
1039     name << glroot->PATH.Data() << "/";
1040     name << glroot->NAME.Data();
1041     //
1042     fcalname[s] = (TString)name.str().c_str();
1043     if ( verbose ) printf(" - event at time %u. From time %u to time %u \n use file %s \n calibration at entry %i \n\n",atime,fromtime[s],totime[s],fcalname[s].Data(),calibno[s]);
1044     //
1045     sgnl = LoadCalib(s);
1046     //
1047     if ( sgnl != 0 ) return(sgnl);
1048     delete glcalo;
1049     delete glroot;
1050     //
1051     return(0);
1052     //
1053     }
1054    
1055     Int_t CaloLevel0::LoadCalib(Int_t s){
1056     //
1057     ifstream myfile;
1058     myfile.open(fcalname[s].Data());
1059     if ( !myfile ){
1060     return(-107);
1061     };
1062     myfile.close();
1063     //
1064     TFile *File = new TFile(fcalname[s].Data());
1065     if ( !File ) return(-108);
1066     TTree *tr = (TTree*)File->Get("CalibCalPed");
1067     if ( !tr ) return(-109);
1068     //
1069     TBranch *calo = tr->GetBranch("CalibCalPed");
1070     //
1071     pamela::CalibCalPedEvent *ce = 0;
1072     tr->SetBranchAddress("CalibCalPed", &ce);
1073     //
1074     Long64_t ncalibs = calo->GetEntries();
1075     //
1076     if ( !ncalibs ) return(-110);
1077     //
1078     calo->GetEntry(calibno[s]);
1079     //
1080     if (ce->cstwerr[s] != 0 && ce->cperror[s] == 0 ) {
1081     for ( Int_t d=0 ; d<11 ;d++ ){
1082     Int_t pre = -1;
1083     for ( Int_t j=0; j<96 ;j++){
1084     if ( j%16 == 0 ) pre++;
1085     if ( s == 2 ){
1086     calped[0][2*d+1][j] = ce->calped[3][d][j];
1087     cstwerr[3] = ce->cstwerr[3];
1088     cperror[3] = ce->cperror[3];
1089     calgood[0][2*d+1][j] = ce->calgood[3][d][j];
1090     calthr[0][2*d+1][pre] = ce->calthr[3][d][pre];
1091     calrms[0][2*d+1][j] = ce->calrms[3][d][j];
1092     calbase[0][2*d+1][pre] = ce->calbase[3][d][pre];
1093     calvar[0][2*d+1][pre] = ce->calvar[3][d][pre];
1094     };
1095     if ( s == 3 ){
1096     calped[0][2*d][j] = ce->calped[1][d][j];
1097     cstwerr[1] = ce->cstwerr[1];
1098     cperror[1] = ce->cperror[1];
1099     calgood[0][2*d][j] = ce->calgood[1][d][j];
1100     calthr[0][2*d][pre] = ce->calthr[1][d][pre];
1101     calrms[0][2*d][j] = ce->calrms[1][d][j];
1102     calbase[0][2*d][pre] = ce->calbase[1][d][pre];
1103     calvar[0][2*d][pre] = ce->calvar[1][d][pre];
1104     };
1105     if ( s == 0 ){
1106     calped[1][2*d][j] = ce->calped[0][d][j];
1107     cstwerr[0] = ce->cstwerr[0];
1108     cperror[0] = ce->cperror[0];
1109     calgood[1][2*d][j] = ce->calgood[0][d][j];
1110     calthr[1][2*d][pre] = ce->calthr[0][d][pre];
1111     calrms[1][2*d][j] = ce->calrms[0][d][j];
1112     calbase[1][2*d][pre] = ce->calbase[0][d][pre];
1113     calvar[1][2*d][pre] = ce->calvar[0][d][pre];
1114     };
1115     if ( s == 1 ){
1116     calped[1][2*d+1][j] = ce->calped[2][d][j];
1117     cstwerr[2] = ce->cstwerr[2];
1118     cperror[2] = ce->cperror[2];
1119     calgood[1][2*d+1][j] = ce->calgood[2][d][j];
1120     calthr[1][2*d+1][pre] = ce->calthr[2][d][pre];
1121     calrms[1][2*d+1][j] = ce->calrms[2][d][j];
1122     calbase[1][2*d+1][pre] = ce->calbase[2][d][pre];
1123     calvar[1][2*d+1][pre] = ce->calvar[2][d][pre];
1124     };
1125     };
1126     };
1127     } else {
1128     if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");
1129     return(-111);
1130     };
1131     File->Close();
1132     return(0);
1133     }

  ViewVC Help
Powered by ViewVC 1.1.23