/[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.31 - (hide annotations) (download)
Mon Apr 19 14:17:22 2010 UTC (14 years, 7 months ago) by mocchiut
Branch: MAIN
Changes since 1.30: +38 -6 lines
Cross-talk correction 'exploding' bug 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 mocchiut 1.25 // extern struct FlEventi eventi_;
60     // extern struct FlGruppo gruppo_;
61     // extern struct FlGruppo2 gruppo2_;
62     // extern struct FlGruppo4 gruppo4_;
63     // extern struct FlTaglioen taglioen_;
64     // extern struct FlAngolo angolo_;
65     // extern struct FlWhere where_;
66     // extern struct FlGeneral general_;
67     // extern struct FlCh ch_;
68     // extern struct FlCalofit calofit_;
69     // extern struct FlPawcd pawcd_;
70     // extern struct FlQuestd questd_;
71     // eventi = &eventi_;
72     // gruppo = &gruppo_;
73     // gruppo2 = &gruppo2_;
74     // gruppo4 = &gruppo4_;
75     // taglioen = &taglioen_;
76     // angolo = &angolo_;
77     // where = &where_;
78     // general = &general_;
79     // ch = &ch_;
80     // calofit = &calofit_;
81     // pawcd = &pawcd_;
82     // questd = &questd_;
83     //
84 mocchiut 1.1 trkseqno = 0;
85     ClearStructs();
86     //
87     memset(dexy, 0, 2*22*96*sizeof(Float_t));
88     memset(dexyc, 0, 2*22*96*sizeof(Float_t));
89     memset(mip, 0, 2*22*96*sizeof(Float_t));
90     memset(base, 0, 2*22*6*sizeof(Float_t));
91     memset(sbase, 0, 2*22*6*sizeof(Float_t));
92 mocchiut 1.9 memset(obadmask, 0, 2*22*96*sizeof(Int_t));
93     memset(obadpulsemask, 0, 2*22*6*sizeof(Int_t));
94     memset(ctprecor, 0, 2*22*6*sizeof(Float_t));
95 mocchiut 1.22 memset(ctsicor, 0, 2*22*9*sizeof(Float_t));
96 mocchiut 1.9 memset(ctneigcor, 0, 2*22*6*sizeof(Float_t));
97 mocchiut 1.1 calopar1 = true;
98     calopar2 = true;
99     calopar3 = true;
100 mocchiut 1.22 calopar4 = true;
101     calopar5 = true;
102 mocchiut 1.1 crosst = true;
103 mocchiut 1.26 mask18 = false;
104 mocchiut 1.1 ftcalopar1 = 0;
105     ttcalopar1 = 0;
106     ftcalopar2 = 0;
107     ttcalopar2 = 0;
108     ftcalopar3 = 0;
109     ttcalopar3 = 0;
110 mocchiut 1.22 ftcalopar4 = 0;
111     ttcalopar4 = 0;
112     ftcalopar5 = 0;
113     ttcalopar5 = 0;
114 mocchiut 1.1 }
115    
116     void CaloLevel0::SetCrossTalk(Bool_t ct){
117     crosst = ct;
118 mocchiut 1.12 }
119 mocchiut 1.1
120 mocchiut 1.9 void CaloLevel0::SetCrossTalkType(Bool_t ct){
121     ctground = ct;
122 mocchiut 1.12 }
123 mocchiut 1.9
124 mocchiut 1.22 void CaloLevel0::SetCrossTalkType(Int_t ct){
125     if ( ct == 0 ) ctground = true;
126     if ( ct == 1 ){
127     ctground = false;
128     noselfct = false;
129     };
130     if ( ct == 2 ){
131     ctground = false;
132     noselfct = true;
133     };
134     }
135    
136 mocchiut 1.1 void CaloLevel0::SetVerbose(Bool_t ct){
137     verbose = ct;
138 mocchiut 1.12 }
139 mocchiut 1.1
140     /**
141     * Initialize CaloLevel0 object
142     **/
143 mocchiut 1.21 void CaloLevel0::ProcessingInit(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
144     if ( !dbc->IsConnected() ) throw -116;
145     this->InitDo(dbc,hs,sgnl,l0tree,isdeb,isverb);
146     }
147    
148     /**
149     * Initialize CaloLevel0 object
150     **/
151 mocchiut 1.7 void CaloLevel0::ProcessingInit(GL_TABLES *glt, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
152     //
153     const TString host = glt->CGetHost();
154     const TString user = glt->CGetUser();
155     const TString psw = glt->CGetPsw();
156     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
157     if ( !dbc->IsConnected() ) throw -116;
158 mocchiut 1.21 this->InitDo(dbc,hs,sgnl,l0tree,isdeb,isverb);
159     dbc->Close();
160     delete dbc;
161     }
162    
163    
164     void CaloLevel0::InitDo(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
165 mocchiut 1.8 stringstream myquery;
166     myquery.str("");
167     myquery << "SET time_zone='+0:00'";
168     dbc->Query(myquery.str().c_str());
169 mocchiut 1.1 //
170     debug = isdeb;
171     verbose = isverb;
172     //
173     l0tr=(TTree*)l0tree;
174     de = new pamela::calorimeter::CalorimeterEvent();
175     l0calo = (TBranch*)l0tr->GetBranch("Calorimeter");
176     l0tr->SetBranchAddress("Calorimeter", &de);
177     //
178     trkseqno = 0;
179     ClearStructs();
180     //
181     GL_CALO_CALIB *glcalo = new GL_CALO_CALIB();
182     //
183     sgnl = 0;
184     UInt_t uptime = 0;
185     //
186     for (Int_t s = 0; s < 4; s++){
187     idcalib[s] = 0;
188     fromtime[s] = 0;
189     totime[s] = 0;
190     calibno[s] = 0;
191     ClearCalibVals(s);
192     //
193     sgnl = glcalo->Query_GL_CALO_CALIB(hs,uptime,s,dbc);
194     if ( sgnl < 0 ){
195     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
196     return;
197     };
198     //
199     idcalib[s] = glcalo->ID_ROOT_L0;
200     fromtime[s] = glcalo->FROM_TIME;
201     if ( glcalo->TO_TIME < hs ){ // calibration is corrupted and we are using the one that preceed the good one
202     totime[s] = uptime;
203     } else {
204     totime[s] = glcalo->TO_TIME;
205     };
206     calibno[s] = glcalo->EV_ROOT;
207     //
208     if ( totime[s] == 0 ){
209     if ( verbose ) printf(" CALORIMETER - WARNING: data with no associated calibration\n");
210     ClearCalibVals(s);
211     sgnl = 100;
212     };
213     };
214     //
215     // determine path and name and entry of the calibration file
216     //
217     GL_ROOT *glroot = new GL_ROOT();
218     if ( verbose ) printf("\n");
219     for (Int_t s = 0; s < 4; s++){
220     if ( verbose ) printf(" ** SECTION %i **\n",s);
221     if ( totime[s] > 0 ){
222     //
223     sgnl = glroot->Query_GL_ROOT(idcalib[s],dbc);
224     if ( sgnl < 0 ){
225     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
226     return;
227     };
228     //
229     stringstream name;
230     name.str("");
231     name << glroot->PATH.Data() << "/";
232     name << glroot->NAME.Data();
233     //
234     fcalname[s] = (TString)name.str().c_str();
235     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]);
236     } else {
237     if ( verbose ) printf(" - runheader at time %u. NO CALIBRATION INCLUDE THE RUNHEADER! ",hs);
238     };
239     sgnl = LoadCalib(s);
240     if ( sgnl ) break;
241     };
242     //
243     delete glcalo;
244     delete glroot;
245     //
246     return;
247     //
248     }
249    
250 mocchiut 1.7 Int_t CaloLevel0::ChkCalib(GL_TABLES *glt, UInt_t atime){
251 mocchiut 1.1 Int_t sgnl = 0;
252     for ( Int_t s = 0; s < 4; s++){
253     if ( atime > totime[s] ){
254 mocchiut 1.7 sgnl = Update(glt,atime,s);
255 mocchiut 1.1 if ( sgnl < 0 ) return(sgnl);
256     };
257     };
258     return(sgnl);
259     }
260    
261 mocchiut 1.21 Int_t CaloLevel0::ChkParam(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){
262     Int_t sig = this->ChkParamDo(dbc,runheader,mechal);
263     return(sig);
264     }
265    
266 mocchiut 1.7 Int_t CaloLevel0::ChkParam(GL_TABLES *glt, UInt_t runheader, Bool_t mechal){
267     const TString host = glt->CGetHost();
268     const TString user = glt->CGetUser();
269     const TString psw = glt->CGetPsw();
270     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
271     if ( !dbc->IsConnected() ) throw -116;
272 mocchiut 1.8 stringstream myquery;
273     myquery.str("");
274     myquery << "SET time_zone='+0:00'";
275     dbc->Query(myquery.str().c_str());
276 mocchiut 1.7 //
277 mocchiut 1.21 Int_t sig = this->ChkParamDo(dbc,runheader,mechal);
278     dbc->Close();
279     delete dbc;
280     return(sig);
281     }
282    
283     Int_t CaloLevel0::ChkParamDo(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){
284     //
285 mocchiut 1.1 stringstream calfile;
286     stringstream bmfile;
287     stringstream aligfile;
288     Int_t error = 0;
289     FILE *f = 0;
290     ifstream badfile;
291     GL_PARAM *glparam = new GL_PARAM();
292     //
293     if ( calopar1 || ( ttcalopar1 != 0 && ttcalopar1 < runheader ) ){
294 mocchiut 1.2 //
295     if ( debug ) printf(" calopar1 %i ftcalopar1 %u ttcalopar1 %u runheader %u \n",calopar1,ftcalopar1,ttcalopar1,runheader);
296     //
297 mocchiut 1.22 if ( calopar1 ){
298     //
299     // determine where I can find calorimeter ADC to MIP conversion file
300     //
301     if ( verbose ) printf(" Querying DB for calorimeter parameters files...\n");
302     //
303     error = 0;
304     error = glparam->Query_GL_PARAM(runheader,101,dbc);
305     if ( error < 0 ) return(error);
306     //
307     calfile.str("");
308     calfile << glparam->PATH.Data() << "/";
309     calfile << glparam->NAME.Data();
310     //
311     if ( verbose ) printf("\n Using ADC to MIP conversion file: \n %s \n",calfile.str().c_str());
312     f = fopen(calfile.str().c_str(),"rb");
313     if ( !f ){
314     if ( verbose ) printf(" CALORIMETER - ERROR: no ADC to MIP file!\n");
315     return(-105);
316     };
317     //
318     for (Int_t m = 0; m < 2 ; m++ ){
319     for (Int_t k = 0; k < 22; k++ ){
320     for (Int_t l = 0; l < 96; l++ ){
321     fread(&mip[m][k][l],sizeof(mip[m][k][l]),1,f);
322     if ( debug ) printf(" %f \n",mip[m][k][l]);
323     };
324     };
325     };
326     fclose(f);
327     };
328     //
329 mocchiut 1.1 calopar1 = false;
330     //
331 mocchiut 1.22 // flight extra corrections:
332 mocchiut 1.1 //
333 mocchiut 1.22 if ( verbose ) printf(" Querying DB for calorimeter flight ADC to MIP files...\n");
334 mocchiut 1.1 //
335     error = 0;
336 mocchiut 1.22 error = glparam->Query_GL_PARAM(runheader,110,dbc);
337 mocchiut 1.1 if ( error < 0 ) return(error);
338     //
339     calfile.str("");
340     calfile << glparam->PATH.Data() << "/";
341     calfile << glparam->NAME.Data();
342     ftcalopar1 = glparam->FROM_TIME;
343     ttcalopar1 = glparam->TO_TIME;
344     //
345 mocchiut 1.22 if ( verbose ) printf("\n Using ADC to MIP special conversion file: \n %s \n",calfile.str().c_str());
346     ifstream spfile;
347     spfile.open(calfile.str().c_str());
348     if ( !spfile ){
349     if ( verbose ) printf(" CALORIMETER - ERROR: no special calibration file!\n");
350     return(-123);
351 mocchiut 1.1 };
352 mocchiut 1.22 //
353     Int_t vview = 0;
354     Int_t vplane = 0;
355     Int_t vstrip = 0;
356     Float_t vval = 0.;
357     while ( spfile >> vview && spfile >> vplane && spfile >> vstrip && spfile >> vval){
358     if ( debug ) printf(" Setting ADC to MIP conversion factor: view %i plane %i strip %i mip %f \n",vview,vplane,vstrip,vval);
359     mip[vview][vplane][vstrip] = vval;
360     };
361 mocchiut 1.1 //
362     };
363     //
364 mocchiut 1.22 //
365 mocchiut 1.1 if ( calopar2 || ( ttcalopar2 != 0 && ttcalopar2 < runheader ) ){
366 mocchiut 1.21 //
367 mocchiut 1.2 if ( debug ) printf(" calopar2 %i ftcalopar2 %u ttcalopar2 %u runheader %u \n",calopar2,ftcalopar2,ttcalopar2,runheader);
368 mocchiut 1.1 calopar2 = false;
369     //
370     // determine where I can find calorimeter alignment file
371     //
372     //
373     error = 0;
374     error = glparam->Query_GL_PARAM(runheader,102,dbc);
375     if ( error < 0 ) return(error);
376     //
377     aligfile.str("");
378     aligfile << glparam->PATH.Data() << "/";
379     aligfile << glparam->NAME.Data();
380     ftcalopar2 = glparam->FROM_TIME;
381     ttcalopar2 = glparam->TO_TIME;
382     //
383 mocchiut 1.2 if ( verbose ) printf("\n Using parameter file: \n %s \n",aligfile.str().c_str());
384 mocchiut 1.1 f = fopen(aligfile.str().c_str(),"rb");
385     if ( !f ){
386 mocchiut 1.2 if ( verbose ) printf(" CALORIMETER - ERROR: no parameter file!\n");
387 mocchiut 1.1 return(-106);
388     };
389     //
390 mocchiut 1.2 if ( !mechal ){
391     //
392     fread(&clevel1->xalig,sizeof(clevel1->xalig),1,f);
393     if ( debug ) printf(" xalig = %f \n",clevel1->xalig);
394     fread(&clevel1->yalig,sizeof(clevel1->yalig),1,f);
395     if ( debug ) printf(" yalig = %f \n",clevel1->yalig);
396     fread(&clevel1->zalig,sizeof(clevel1->zalig),1,f);
397     if ( debug ) printf(" zalig = %f \n",clevel1->zalig);
398     } else {
399     if ( verbose ) printf("\n Using MECHANICAL alignement parameters \n");
400     //
401     CaloStrip cs = CaloStrip();
402     cs.UseMechanicalAlig();
403     clevel1->xalig = cs.GetXalig();
404     if ( debug ) printf(" xalig = %f \n",clevel1->xalig);
405     clevel1->yalig = cs.GetYalig();
406     if ( debug ) printf(" yalig = %f \n",clevel1->yalig);
407     clevel1->zalig = cs.GetZalig();
408     if ( debug ) printf(" zalig = %f \n",clevel1->zalig);
409     //
410     Float_t tmp = 0;
411     fread(&tmp,sizeof(clevel1->xalig),1,f);
412     fread(&tmp,sizeof(clevel1->yalig),1,f);
413     fread(&tmp,sizeof(clevel1->zalig),1,f);
414 mocchiut 1.11 // clevel1->zalig = -265.82;
415 mocchiut 1.2 //
416     };
417 mocchiut 1.1 fread(&clevel1->emin,sizeof(clevel1->emin),1,f);
418     if ( debug ) printf(" signal threshold = %f \n",clevel1->emin);
419     //
420     fclose(f);
421     };
422     //
423     // Load offline bad strip mask
424     //
425     if ( calopar3 || ( ttcalopar3 != 0 && ttcalopar3 < runheader ) ){
426 mocchiut 1.2 if ( debug ) printf(" calopar3 %i ftcalopar3 %u ttcalopar3 %u runheader %u \n",calopar3,ftcalopar3,ttcalopar3,runheader);
427 mocchiut 1.1 calopar3 = false;
428     //
429     // determine where I can find calorimeter alignment file
430     //
431     //
432     error = 0;
433     error = glparam->Query_GL_PARAM(runheader,103,dbc);
434     if ( error < 0 ) return(error);
435     //
436     bmfile.str("");
437     bmfile << glparam->PATH.Data() << "/";
438     bmfile << glparam->NAME.Data();
439     ftcalopar3 = glparam->FROM_TIME;
440     ttcalopar3 = glparam->TO_TIME;
441     //
442     if ( verbose ) printf("\n Using bad strip offline mask file: \n %s \n\n",bmfile.str().c_str());
443     badfile.open(bmfile.str().c_str());
444     if ( !badfile ){
445     if ( verbose ) printf(" CALORIMETER - ERROR: no bad strip offline mask file!\n");
446     return(-115);
447     };
448     //
449     Bool_t isdone = false;
450     Int_t bad = 0;
451     Int_t view = 1;
452     Int_t strip = 0;
453     Int_t plane = 21;
454     while ( !isdone ) {
455     badfile >> bad;
456     obadmask[view][plane][strip] = bad;
457     if ( debug && bad ) printf(" SETTING view %i plane %i strip %i BAD = %i \n",view,plane,strip,bad);
458     strip++;
459     if ( strip > 95 ){
460     strip = 0;
461     plane--;
462     if ( plane < 0 ){
463     plane = 21;
464     view--;
465     };
466     if ( view < 0 ) isdone = true;
467     };
468     };
469     //
470     badfile.close();
471     };
472     //
473 mocchiut 1.22 // calopar4
474     //
475     if ( calopar4 || ( ttcalopar4 != 0 && ttcalopar4 < runheader ) ){
476     //
477     if ( debug ) printf(" calopar4 %i ftcalopar4 %u ttcalopar4 %u runheader %u \n",calopar4,ftcalopar4,ttcalopar4,runheader);
478     //
479     calopar4 = false;
480     //
481     // flight extra corrections:
482     //
483     if ( verbose ) printf(" Querying DB for calorimeter max rms file...\n");
484     //
485     error = 0;
486     error = glparam->Query_GL_PARAM(runheader,109,dbc);
487     if ( error < 0 ) return(error);
488     //
489     calfile.str("");
490     calfile << glparam->PATH.Data() << "/";
491     calfile << glparam->NAME.Data();
492     ftcalopar4 = glparam->FROM_TIME;
493     ttcalopar4 = glparam->TO_TIME;
494     //
495     if ( verbose ) printf("\n Using calorimeter max rms file: \n %s \n",calfile.str().c_str());
496     ifstream spfile;
497     spfile.open(calfile.str().c_str());
498     if ( !spfile ){
499     if ( verbose ) printf(" CALORIMETER - ERROR: no max rms file!\n");
500     return(-124);
501     };
502     //
503     Int_t vview = 0;
504     Int_t vplane = 0;
505     Int_t vval = 0;
506     for (Int_t l=0; l<2; l++){
507     for (Int_t m=0; m<22; m++){
508     maxrms[l][m] = 26;
509     };
510     };
511     while ( spfile >> vview && spfile >> vplane && spfile >> vval){
512     if ( debug ) printf(" Setting view %i plane %i max rms %i \n",vview,vplane,vval);
513     maxrms[vview][vplane] = vval;
514     };
515     spfile.close();
516     //
517     };
518     //
519     // calopar5
520     //
521     if ( calopar5 || ( ttcalopar5 != 0 && ttcalopar5 < runheader ) ){
522     //
523     if ( debug ) printf(" calopar5 %i ftcalopar5 %u ttcalopar5 %u runheader %u \n",calopar5,ftcalopar5,ttcalopar5,runheader);
524     //
525     calopar5 = false;
526     //
527     // flight extra corrections:
528     //
529     if ( verbose ) printf(" Querying DB for calorimeter noise to signal threshold file...\n");
530     //
531     error = 0;
532     error = glparam->Query_GL_PARAM(runheader,111,dbc);
533     if ( error < 0 ) return(error);
534     //
535     calfile.str("");
536     calfile << glparam->PATH.Data() << "/";
537     calfile << glparam->NAME.Data();
538     ftcalopar5 = glparam->FROM_TIME;
539     ttcalopar5 = glparam->TO_TIME;
540     //
541     if ( verbose ) printf("\n Using calorimeter noise to signal threshold file: \n %s \n",calfile.str().c_str());
542     ifstream spfile;
543     spfile.open(calfile.str().c_str());
544     if ( !spfile ){
545     if ( verbose ) printf(" CALORIMETER - ERROR: no noise to signal threshold file!\n");
546     return(-125);
547     };
548     //
549     Int_t vview = 0;
550     Int_t vplane = 0;
551     Int_t vstrip = 0;
552     Float_t vval = 0.;
553     for (Int_t l=0; l<2; l++){
554     for (Int_t m=0; m<22; m++){
555     for (Int_t n=0; n<96; n++){
556     memin[l][m][n] = 0.7;
557     };
558     };
559     };
560     while ( spfile >> vview && spfile >> vplane && spfile >> vstrip && spfile >> vval){
561     if ( vstrip == -1 ){
562     for (Int_t ll=0; ll<96; ll++){
563     if ( debug ) printf(" Setting view %i plane %i strip %i noise to signal ratio %f \n",vview,vplane,ll,vval);
564     memin[vview][vplane][ll] = vval;
565     };
566     } else {
567     if ( debug ) printf(" Setting view %i plane %i strip %i noise to signal ratio %f \n",vview,vplane,vstrip,vval);
568     memin[vview][vplane][vstrip] = vval;
569     };
570     };
571     spfile.close();
572     //
573     };
574     //
575     //
576 mocchiut 1.1 delete glparam;
577     //
578     return(0);
579     }
580    
581 mocchiut 1.22 Int_t CaloLevel0::CalcCrossTalkCorr(TSQLServer *dbc, UInt_t runheader, Bool_t ctusetable){
582     Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,ctusetable);
583     return(sig);
584     };
585    
586 mocchiut 1.21 Int_t CaloLevel0::CalcCrossTalkCorr(TSQLServer *dbc, UInt_t runheader){
587 mocchiut 1.22 Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,true);
588 mocchiut 1.21 return(sig);
589     }
590    
591 mocchiut 1.22 Int_t CaloLevel0::CalcCrossTalkCorr(GL_TABLES *glt, UInt_t runheader, Bool_t usetable){
592     const TString host = glt->CGetHost();
593     const TString user = glt->CGetUser();
594     const TString psw = glt->CGetPsw();
595     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
596     if ( !dbc->IsConnected() ) throw -116;
597     stringstream myquery;
598     myquery.str("");
599     myquery << "SET time_zone='+0:00'";
600     dbc->Query(myquery.str().c_str());
601     //
602     Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,usetable);
603     dbc->Close();
604     delete dbc;
605     //
606     return(sig);
607     //
608     };
609    
610 mocchiut 1.9 Int_t CaloLevel0::CalcCrossTalkCorr(GL_TABLES *glt, UInt_t runheader){
611     const TString host = glt->CGetHost();
612     const TString user = glt->CGetUser();
613     const TString psw = glt->CGetPsw();
614     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
615     if ( !dbc->IsConnected() ) throw -116;
616     stringstream myquery;
617     myquery.str("");
618     myquery << "SET time_zone='+0:00'";
619     dbc->Query(myquery.str().c_str());
620     //
621 mocchiut 1.22 Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,true);
622 mocchiut 1.21 dbc->Close();
623     delete dbc;
624     //
625     return(sig);
626     //
627     }
628    
629 mocchiut 1.22 Int_t CaloLevel0::CalcCrossTalkCorrDo(TSQLServer *dbc, UInt_t runheader, Bool_t usetable){
630 mocchiut 1.21 //
631     if ( ctground ) return(0);
632     //
633 mocchiut 1.9 Int_t error = 0;
634 mocchiut 1.22 GL_PARAM *glparam = new GL_PARAM();
635 mocchiut 1.9 //
636     // determine where I can find file with offline bad pulser mask
637     //
638 mocchiut 1.22 stringstream bmfile;
639 mocchiut 1.9 error = 0;
640     error = glparam->Query_GL_PARAM(runheader,105,dbc);
641     if ( error < 0 ) return(error);
642     //
643     bmfile.str("");
644     bmfile << glparam->PATH.Data() << "/";
645     bmfile << glparam->NAME.Data();
646     //
647 mocchiut 1.22 ifstream badfile;
648 mocchiut 1.9 if ( verbose ) printf("\n Using bad pulser offline mask file: \n %s \n\n",bmfile.str().c_str());
649     badfile.open(bmfile.str().c_str());
650     if ( !badfile ){
651     if ( verbose ) printf(" CALORIMETER - ERROR: no bad pulser offline mask file!\n");
652     return(-115);
653     };
654     //
655     Bool_t isdone = false;
656     Int_t bad = 0;
657     Int_t view = 1;
658     Int_t pre = 0;
659     Int_t plane = 21;
660     while ( !isdone ) {
661     badfile >> bad;
662     obadpulsemask[view][plane][pre] = bad;
663     if ( debug && bad ) printf(" SETTING view %i plane %i pre %i BAD = %i \n",view,plane,pre,bad);
664     pre++;
665     if ( pre > 5 ){
666     pre = 0;
667     plane--;
668     if ( plane < 0 ){
669     plane = 21;
670     view--;
671     };
672     if ( view < 0 ) isdone = true;
673     };
674     };
675     //
676     badfile.close();
677 mocchiut 1.22 if ( !usetable ){
678 mocchiut 1.9 //
679 mocchiut 1.22 // Let's start with cross-talk correction calculation
680 mocchiut 1.9 //
681 mocchiut 1.22 GL_CALOPULSE_CALIB *glp = new GL_CALOPULSE_CALIB();
682     Float_t adcp[2][22][96];
683     Float_t adcpcal[2][22][96];
684     memset(adcp , 0, 2*22*96*sizeof(Float_t));
685     memset(adcpcal , 0, 2*22*96*sizeof(Float_t));
686 mocchiut 1.9 //
687 mocchiut 1.22 UInt_t pampli = 0;
688     for (Int_t s=0; s<4; s++){
689     //
690     // Save into matrix adcp the values of the highest pulse calibration (pulse amplitude = 2)
691     //
692     pampli = 2;
693     error = 0;
694     error = glp->Query_GL_CALOPULSE_CALIB(runheader,s,pampli,dbc);
695     if ( error < 0 ){
696     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
697     return(error);
698     };
699     //
700     UInt_t idcalib = glp->ID_ROOT_L0;
701     UInt_t fromtime = glp->FROM_TIME;
702     UInt_t calibno = glp->EV_ROOT;
703     //
704     // determine path and name and entry of the calibration file
705     //
706     GL_ROOT *glroot = new GL_ROOT();
707     if ( verbose ) printf("\n");
708     if ( verbose ) printf(" ** SECTION %i **\n",s);
709     //
710     error = 0;
711     error = glroot->Query_GL_ROOT(idcalib,dbc);
712     if ( error < 0 ){
713     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
714     return(error);
715     };
716     //
717     stringstream name;
718     name.str("");
719     name << glroot->PATH.Data() << "/";
720     name << glroot->NAME.Data();
721     //
722     TString fcalname = (TString)name.str().c_str();
723     ifstream myfile;
724     myfile.open(fcalname.Data());
725     if ( !myfile ){
726     return(-107);
727     };
728     myfile.close();
729     //
730     TFile *File = new TFile(fcalname.Data());
731     if ( !File ) return(-108);
732     TTree *tr = (TTree*)File->Get("CalibCalPulse2");
733     if ( !tr ) return(-119);
734     //
735     TBranch *calo = tr->GetBranch("CalibCalPulse2");
736     //
737     pamela::CalibCalPulse2Event *ce = 0;
738     tr->SetBranchAddress("CalibCalPulse2", &ce);
739     //
740     Long64_t ncalibs = calo->GetEntries();
741     //
742     if ( !ncalibs ) return(-110);
743     //
744 mocchiut 1.30 if ( calo->GetEntry(calibno) <= 0) throw -36;
745 mocchiut 1.22 if ( verbose ) printf(" PULSE2 using entry %u from file %s",calibno,fcalname.Data());
746     //
747     // retrieve calibration table
748     //
749     if ( ce->pstwerr[s] && ce->pperror[s] == 0 && ce->unpackError == 0 ){
750     for ( Int_t d=0 ; d<11 ;d++ ){
751     for ( Int_t j=0; j<96 ;j++){
752     if ( s == 2 ){
753     adcp[0][2*d+1][j] = ce->calpuls[3][d][j];
754     };
755     if ( s == 3 ){
756     adcp[0][2*d][j] = ce->calpuls[1][d][j];
757     };
758     if ( s == 0 ){
759     adcp[1][2*d][j] = ce->calpuls[0][d][j];
760     };
761     if ( s == 1 ){
762     adcp[1][2*d+1][j] = ce->calpuls[2][d][j];
763     };
764     };
765     };
766     } else {
767     if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");
768     return(-111);
769     };
770     //
771     File->Close();
772     delete glroot;
773     //
774     // Save into matrix adcpcal the calibrated values of the pulse calibration (subtraction of pulse amplitude = 0 relative to the pulse2 calibration used)
775     //
776     pampli = 0;
777     error = 0;
778     error = glp->Query_GL_CALOPULSE_CALIB(fromtime,s,pampli,dbc);
779     if ( error < 0 ){
780     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
781     return(error);
782     };
783     //
784     idcalib = glp->ID_ROOT_L0;
785     calibno = glp->EV_ROOT;
786     //
787     // determine path and name and entry of the calibration file
788     //
789     glroot = new GL_ROOT();
790     if ( verbose ) printf("\n");
791     if ( verbose ) printf(" ** SECTION %i **\n",s);
792     //
793     error = 0;
794     error = glroot->Query_GL_ROOT(idcalib,dbc);
795     if ( error < 0 ){
796     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
797     return(error);
798     };
799     //
800     name.str("");
801     name << glroot->PATH.Data() << "/";
802     name << glroot->NAME.Data();
803     //
804     fcalname = (TString)name.str().c_str();
805     myfile.open(fcalname.Data());
806     if ( !myfile ){
807     return(-107);
808     };
809     myfile.close();
810     //
811     TFile *File1 = new TFile(fcalname.Data());
812     if ( !File1 ) return(-108);
813     TTree *tr1 = (TTree*)File1->Get("CalibCalPulse1");
814     if ( !tr1 ) return(-120);
815     //
816     TBranch *calo1 = tr1->GetBranch("CalibCalPulse1");
817     //
818     pamela::CalibCalPulse1Event *ce1 = 0;
819     tr1->SetBranchAddress("CalibCalPulse1", &ce1);
820     //
821     ncalibs = calo1->GetEntries();
822     //
823     if ( !ncalibs ) return(-110);
824     //
825 mocchiut 1.30 if ( calo1->GetEntry(calibno) <= 0 ) throw -36;
826 mocchiut 1.22 if ( verbose ) printf(" PULSE1 using entry %u from file %s",calibno,fcalname.Data());
827     //
828     // retrieve calibration table
829     //
830     if ( ce1->pstwerr[s] && ce1->pperror[s] == 0 && ce1->unpackError == 0 ){
831     for ( Int_t d=0 ; d<11 ;d++ ){
832     for ( Int_t j=0; j<96 ;j++){
833     if ( s == 2 ){
834     adcpcal[0][2*d+1][j] = adcp[0][2*d+1][j] - ce1->calpuls[3][d][j];
835     };
836     if ( s == 3 ){
837     adcpcal[0][2*d][j] = adcp[0][2*d][j] - ce1->calpuls[1][d][j];
838     };
839     if ( s == 0 ){
840     adcpcal[1][2*d][j] = adcp[1][2*d][j] - ce1->calpuls[0][d][j];
841     };
842     if ( s == 1 ){
843     adcpcal[1][2*d+1][j] = adcp[1][2*d+1][j] - ce1->calpuls[2][d][j];
844     };
845     };
846     };
847     } else {
848     if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");
849     return(-111);
850     };
851     //
852     File1->Close();
853     //
854     delete glroot;
855     //
856     };// loop on the four sections
857 mocchiut 1.9 //
858     //
859 mocchiut 1.22 delete glp;
860 mocchiut 1.9 //
861 mocchiut 1.22 // Ok, now we can try to calculate the cross-talk correction for each pre-amplifier
862 mocchiut 1.9 //
863 mocchiut 1.22 for ( Int_t v=0; v<2; v++){
864     if ( debug ) printf(" \n\n NEW VIEW \n");
865     for ( Int_t p=0; p<22; p++){
866     for ( Int_t npre=0; npre<6; npre++){
867     ctprecor[v][p][npre] = 1000.;
868     ctneigcor[v][p][npre] = 1000.;
869     Int_t str0=npre*16;
870     Int_t str16= -1 + (1+npre)*16;
871     //
872     UInt_t neigc = 0;
873     UInt_t farc = 0;
874     UInt_t pulsc = 0;
875     Float_t sigpulsed = 0.;
876     Float_t neigbase = 0.;
877     Float_t farbase = 0.;
878     //
879     // Loop over the strip of the pre and sum all signal far away from pulsed strip, signal in the neighbour(s) strip(s) and save the pulsed signal
880     // moreover count the number of strips in each case
881     //
882     for (Int_t s=str0; s<=str16; s++){
883     if ( adcpcal[v][p][s] > 10000.){
884     sigpulsed = adcpcal[v][p][s];
885     pulsc++;
886     if ( s > str0 ){
887     neigbase += adcpcal[v][p][s-1];
888     neigc++;
889     farbase -= adcpcal[v][p][s-1];
890     farc--;
891     };
892     if ( s < str16 ){
893     neigbase += adcpcal[v][p][s+1];
894     neigc++;
895     farbase -= adcpcal[v][p][s+1];
896     farc--;
897     };
898     } else {
899     farc++;
900     farbase += adcpcal[v][p][s];
901     };
902 mocchiut 1.9 };
903 mocchiut 1.22 //
904     // Now calculate the corrections
905     //
906     Float_t avefarbase = 0.;
907     if ( farc ) avefarbase = farbase/(Float_t)farc;
908     Float_t aveneigbase = 0.;
909     if ( neigc ) aveneigbase = neigbase/(Float_t)neigc;
910     //
911     if ( pulsc == 1 && farc && neigc ){
912     ctprecor[v][p][npre] = -avefarbase/(sigpulsed+fabs(avefarbase));
913     ctneigcor[v][p][npre] = fabs(aveneigbase-avefarbase)/(sigpulsed+fabs(avefarbase));
914     if ( debug ) printf(" Cross-talk correction View %i Plane %i Pre %i : pre-correction: %f neighbour strips correction %f \n",v,p,npre,ctprecor[v][p][npre],ctneigcor[v][p][npre]);
915     } else {
916     //
917     // did not find the pulsed strip or more than one pulsed strip found!
918     //
919     if ( debug ) printf(" Problems finding the cross-talk corrections: \n View %i Plane %i Pre %i number of pulsed strip %i \n Average faraway baseline %f number of strips %i Average neighbour baseline %f number of neighbour strips %i \n",v,p,npre,pulsc,avefarbase,farc,aveneigbase,neigc);
920     //
921 mocchiut 1.9 };
922     };
923 mocchiut 1.22 if ( debug ) printf(" \n ==================== \n");
924 mocchiut 1.9 };
925     };
926 mocchiut 1.22 } else {
927 mocchiut 1.9 //
928 mocchiut 1.22 // use pre-amply table
929     //
930 mocchiut 1.9 //
931 mocchiut 1.22 // determine where I can find file with offline neighbour correction table
932 mocchiut 1.9 //
933 mocchiut 1.22 stringstream bmfile2;
934 mocchiut 1.9 error = 0;
935 mocchiut 1.22 error = glparam->Query_GL_PARAM(runheader,106,dbc);
936     if ( error < 0 ) return(error);
937     //
938     bmfile2.str("");
939     bmfile2 << glparam->PATH.Data() << "/";
940     bmfile2 << glparam->NAME.Data();
941     //
942     ifstream badfile2;
943     if ( verbose ) printf("\n Using pre-amply neighbour crosstalk table file: \n %s \n\n",bmfile2.str().c_str());
944     badfile2.open(bmfile2.str().c_str());
945     if ( !badfile2 ){
946     if ( verbose ) printf(" CALORIMETER - ERROR: no pre-amply neighbour crosstalk table file!\n");
947     return(-121);
948 mocchiut 1.9 };
949 mocchiut 1.22 //
950     Int_t vview = 0;
951     Int_t vplane = 0;
952     Int_t vpre = 0;
953     Float_t vcorr = 0.;
954     while ( badfile2 >> vview && badfile2 >> vplane && badfile2 >> vpre && badfile2 >> vcorr){
955     if ( debug ) printf(" Pre-amply neighbour correction: view %i plane %i pre %i correction %f \n",vview,vplane,vpre,vcorr);
956     ctneigcor[vview][vplane][vpre] = vcorr;
957     };
958 mocchiut 1.9 //
959 mocchiut 1.22 // determine where I can find file with offline SECOND neighbour correction table
960 mocchiut 1.9 //
961 mocchiut 1.22 stringstream bmfile3;
962 mocchiut 1.9 error = 0;
963 mocchiut 1.22 error = glparam->Query_GL_PARAM(runheader,107,dbc);
964     if ( error < 0 ) return(error);
965 mocchiut 1.9 //
966 mocchiut 1.22 bmfile3.str("");
967     bmfile3 << glparam->PATH.Data() << "/";
968     bmfile3 << glparam->NAME.Data();
969     //
970     ifstream badfile3;
971     if ( verbose ) printf("\n Using pre-amply second neighbour crosstalk table file: \n %s \n\n",bmfile3.str().c_str());
972     badfile3.open(bmfile3.str().c_str());
973     if ( !badfile3 ){
974     if ( verbose ) printf(" CALORIMETER - ERROR: no pre-amply second neighbour crosstalk table file!\n");
975     return(-122);
976 mocchiut 1.9 };
977 mocchiut 1.22 //
978     Int_t pview = 0;
979     Int_t pplane = 0;
980     Int_t ppre = 0;
981     Float_t pcorr = 0.;
982     while ( badfile3 >> pview && badfile3 >> pplane && badfile3 >> ppre && badfile3 >> pcorr){
983     if ( debug ) printf(" Pre-amply second neighbour correction: view %i plane %i pre %i correction %f \n",pview,pplane,ppre,-pcorr);
984     ctprecor[pview][pplane][ppre] = -pcorr; // data are saved as negatives in the file
985     };
986 mocchiut 1.9 //
987 mocchiut 1.22 // determine where to find the file containing the Silicon crosstalk correction table
988 mocchiut 1.9 //
989 mocchiut 1.22 stringstream bmfile4;
990     error = 0;
991     error = glparam->Query_GL_PARAM(runheader,108,dbc);
992     if ( error < 0 ) return(error);
993 mocchiut 1.9 //
994 mocchiut 1.22 bmfile4.str("");
995     bmfile4 << glparam->PATH.Data() << "/";
996     bmfile4 << glparam->NAME.Data();
997     //
998     ifstream badfile4;
999     if ( verbose ) printf("\n Using Silicon crosstalk table file: \n %s \n\n",bmfile4.str().c_str());
1000     badfile4.open(bmfile4.str().c_str());
1001     if ( !badfile4 ){
1002     if ( verbose ) printf(" CALORIMETER - ERROR: no Silicon crosstalk table file!\n");
1003     return(-125);
1004 mocchiut 1.9 };
1005 mocchiut 1.22 //
1006     Int_t spview = 0;
1007     Int_t spplane = 0;
1008     Int_t psil = 0;
1009     Float_t spcorr = 0.;
1010     memset(ctsicor, 0, 2*22*9*sizeof(Float_t));
1011     while ( badfile4 >> spview && badfile4 >> spplane && badfile4 >> psil && badfile4 >> spcorr){
1012     if ( debug ) printf(" Silicon correction: view %i plane %i silicon %i correction %f \n",spview,spplane,psil,-spcorr);
1013     ctsicor[spview][spplane][psil] = -spcorr; // data are saved as negatives in the file
1014     };
1015 mocchiut 1.9 //
1016 mocchiut 1.22 };
1017 mocchiut 1.9 //
1018 mocchiut 1.22 delete glparam;
1019 mocchiut 1.9 //
1020     // Check the calculated corrections
1021     //
1022     Int_t opre=0;
1023     Int_t ppre=0;
1024     Bool_t found = false;
1025     for ( Int_t v=0; v<2; v++){
1026     for ( Int_t p=0; p<22; p++){
1027     for ( Int_t npre=0; npre<6; npre++){
1028     if ( ctprecor[v][p][npre] == 1000. || ctneigcor[v][p][npre] == 1000. || obadpulsemask[v][p][npre] != 0 ){
1029     if ( debug ) printf(" Cross-talk correction CHANGED for view %i Plane %i Pre %i\n BEFORE: pre-correction: %f neighbour strips correction %f \n",v,p,npre,ctprecor[v][p][npre],ctneigcor[v][p][npre]);
1030     if ( npre%2 ){
1031     opre = npre-1;
1032     } else {
1033     opre = npre+1;
1034     };
1035     if ( ctprecor[v][p][opre] == 1000. || ctneigcor[v][p][opre] == 1000. || obadpulsemask[v][p][opre] != 0 ){
1036     ppre=0;
1037     found = false;
1038     while ( ppre < 6 ){
1039     if ( ctprecor[v][p][ppre] != 1000. && ctneigcor[v][p][ppre] != 1000. && !obadpulsemask[v][p][ppre] ){
1040     found = true;
1041     ctprecor[v][p][npre] = ctprecor[v][p][ppre];
1042     ctneigcor[v][p][npre] = ctneigcor[v][p][ppre];
1043     break;
1044     };
1045     ppre++;
1046     };
1047     if ( !found ){
1048     if ( verbose ) printf(" WARNING: cannot find a good cross-talk correction for view %i plane %i pre %i \n Setting to default values 0.002 0.002\n",v,p,npre);
1049     ctprecor[v][p][npre] = 0.002;
1050     ctneigcor[v][p][npre] = 0.002;
1051     };
1052     } else {
1053     ctprecor[v][p][npre] = ctprecor[v][p][opre];
1054     ctneigcor[v][p][npre] = ctneigcor[v][p][opre];
1055     };
1056     if ( debug ) printf(" AFTER: pre-correction: %f neighbour strips correction %f \n",ctprecor[v][p][npre],ctneigcor[v][p][npre]);
1057     };
1058     };
1059     };
1060 mocchiut 1.22 };
1061 mocchiut 1.9 //
1062     return(0);
1063 mocchiut 1.12 }
1064 mocchiut 1.1
1065 mocchiut 1.16 void CaloLevel0::FindBaseCompress(Int_t l, Int_t m, Int_t pre){
1066 mocchiut 1.17 Int_t n = 0;
1067     Float_t q = 0;
1068     this->FindBaseCompress(l,m,pre,n,q);
1069     }
1070    
1071     void CaloLevel0::FindBaseCompress(Int_t l, Int_t m, Int_t pre, Int_t &nst, Float_t &qp){
1072 mocchiut 1.16 for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1073     dexy[l][m][e] = dexyc[l][m][e];
1074     };
1075 mocchiut 1.17 this->FindBaseRaw(l,m,pre,nst,qp);
1076 mocchiut 1.16 }
1077    
1078 mocchiut 1.1 void CaloLevel0::FindBaseRaw(Int_t l, Int_t m, Int_t pre){
1079 mocchiut 1.17 Int_t n = 0;
1080     Float_t q = 0;
1081     this->FindBaseRaw(l,m,pre,n,q);
1082     }
1083    
1084     void CaloLevel0::FindBaseRaw(Int_t l, Int_t m, Int_t pre, Int_t &nst, Float_t &qp){
1085     //
1086     Float_t minstrip = 100000.;
1087     Float_t rms = 0.;
1088 mocchiut 1.21 Int_t process = 0;
1089     Int_t onlmask[16];
1090     memset(onlmask, 0, 16*sizeof(Int_t));
1091     //
1092     while ( process < 2 ){
1093     //
1094     minstrip = 100000.;
1095     rms = 0.;
1096     base[l][m][pre] = 0.;
1097     qp = 0.;
1098     //
1099     Int_t spos = -1;
1100     Int_t ee = 0;
1101 mocchiut 1.1 for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1102 mocchiut 1.21 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. && onlmask[ee] == 0 ) {
1103     minstrip = dexy[l][m][e]-calped[l][m][e];
1104     rms = calthr[l][m][pre];
1105     spos = ee;
1106 mocchiut 1.17 };
1107 mocchiut 1.21 ee++;
1108     qp += (dexy[l][m][e]-calped[l][m][e]-sbase[l][m][e]);
1109 mocchiut 1.1 };
1110 mocchiut 1.17 //
1111 mocchiut 1.21 if ( debug && l==0 ){
1112     printf("\n BASELINE CALCULATION for view %i pl %i pre %i: \n => minstrip %f rms %f \n => qp = %f \n",l,m,pre,minstrip,rms,qp);
1113 mocchiut 1.16 };
1114 mocchiut 1.21 if ( minstrip != 100000. ) {
1115     Float_t strip6s = 0.;
1116     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1117     if ( (dexy[l][m][e]-calped[l][m][e]) >= minstrip && (dexy[l][m][e]-calped[l][m][e]) <= (minstrip+rms) ) {
1118     strip6s += 1.;
1119     base[l][m][pre] += (dexy[l][m][e] - calped[l][m][e]);
1120     };
1121     //
1122     // compression
1123     //
1124     // if ( abs((int)(dexy[l][m][e]-calped[l][m][e])) <= (minstrip+rms) ) {
1125     // dexyc[l][m][e] = 0.;
1126     // } else {
1127     dexyc[l][m][e] = dexy[l][m][e];
1128     // };
1129 mocchiut 1.17 };
1130     //
1131 mocchiut 1.21 if ( strip6s == 1. && process < 1 ){
1132     onlmask[spos] = 1;
1133     process++;
1134 mocchiut 1.22 if ( debug ) printf(" Warning, only one strip to calculate baseline: minstrip %f rms %f spos %i l %i m %i pre %i \n",minstrip,rms,spos,l,m,pre);
1135 mocchiut 1.21 continue;
1136     };
1137     process += 2;
1138     nst = (Int_t)strip6s;
1139 mocchiut 1.17 //
1140 mocchiut 1.22 if ( debug ){
1141 mocchiut 1.21 printf(" strip6s %f \n",strip6s);
1142     };
1143     // if ( strip6s >= 9. ){
1144     if ( (strip6s >= 2. && process == 2) || (strip6s >= 9. && process > 2) ){
1145 mocchiut 1.22 //if ( (strip6s >= 4. && process == 2) || (strip6s >= 9. && process > 2) ){
1146 mocchiut 1.21 Double_t arro = base[l][m][pre]/strip6s;
1147     Float_t deci = 1000.*((float)arro - float(int(arro)));
1148     if ( deci < 500. ) {
1149     arro = double(int(arro));
1150     } else {
1151     arro = 1. + double(int(arro));
1152     };
1153     base[l][m][pre] = arro;
1154     //
1155     // if too few strips were used to determine the baseline check if it is comparable with the previous event, if not mark it as bad
1156     //
1157 mocchiut 1.22 if ( debug && process > 2 ) printf(" AGH low strip value was discarded process %i strip6s %f minstrip %f rms %f spos %i\n",process,strip6s,minstrip,rms,spos);
1158     if ( debug ) printf(" Calculated baseline: base %f sbase-0.02*qp %f \n",base[l][m][pre],(-qp*0.02+sbase[l][m][pre]));
1159 mocchiut 1.21 //
1160 mocchiut 1.22 if ( strip6s < 4 && base[l][m][pre] > (-0.015*qp+sbase[l][m][pre]) && sbase[l][m][pre] > 0. ){
1161     if ( debug ) printf(" Suspicious calculated baseline: base %f sbase-0.02*qp %f strip6s %i \n",base[l][m][pre],(-qp*0.02+sbase[l][m][pre]),(Int_t)strip6s);
1162 mocchiut 1.21 base[l][m][pre] = 31000.;
1163 mocchiut 1.31 nst = 0; // 9RED BUG
1164     qp = 0.; // 9RED BUG
1165 mocchiut 1.21 for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1166     dexyc[l][m][e] = dexy[l][m][e];
1167     };
1168     };
1169     } else {
1170 mocchiut 1.31 if ( debug ) printf(" reset baseline here if ! ( (strip6s >=2 && process == 2) || (strip6s >= 9 and process > 2) ) \n");
1171 mocchiut 1.21 base[l][m][pre] = 31000.;
1172 mocchiut 1.31 nst = 0; // 9RED BUG
1173     qp = 0.; // 9RED BUG
1174 mocchiut 1.21 for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1175     dexyc[l][m][e] = dexy[l][m][e];
1176     };
1177 mocchiut 1.17 };
1178 mocchiut 1.1 } else {
1179 mocchiut 1.31 if ( debug ) printf(" reset baseline here if no minimum find\n");
1180     nst = 0; // 9RED BUG
1181     qp = 0.; // 9RED BUG
1182 mocchiut 1.21 process += 2;
1183 mocchiut 1.17 base[l][m][pre] = 31000.;
1184     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1185     dexyc[l][m][e] = dexy[l][m][e];
1186     };
1187 mocchiut 1.1 };
1188 mocchiut 1.17 };
1189 mocchiut 1.31 if ( debug ) printf(" Baseline calculation: baseline for view %i plane %i pre %i is %f nst %i qp %f \n",l,m,pre,base[l][m][pre],nst,qp);
1190 mocchiut 1.1 }
1191    
1192     Int_t CaloLevel0::Calibrate(Int_t ei){
1193     //
1194     // get entry ei
1195     //
1196 mocchiut 1.30 if ( l0calo->GetEntry(ei) <= 0 ) throw -36;
1197 mocchiut 1.1 //
1198     // if it was not a selftrigger event, could it ever been a selftrigger event? if so trigty = 3.
1199     //
1200 mocchiut 1.14 clevel2->nsatstrip = 0.;
1201 mocchiut 1.1 Int_t val = 0;
1202 mocchiut 1.26 Int_t del = 1000;
1203 mocchiut 1.6 for (Int_t sec = 0; sec < 4; sec++){
1204     for (Int_t dsec = 0; dsec < 7; dsec++){
1205     val = (Int_t)de->calselftrig[sec][dsec];
1206     del = delay(val);
1207     clevel2->selfdelay[sec][dsec] = del;
1208     };
1209     };
1210     val = 0;
1211 mocchiut 1.26 del = 1000;
1212     if ( clevel2->trigty < 2. ){
1213 mocchiut 1.1 Bool_t ck = false;
1214     for (Int_t sec = 0; sec < 4; sec++){
1215     val = (Int_t)de->calselftrig[sec][6];
1216     del = delay(val);
1217 mocchiut 1.26 if ( del < 1000 ){
1218 mocchiut 1.1 clevel2->wartrig = 0.;
1219     clevel2->trigty = 3.;
1220     ck = true;
1221     break;
1222     };
1223     };
1224 mocchiut 1.26 // if ( !ck ) clevel2->wartrig = 100.;
1225 mocchiut 1.1 } else {
1226     Bool_t ck = false;
1227     for (Int_t sec = 0; sec < 4; sec++){
1228     val = (Int_t)de->calselftrig[sec][6];
1229     del = delay(val);
1230 mocchiut 1.26 if ( del < 1000 ){
1231 mocchiut 1.1 clevel2->wartrig = 0.;
1232     ck = true;
1233     };
1234     };
1235     if ( !ck ) clevel2->wartrig = 100.;
1236     };
1237     //
1238     Int_t se = 5;
1239     Int_t done = 0;
1240     Int_t pre = -1;
1241     Bool_t isCOMP = false;
1242     Bool_t isFULL = false;
1243     Bool_t isRAW = false;
1244     Float_t ener;
1245     Int_t doneb = 0;
1246     Int_t donec = 0;
1247 mocchiut 1.22 Int_t ck[2][22][6];
1248     memset(ck, 0, 2*22*6*sizeof(Int_t));
1249 mocchiut 1.1 Int_t ipre = 0;
1250 mocchiut 1.19 // Int_t ip[3] = {0};
1251     Int_t ip[3] = {0,0,0};
1252 mocchiut 1.22 Int_t ipp = 0;
1253 mocchiut 1.1 Float_t base0, base1, base2;
1254     base0 = 0.;
1255     base1 = 0.;
1256     base2 = 0.;
1257 mocchiut 1.22 Float_t qpre[2][22][6];
1258     memset(qpre, 0, 2*22*6*sizeof(Float_t));
1259 mocchiut 1.1 Float_t ene[96];
1260     Int_t chdone[4] = {0,0,0,0};
1261     Int_t pe = 0;
1262     //
1263     Float_t ener0 = 0.;
1264     Float_t cbase0 = 0.;
1265 mocchiut 1.27 Float_t totbase = 0.;
1266     Float_t totped = 0.;
1267 mocchiut 1.1 Bool_t pproblem = false;
1268 mocchiut 1.22 Bool_t negbase = false;
1269 mocchiut 1.1 //
1270     Float_t tim = 0.;
1271     Int_t plo = 0;
1272     Int_t fbi = 0;
1273     Int_t cle = 0;
1274     //
1275     // run over views and planes
1276     //
1277     for (Int_t l = 0; l < 2; l++){
1278     for (Int_t m = 0; m < 22; m++){
1279     //
1280 mocchiut 1.2 // determine the section number
1281 mocchiut 1.1 //
1282 mocchiut 1.22 negbase = false;
1283 mocchiut 1.1 se = 5;
1284 mocchiut 1.2 if (l == 0 && m%2 == 0) se = 3;
1285 mocchiut 1.1 if (l == 0 && m%2 != 0) se = 2;
1286 mocchiut 1.2 if (l == 1 && m%2 != 0) se = 1;
1287     if (l == 1 && m%2 == 0) se = 0;
1288 mocchiut 1.1 //
1289     // determine what kind of event we are going to analyze
1290     //
1291     isCOMP = false;
1292     isFULL = false;
1293     isRAW = false;
1294     if ( de->stwerr[se] & (1 << 16) ) isCOMP = true;
1295     if ( de->stwerr[se] & (1 << 17) ) isFULL = true;
1296     if ( de->stwerr[se] & (1 << 3) ) isRAW = true;
1297     if ( !chdone[se] ){
1298     //
1299     // check for any error in the event
1300     //
1301     clevel2->crc[se] = 0;
1302     if ( de->perror[se] == 132 ){
1303     clevel2->crc[se] = 1;
1304     pe++;
1305     };
1306     clevel2->perr[se] = 0;
1307     if ( de->perror[se] != 0 ){
1308 mocchiut 1.24 clevel2->perr[se] = (Int_t)de->perror[se];
1309 mocchiut 1.1 pe++;
1310     };
1311     clevel2->swerr[se] = 0;
1312     for (Int_t j = 0; j < 7 ; j++){
1313     if ( (j != 3) && (de->stwerr[se] & (1 << j)) ){
1314     clevel2->swerr[se] = 1;
1315     pe++;
1316     };
1317     };
1318     chdone[se] = 1;
1319     };
1320     if ( clevel2->crc[se] == 0 && (clevel1->good2 == 1 || clevel2->trigty >= 2) ){
1321     pre = -1;
1322     //
1323     for (Int_t nn = 0; nn < 96; nn++){
1324 mocchiut 1.22 // ene[nn] = 0.;
1325 mocchiut 1.1 dexy[l][m][nn] = de->dexy[l][m][nn] ;
1326     dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
1327     };
1328     //
1329     // run over preamplifiers
1330     //
1331     pre = -1;
1332     cbase0 = 0.;
1333 mocchiut 1.17 Int_t nstt[2];
1334     Float_t rqp[2];
1335 mocchiut 1.1 for (Int_t i = 0; i < 3; i++){
1336 mocchiut 1.21 nstt[0] = 1000;
1337     nstt[1] = 1000;
1338 mocchiut 1.17 rqp[0] = 0.;
1339     rqp[1] = 0.;
1340 mocchiut 1.1 for (Int_t j = 0; j < 2; j++){
1341     pre = j + i*2;
1342     //
1343     // baseline check and calculation
1344     //
1345 mocchiut 1.16 if ( !isRAW ){
1346 mocchiut 1.18 //
1347     // if it is a compress event with fully transmitted pre try to calculate the baseline
1348     //
1349 mocchiut 1.16 if ( de->base[l][m][pre] != 0. && de->base[l][m][pre]<31000. ) {
1350     base[l][m][pre] = de->base[l][m][pre] ;
1351     } else {
1352 mocchiut 1.17 FindBaseCompress(l,m,pre,nstt[j],rqp[j]);
1353 mocchiut 1.16 };
1354 mocchiut 1.1 cbase0 += base[l][m][pre];
1355     } else {
1356     //
1357 mocchiut 1.17 // if it is a raw event calculate the baseline.
1358 mocchiut 1.1 //
1359 mocchiut 1.17 FindBaseRaw(l,m,pre,nstt[j],rqp[j]);
1360 mocchiut 1.1 cbase0 += base[l][m][pre];
1361     };
1362     };
1363 mocchiut 1.17 //
1364     // if we are able to calculate the baseline with more than 3 strips on one pre and not in the other one choose the pre with more calculated strips
1365     //
1366 mocchiut 1.21 if ( nstt[0] < 4 && nstt[1] >= 4 && nstt[0] != 1000 && nstt[1] != 1000 ) base[l][m][pre-1] = 31000.;
1367     if ( nstt[0] >= 4 && nstt[1] < 4 && nstt[0] != 1000 && nstt[1] != 1000 ) base[l][m][pre] = 31000.;
1368 mocchiut 1.22 // //
1369     // // if we are NOT able to calculate the baseline with more than 3 strips on both pres take the baseline (if any) of the one which has less energy
1370     // //
1371     // if ( nstt[0] < 4 && nstt[1] < 4 ){
1372     // if ( rqp[0] >= rqp[1] ) base[l][m][pre-1] = 31000.;
1373     // if ( rqp[0] < rqp[1] ) base[l][m][pre] = 31000.;
1374     // };
1375 mocchiut 1.1 };
1376     //
1377     // run over strips
1378     //
1379     pre = -1;
1380     ener0 = 0.;
1381 mocchiut 1.27 totbase = 0.;
1382     totped = 0.;
1383 mocchiut 1.1 for (Int_t i = 0 ; i < 3 ; i++){
1384     ip[i] = 0;
1385     for (Int_t n = i*32 ; n < (i+1)*32 ; n++){
1386     if (n%16 == 0) {
1387     done = 0;
1388     doneb = 0;
1389     donec = 0;
1390     pre++;
1391 mocchiut 1.22 ck[l][m][pre] = 0;
1392     qpre[l][m][pre] = 0.;
1393 mocchiut 1.1 };
1394     //
1395     // baseline check and calculation
1396     //
1397     // no suitable new baseline, use old ones!
1398     //
1399     if ( !done ){
1400 mocchiut 1.31 if ( debug ) printf(" l %i m %i pre %i ip[i] %i base %f base ip[i] %f sbase %f \n",l,m,pre,ip[i],base[l][m][pre],base[l][m][ip[i]],sbase[l][m][pre]);
1401 mocchiut 1.1 if ( (base[l][m][pre] == 31000. || base[l][m][pre] == 0.) ){
1402 mocchiut 1.22 ck[l][m][pre] = 1;
1403 mocchiut 1.1 if (pre%2 == 0) {
1404     ip[i] = pre + 1;
1405     } else {
1406     ip[i] = pre - 1;
1407     };
1408 mocchiut 1.3 if ( (base[l][m][ip[i]] == 31000. || base[l][m][ip[i]] == 0. || !crosst ) ){
1409 mocchiut 1.1 //
1410 mocchiut 1.22 ck[l][m][pre] = 2;
1411 mocchiut 1.1 if ( sbase[l][m][pre] == 31000. || sbase[l][m][pre] == 0. ) {
1412 mocchiut 1.22 ck[l][m][pre] = 3;
1413 mocchiut 1.1 };
1414     };
1415     };
1416 mocchiut 1.21 done = 1;
1417 mocchiut 1.1 };
1418     //
1419     // CALIBRATION ALGORITHM
1420     //
1421     if ( !doneb ){
1422 mocchiut 1.22 if ( debug ) printf(" ck[l][m][pre] is %i \n",ck[l][m][pre]);
1423     switch (ck[l][m][pre]) {
1424 mocchiut 1.1 case 0:
1425     base0 = base[l][m][pre];
1426     base2 = calbase[l][m][pre];
1427 mocchiut 1.16 if ( debug ) printf(" base0 = base l%i m%i pre%i = %f base2 = calbase l m pre = %f \n",l,m,pre,base[l][m][pre],calbase[l][m][pre]);
1428 mocchiut 1.1 break;
1429     case 1:
1430     base0 = base[l][m][ip[i]];
1431     base2 = calbase[l][m][ip[i]];
1432 mocchiut 1.16 if ( debug ) printf(" base0 = base l%i m%i ip(i)%i = %f base2 = calbase l m ip(i) = %f \n",l,m,ip[i],base[l][m][ip[i]],calbase[l][m][ip[i]]);
1433 mocchiut 1.1 break;
1434     case 2:
1435     base0 = sbase[l][m][pre];
1436 mocchiut 1.3 base2 = calbase[l][m][pre];
1437 mocchiut 1.16 if ( debug ) printf(" base0 = sbase l%i m%i pre%i = %f base2 = calbase l m pre = %f \n",l,m,pre,sbase[l][m][pre],calbase[l][m][pre]);
1438 mocchiut 1.1 break;
1439     case 3:
1440     base0 = calbase[l][m][pre];
1441     base2 = calbase[l][m][pre];
1442 mocchiut 1.16 if ( debug ) printf(" base0 = calbase l%i m%i pre%i = %f base2 = calbase l m pre = %f \n",l,m,pre,calbase[l][m][pre],calbase[l][m][pre]);
1443 mocchiut 1.1 break;
1444     };
1445     base1 = calbase[l][m][pre];
1446     doneb = 1;
1447     };
1448     ener = dexyc[l][m][n];
1449     ener0 += ener;
1450     clevel1->estrip[n][m][l] = 0.;
1451 mocchiut 1.29 totbase += de->base[l][m][pre]/96.;
1452 mocchiut 1.27 totped += fabs(calped[l][m][n]);
1453 mocchiut 1.22 if ( de->base[l][m][pre] < 0 ) negbase = true;
1454 mocchiut 1.1 if ( base0>0 && base0 < 30000. ){
1455 mocchiut 1.22 //
1456     // save the baseline only if the energy release is "small"
1457     //
1458     if ( !donec && (base0 + base1 - base2) != 0. && (n+1)%16==0 ){
1459     if ( qpre[l][m][pre] < 200. ) sbase[l][m][pre] = base0 + base1 - base2;
1460 mocchiut 1.1 donec = 1;
1461     };
1462     if ( ener > 0. ){
1463     clevel1->estrip[n][m][l] = (ener - calped[l][m][n] - base0 - base1 + base2)/mip[l][m][n] ;
1464     //
1465     // 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)
1466     //
1467 mocchiut 1.31 if ( debug && l==0 && (m==17 || m==18) ) printf(" view %i plane %i strip %i ener %f calped %f base0 %f base1 %f base2 %f mip %f ENERGIA %f \n",l,m,n,ener,calped[l][m][n],base0,base1,base2,mip[l][m][n],clevel1->estrip[n][m][l]);
1468 mocchiut 1.22 if ( clevel1->estrip[n][m][l] > 0. ) qpre[l][m][pre] += clevel1->estrip[n][m][l];
1469 mocchiut 1.3 //
1470     //
1471 mocchiut 1.1 };
1472     };
1473     };
1474 mocchiut 1.22 };
1475     //
1476     // check if there were problems with 5.7 or glitches in the power supply
1477 mocchiut 1.27 //
1478 mocchiut 1.29 // if ( ((ener0 == 0. && cbase0 == 0.) || negbase || totbase > 196600. || totped < 1. ) && !pproblem && clevel2->perr[se] == 0){ // check pedestal and baseline values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
1479     if ( ((ener0 == 0. && cbase0 == 0.) || negbase || totbase > 32700. || totped < 1. ) && !pproblem && clevel2->perr[se] == 0){ // check pedestal and baseline values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
1480     if ( verbose ) printf(" L0 entry %i : calorimeter power problems! event marked as bad perr %f swerr %X view %i plane %i negbase %i totbase %f totped %f\n",ei,de->perror[se],de->stwerr[se],l,m, negbase, totbase, totped);
1481 mocchiut 1.22 pproblem = true;
1482     pe++;
1483     };
1484     //
1485     } else {
1486     for (Int_t nn = 0; nn < 96; nn++){
1487     clevel1->estrip[nn][m][l] = 0.;
1488     };
1489     };
1490     };
1491     };
1492     //
1493     // run over views and planes to apply crosstalk corrections
1494     //
1495     for (Int_t l = 0; l < 2; l++){
1496     for (Int_t m = 0; m < 22; m++){
1497     //
1498     // determine the section number
1499     //
1500     se = 5;
1501     if (l == 0 && m%2 == 0) se = 3;
1502     if (l == 0 && m%2 != 0) se = 2;
1503     if (l == 1 && m%2 != 0) se = 1;
1504     if (l == 1 && m%2 == 0) se = 0;
1505     //
1506     // check for any error in the event
1507     //
1508     if ( clevel2->crc[se] == 0 && (clevel1->good2 == 1 || clevel2->trigty >= 2) ){
1509     //
1510     // Cross-talk corrections
1511     //
1512 mocchiut 1.31 if ( crosst ){
1513 mocchiut 1.22 //
1514     // energy on silicon ladders
1515     //
1516     Float_t qsi[3];
1517     qsi[0] = qpre[l][m][0]+qpre[l][m][1];
1518     qsi[1] = qpre[l][m][2]+qpre[l][m][3];
1519     qsi[2] = qpre[l][m][4]+qpre[l][m][5];
1520     //
1521     for ( pre = 1; pre < 6; pre += 2 ){
1522     Int_t ladder = (pre - 1)/2;
1523     //
1524     // If the noselfct flag is set the strip doesn't suffer the self crosstalk due to electronics so we must subtract some energy
1525     //
1526     if ( noselfct ){
1527     for (Int_t j = ladder*32 ; j < (ladder+1)*32 ; j++){
1528     ipre = j/16 ;
1529 mocchiut 1.31 if ( debug ) printf(" CT STEP1 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctprecor[l][m][ipre]);
1530 mocchiut 1.22 if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] -= clevel1->estrip[j][m][l] * ctprecor[l][m][ipre];
1531 mocchiut 1.31 if ( debug ) printf(" CT STEP2 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctprecor[l][m][ipre]);
1532 mocchiut 1.22 };
1533     };
1534     //
1535     // Using the neighbour pre baseline
1536     //
1537     if (ck[l][m][pre] == 1 || ck[l][m][pre-1] == 1){
1538     //
1539     // pre-amplifier effect on baseline when using the neighbour pre (ck=1)
1540     //
1541     if (ck[l][m][pre] == 1){
1542 mocchiut 1.19 ipre = pre;
1543 mocchiut 1.22 ipp = pre - 1;
1544 mocchiut 1.1 } else {
1545 mocchiut 1.19 ipre = pre - 1;
1546 mocchiut 1.22 ipp = pre;
1547 mocchiut 1.1 };
1548 mocchiut 1.22 Int_t it = 0;
1549     Float_t nqpre = 0.;
1550     //
1551     if ( debug ) printf(" CK1 Limit for while: 0.07 \n");
1552 mocchiut 1.1 for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
1553 mocchiut 1.31 if ( debug ) printf(" CT STEP3 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctprecor[l][m][ipp]);
1554 mocchiut 1.9 if ( !ctground ){
1555 mocchiut 1.22 if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += - qpre[l][m][ipp] * ctprecor[l][m][ipp];
1556 mocchiut 1.9 } else {
1557 mocchiut 1.22 if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += - qpre[l][m][ipp] * 0.00478;
1558 mocchiut 1.9 };
1559 mocchiut 1.22 if ( clevel1->estrip[j][m][l] > 0. ) nqpre += clevel1->estrip[j][m][l] ;
1560 mocchiut 1.31 if ( debug ) printf(" CT STEP4 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctprecor[l][m][ipp]);
1561 mocchiut 1.1 };
1562 mocchiut 1.22 qpre[l][m][ipre] = nqpre;
1563     nqpre = 0.;
1564     Float_t deltaqpre = qpre[l][m][ipre];
1565     //
1566     // these values are empirically determined, usually the routine converge due to deltaqsi and the latest applied correction is based on less than 1 mip
1567     //
1568     while ( it < 10 && deltaqpre > 0.07 ){
1569     nqpre = 0.;
1570     for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
1571 mocchiut 1.31 if ( debug ) printf(" CT STEP5 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctprecor[l][m][ipre]);
1572 mocchiut 1.22 if ( !ctground ){
1573     if ( debug ) printf(" CK1 pre correction: iteration %i deltaqpre %f ctprecor %f TOTAL CORRECTION %f \n",it,deltaqpre,ctprecor[l][m][ipre],deltaqpre * ctprecor[l][m][ipre]);
1574     if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += deltaqpre * ctprecor[l][m][ipre];
1575     } else {
1576     if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += deltaqpre * 0.00478;
1577     };
1578     if ( clevel1->estrip[j][m][l] > 0. ) nqpre += clevel1->estrip[j][m][l] ;
1579 mocchiut 1.31 if ( debug ) printf(" CT STEP6 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctprecor[l][m][ipre]);
1580 mocchiut 1.22 };
1581     if ( ctground ) it = 100;
1582     it++;
1583     deltaqpre = nqpre - qpre[l][m][ipre];
1584     if ( debug ) printf(" CK1 BEFORE: qpre %f \n",qpre[l][m][ipre]);
1585     qpre[l][m][ipre] = nqpre;
1586     if ( debug ) printf(" CK1 AFTER: qpre %f \n",qpre[l][m][ipre]);
1587     };
1588     //
1589 mocchiut 1.1 };
1590 mocchiut 1.22 //
1591     // No baseline calculation due to high energy release
1592     //
1593     if (ck[l][m][pre] == 2 && ck[l][m][pre-1] == 2){
1594     //
1595     // y^
1596     // |
1597     // | 6 7 8
1598     // | 3 4 5
1599     // | 0 1 2
1600     // | --------------------------------------> x
1601     //
1602     Int_t si1 = 0;
1603     Int_t si2 = 0;
1604     Int_t si3 = 0;
1605     if ( l == 0 ){
1606     if ( ladder == 0 ){
1607     si1 = 0;
1608     si2 = 3;
1609     si3 = 6;
1610     };
1611     if ( ladder == 1 ){
1612     si1 = 1;
1613     si2 = 4;
1614     si3 = 7;
1615     };
1616     if ( ladder == 2 ){
1617     si1 = 2;
1618     si2 = 5;
1619     si3 = 8;
1620     };
1621     } else {
1622     if ( ladder == 0 ){
1623     si1 = 0;
1624     si2 = 1;
1625     si3 = 2;
1626     };
1627     if ( ladder == 1 ){
1628     si1 = 3;
1629     si2 = 4;
1630     si3 = 5;
1631     };
1632     if ( ladder == 2 ){
1633     si1 = 6;
1634     si2 = 7;
1635     si3 = 8;
1636     };
1637     };
1638     //
1639     // Find the energy distribution along the considered plane looking at the two sandwiching plane of the other view.
1640     //
1641     Float_t sied[3] = {0.,0.,0.};
1642     Int_t othv = !l;
1643     Int_t othpl1 = m - 1;
1644     Int_t othpl2 = m + 1;
1645     Float_t oprof[3] = {0.,0.,0.};
1646     for(Int_t s=0; s<3; s++){
1647     for(Int_t t=(s*32); t<32*(s + 1); t++){
1648     if ( othpl1 > -1 ) {
1649     oprof[s] += clevel1->estrip[othv][othpl1][t];
1650     };
1651     if ( othpl2 < 22 ) {
1652     oprof[s] += clevel1->estrip[othv][othpl2][t];
1653     };
1654     };
1655     };
1656     Float_t otote = fabs(oprof[0]) + fabs(oprof[1]) + fabs(oprof[2]);
1657     for(Int_t g=0; g<3; g++){
1658     if ( otote > 0. ){
1659     sied[g] = fabs(oprof[g])/otote;
1660 mocchiut 1.9 } else {
1661 mocchiut 1.22 sied[g] = 1./3.;
1662 mocchiut 1.9 };
1663 mocchiut 1.1 };
1664 mocchiut 1.22 //
1665     //
1666     //
1667     Int_t it = 0;
1668     Int_t jpre = 0;
1669     Float_t nqsi = 0.;
1670     Float_t snqsi = qsi[ladder];
1671     Float_t nqpre[2] = {0.,0.};
1672     Float_t deltaqsi = qsi[ladder];
1673     Float_t deltaqpre[2];
1674     deltaqpre[0] = qpre[l][m][pre-1];
1675     deltaqpre[1] = qpre[l][m][pre];
1676     //
1677     if ( debug ) printf(" Limit for while: 0.07 it < 10 \n");
1678     //
1679     // these values are empirically determined, usually the routine converge due to deltaqsi and the latest applied correction is based on less than 1 mip
1680     //
1681     while ( it < 10 && (deltaqsi > 0.07 || deltaqpre[0] > 0.07 || deltaqpre[1] > 0.07) ){
1682     nqsi = 0.;
1683     nqpre[0] = 0.;
1684     nqpre[1] = 0.;
1685     for (Int_t j = ladder*32 ; j < (ladder+1)*32 ; j++){
1686 mocchiut 1.31 if ( debug ) printf(" CT STEP6 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctsicor[l][m][si2]);
1687 mocchiut 1.22 ipre = 0;
1688     if ( j > (ladder*32)+15 ) ipre = 1;
1689     jpre = j/16 ;
1690     //
1691     // Silicon effect on the baseline when using the same pre previous baseline (ck = 2) + pre-amply effect
1692     //
1693     if ( !ctground ){
1694     if ( debug ) printf(" silicon correction: iteration %i deltaqsi[%i] %f ctsicor %f %f %f sied %f %f %f si %i %i %i TOTAL CORRECTION %f \n",it,ladder,deltaqsi,ctsicor[l][m][si1],ctsicor[l][m][si2],ctsicor[l][m][si3],sied[0],sied[1],sied[2],si1,si2,si3,deltaqsi * (ctsicor[l][m][si1] * sied[0] + ctsicor[l][m][si2] * sied[1] + ctsicor[l][m][si3] * sied[2]));
1695     if ( debug ) printf(" pre correction: iteration %i deltaqpre[0] %f deltaqpre[1] %f ctprecor %f TOTAL CORRECTION %f \n",it,deltaqpre[0],deltaqpre[1],ctprecor[l][m][jpre],deltaqpre[ipre] * ctprecor[l][m][jpre]);
1696     if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += (deltaqsi * (ctsicor[l][m][si1] * sied[0] + ctsicor[l][m][si2] * sied[1] + ctsicor[l][m][si3] * sied[2])/mip[l][m][j]) + deltaqpre[ipre] * ctprecor[l][m][jpre];
1697     } else {
1698     if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += 0. + qpre[l][m][jpre] * 0.00478; // no correction
1699     };
1700     if ( clevel1->estrip[j][m][l] > 0. ) nqsi += clevel1->estrip[j][m][l] ;
1701     if ( clevel1->estrip[j][m][l] > 0. ) nqpre[ipre] += clevel1->estrip[j][m][l] ;
1702 mocchiut 1.31 if ( debug ) printf(" CT STEP7 %i %i %i estrip %f ctprecor %f \n",j,m,l,clevel1->estrip[j][m][l],ctsicor[l][m][si2]);
1703 mocchiut 1.22 };
1704     if ( ctground ) it = 100;
1705     deltaqsi = nqsi-snqsi;
1706 mocchiut 1.31 deltaqpre[0] = nqpre[0] - qpre[l][m][pre-1];
1707     deltaqpre[1] = nqpre[1] - qpre[l][m][pre];
1708     //
1709     // Check for divergence and stop if it happens! [9RED bug noticed with plane 18X]
1710     //
1711     if ( deltaqpre[0] > qpre[l][m][pre-1] || deltaqpre[1] > qpre[l][m][pre] || deltaqsi >snqsi ){
1712     if ( debug ) printf(" WARNING!! DIVERGING CORRECTION EXIT IMMEDIATLY FROM THE LOOP!! dqpre0 %f qpre0 %f // dqpre1 %f qpre1 %f // dqsi %f qsi %f \n",deltaqpre[0],qpre[l][m][pre-1],deltaqpre[1],qpre[l][m][pre],deltaqsi,snqsi);
1713     it = 1000;
1714     };
1715     //
1716 mocchiut 1.22 snqsi = nqsi;
1717     it++;
1718     if ( debug ) printf(" BEFORE: qpre 0 %f qpre 1 %f \n",qpre[l][m][pre-1],qpre[l][m][pre]);
1719     qpre[l][m][pre-1] = nqpre[0];
1720     qpre[l][m][pre] = nqpre[1];
1721     if ( debug ) printf(" AFTER: qpre 0 %f qpre 1 %f \n",qpre[l][m][pre-1],qpre[l][m][pre]);
1722     };
1723     //
1724     //
1725     //
1726     // for (Int_t j = ladder*32 ; j < (ladder+1)*32 ; j++){
1727     // ipre = j/16 ;
1728     // //
1729     // // pre-amplifier effect on baseline when using the same pre previous event baseline (ck=2)
1730     // //
1731     // if ( !ctground ){
1732     // if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += qpre[l][m][ipre] * ctprecor[l][m][ipre];
1733     // } else {
1734     // if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += qpre[l][m][ipre] * 0.00478;
1735     // };
1736     // };
1737 mocchiut 1.1 };
1738     };
1739     };
1740 mocchiut 1.22 };
1741     //
1742     Int_t j4 = -4;
1743     Int_t jjj = -3;
1744     Int_t jj = -2;
1745     Int_t jjpre = -1;
1746     Int_t jjjpre = -1;
1747     memset(ene, 0, 96*sizeof(Float_t));
1748     for (Int_t j = 0 ; j < 100 ; j++){
1749     jj++;
1750     jjj++;
1751     j4++;
1752     if ( j < 96 ) ene[j] = clevel1->estrip[j][m][l];
1753 mocchiut 1.31 if ( crosst ){
1754 mocchiut 1.22 //
1755     // "Real" crosstalk effect on the neighbour strips respect to the one which have seen the energy deposit
1756     //
1757     if ( jj >= 0 && jj < 96 ){
1758     if ( !ctground ){
1759     if ( jj%16 == 0 ) jjpre++;
1760     if ( jj != 0 && jj != 32 && jj != 64 && ene[jj-1] != 0. ) ene[jj-1] += -clevel1->estrip[jj][m][l] * ctneigcor[l][m][jjpre];
1761     if ( jj != 31 && jj != 63 && jj != 95 && ene[jj+1] != 0. ) ene[jj+1] += -clevel1->estrip[jj][m][l] * ctneigcor[l][m][jjpre];
1762     } else {
1763     if ( jj != 0 && jj != 32 && jj != 64 && ene[jj-1] != 0. ) ene[jj-1] += -clevel1->estrip[jj][m][l] * 0.01581;
1764     if ( jj != 31 && jj != 63 && jj != 95 && ene[jj+1] != 0. ) ene[jj+1] += -clevel1->estrip[jj][m][l] * 0.01581;
1765 mocchiut 1.1 };
1766 mocchiut 1.22 };
1767     if ( jjj >= 0 && jjj < 96 ){
1768     if ( !ctground ){
1769     if ( jjj%16 == 0 ) jjjpre++;
1770     if ( jjj != 0 && jjj != 32 && jjj != 64 && clevel1->estrip[jjj-1][m][l] != 0. ) clevel1->estrip[jjj-1][m][l] += -ene[jjj] * ctneigcor[l][m][jjjpre];
1771     if ( jjj != 31 && jjj != 63 && jjj != 95 && clevel1->estrip[jjj+1][m][l] !=0. ) clevel1->estrip[jjj+1][m][l] += -ene[jjj] * ctneigcor[l][m][jjjpre];
1772     } else {
1773     if ( jjj != 0 && jjj != 32 && jjj != 64 && clevel1->estrip[jjj-1][m][l] != 0. ) clevel1->estrip[jjj-1][m][l] += -ene[jjj] * 0.01581;
1774     if ( jjj != 31 && jjj != 63 && jjj != 95 && clevel1->estrip[jjj+1][m][l] != 0. ) clevel1->estrip[jjj+1][m][l] += -ene[jjj] * 0.01581;
1775 mocchiut 1.1 };
1776     };
1777 mocchiut 1.22 };
1778     if ( j4 >= 0 && j4 < 96 ){
1779     //
1780     // CALOLEVEL1 CODING AND FILLING
1781     //
1782     //
1783 mocchiut 1.26 // NOTICE: THE FOLLOWING LINE EXCLUDE ALL STRIPS FOR WHICH THE RMS*4 IS GREATER THAN 26 !!! <=============== IMPORTANT! =================> // not true anymore, now it trust parameter files
1784 mocchiut 1.22 //
1785 mocchiut 1.26 if ( obadmask[l][m][j4] == 1 || clevel1->estrip[j4][m][l] <= clevel1->emin || clevel1->estrip[j4][m][l] <= memin[l][m][j4] || calrms[l][m][j4] > maxrms[l][m] || (l==0 && m == 18 && mask18 ) ){
1786 mocchiut 1.22 clevel1->estrip[j4][m][l] = 0.;
1787     };
1788     //
1789 mocchiut 1.28 if ( debug ) printf(" STRIP: view %i plane %i strip %i energy: %f \n",l,m,j4,clevel1->estrip[j4][m][l]);
1790     //
1791 mocchiut 1.22 // code and save the energy for each strip in svstrip
1792     //
1793     if ( clevel1->estrip[j4][m][l] > clevel1->emin ){
1794 mocchiut 1.1 //
1795 mocchiut 1.22 Float_t savel1 = clevel1->estrip[j4][m][l];
1796 mocchiut 1.28 //
1797     if ( m == 18 && l == 0 ){
1798     if ( debug ) printf(" Resetting plane 18X for variable calculation: view %i plane %i strip %i \n",l,m,j4);
1799     clevel1->estrip[j4][m][l] = 0.; // SAVE STRIPS VALUE FOR PLANE 18 X but DO NOT USE IT FOR VARIABLE CALCULATION
1800     };
1801     if ( debug ) printf(" HIT STRIP: view %i plane %i strip %i energy: %f \n",l,m,j4,clevel1->estrip[j4][m][l]);
1802 mocchiut 1.22 // if ( dexyc[l][m][j4] == 32767. ){
1803 mocchiut 1.31 if ( dexyc[l][m][j4] > 32000. || savel1 > 5000.){ // CaloLevel1 bug with plane 18X [9RED 14/04/2010]
1804     if ( savel1 > 5000 ){
1805     if ( debug ) printf(" Absurd plane 18X energy... resetting value to 1100 MIP \n");
1806     savel1 = 1100.; // CaloLevel1 bug with plane 18x [9RED 14/04/2010]
1807     };
1808     savel1 += 5000.;
1809 mocchiut 1.22 clevel2->nsatstrip += 1.;
1810 mocchiut 1.1 };
1811     //
1812 mocchiut 1.22 tim = 100000.;
1813     plo = m;
1814     fbi = 0;
1815     if ( savel1 > 0.99995 ){
1816     tim = 10000.;
1817 mocchiut 1.1 plo = m;
1818 mocchiut 1.22 fbi = 1;
1819     };
1820     if ( savel1 > 9.9995 ){
1821     tim = 1000.;
1822     plo = 22 + m;
1823     fbi = 1;
1824     };
1825     if ( savel1 > 99.995 ){
1826     tim = 100.;
1827     plo = 22 + m;
1828     fbi = 0;
1829     };
1830     if ( savel1 > 999.95 ){
1831     tim = 10.;
1832     plo = 44 + m;
1833     fbi = 0;
1834     };
1835     if ( savel1 > 9999.5 ){
1836     tim = 1.;
1837     plo = 66 + m;
1838 mocchiut 1.1 fbi = 0;
1839 mocchiut 1.22 };
1840     //
1841     cle = (Int_t)lroundf(tim*savel1);
1842     //
1843     if ( l == 0 ){
1844 mocchiut 1.1 //
1845 mocchiut 1.22 // +-PPSSmmmm.mmmm
1846 mocchiut 1.1 //
1847 mocchiut 1.22 svstrip[istrip] = fbi*1000000000 + plo*10000000 + j4*100000 + cle;
1848     } else {
1849     svstrip[istrip] = -(fbi*1000000000 + plo*10000000 + j4*100000 + cle);
1850 mocchiut 1.1 };
1851 mocchiut 1.31 if ( debug ) printf(" svstrip[%i] = %i fbi %i plo %i j4 %i cle %i \n",istrip,svstrip[istrip],fbi,plo,j4,cle);
1852 mocchiut 1.22 //
1853     istrip++;
1854 mocchiut 1.1 };
1855     };
1856     };
1857 mocchiut 1.22 //
1858 mocchiut 1.1 };
1859     };
1860 mocchiut 1.22 //
1861     // store goodness flag
1862     //
1863 mocchiut 1.1 if ( !pe ){
1864     clevel2->good = 1;
1865     } else {
1866     clevel2->good = 0;
1867     };
1868 mocchiut 1.22 //
1869     // done
1870     //
1871 mocchiut 1.1 return(0);
1872     }
1873    
1874     void CaloLevel0::GetTrkVar(){
1875     calol2tr();
1876     }
1877    
1878     void CaloLevel0::FillTrkVar(CaloLevel2 *ca, Int_t nutrk){
1879     //
1880     CaloTrkVar *t_ca = new CaloTrkVar();
1881     //
1882     t_ca->trkseqno = trkseqno;
1883     t_ca->ncore = (Int_t)clevel2->ncore;
1884     t_ca->qcore = clevel2->qcore;
1885     t_ca->noint = (Int_t)clevel2->noint;
1886     t_ca->ncyl = (Int_t)clevel2->ncyl;
1887     t_ca->qcyl = clevel2->qcyl;
1888     t_ca->qtrack = clevel2->qtrack;
1889     t_ca->qtrackx = clevel2->qtrackx;
1890     t_ca->qtracky = clevel2->qtracky;
1891     t_ca->dxtrack = clevel2->dxtrack;
1892     t_ca->dytrack = clevel2->dytrack;
1893     t_ca->qlast = clevel2->qlast;
1894     t_ca->nlast = (Int_t)clevel2->nlast;
1895     t_ca->qpre = clevel2->qpre;
1896     t_ca->npre = (Int_t)clevel2->npre;
1897     t_ca->qpresh = clevel2->qpresh;
1898     t_ca->npresh = (Int_t)clevel2->npresh;
1899     t_ca->qtr = clevel2->qtr;
1900     t_ca->ntr = (Int_t)clevel2->ntr;
1901     t_ca->planetot = (Int_t)clevel2->planetot;
1902     t_ca->qmean = clevel2->qmean;
1903     t_ca->dX0l = clevel2->dX0l;
1904     t_ca->qlow = clevel2->qlow;
1905     t_ca->nlow = (Int_t)clevel2->nlow;
1906     //
1907     if ( trkseqno == -1 ){
1908     // ca->impx = clevel2->impx;
1909     // ca->impy = clevel2->impy;
1910     ca->tanx[1] = clevel2->tanx;
1911     ca->tany[1] = clevel2->tany;
1912     ca->elen = clevel2->elen;
1913     ca->selen = clevel2->selen;
1914     // memcpy(ca->cibar,clevel2->cibar,sizeof(clevel2->cibar));
1915     // memcpy(ca->cbar,clevel2->cbar,sizeof(clevel2->cbar));
1916     memcpy(t_ca->tibar,clevel2->cibar,sizeof(clevel2->cibar));
1917     memcpy(t_ca->tbar,clevel2->cbar,sizeof(clevel2->cbar));
1918     memcpy(ca->planemax,clevel2->planemax,sizeof(clevel2->planemax));
1919 mocchiut 1.6 memcpy(ca->selfdelay,clevel2->selfdelay,sizeof(clevel2->selfdelay));
1920 mocchiut 1.1 ca->varcfit[2] = clevel2->varcfit[0];
1921     ca->varcfit[3] = clevel2->varcfit[1];
1922     ca->npcfit[2] = clevel2->npcfit[0];
1923     ca->npcfit[3] = clevel2->npcfit[1];
1924     // memcpy(ca->varcfit,clevel2->varcfit,sizeof(clevel2->varcfit));
1925     // memcpy(ca->npcfit,clevel2->npcfit,sizeof(clevel2->npcfit));
1926     } else {
1927     memcpy(t_ca->tibar,clevel2->tibar,sizeof(clevel2->tibar));
1928     memcpy(t_ca->tbar,clevel2->tbar,sizeof(clevel2->tbar));
1929     };
1930     //
1931     //
1932     if ( !(ca->CaloTrk) ) ca->CaloTrk = new TClonesArray("CaloTrkVar",1); //ELENA
1933     TClonesArray &t = *ca->CaloTrk;
1934     new(t[nutrk]) CaloTrkVar(*t_ca);
1935     //
1936     delete t_ca;
1937     //
1938     ClearTrkVar();
1939     }
1940    
1941     void CaloLevel0::GetCommonVar(){
1942     calol2cm();
1943     }
1944    
1945     void CaloLevel0::FillCommonVar(CaloLevel1 *c1, CaloLevel2 *ca){
1946     //
1947     ca->good = clevel2->good;
1948 mocchiut 1.26 // if ( clevel2->trigty == 2. ){
1949     // ca->selftrigger = 1;
1950     // } else {
1951     // ca->selftrigger = 0;
1952     // };
1953 mocchiut 1.1 //
1954 mocchiut 1.26 ca->selftrigger = (Int_t)clevel2->trigty + (Int_t)clevel2->wartrig;
1955 mocchiut 1.1 //
1956     memcpy(ca->perr,clevel2->perr,sizeof(clevel2->perr));
1957     memcpy(ca->swerr,clevel2->swerr,sizeof(clevel2->swerr));
1958     memcpy(ca->crc,clevel2->crc,sizeof(clevel2->crc));
1959     ca->nstrip = (Int_t)clevel2->nstrip;
1960 mocchiut 1.13 ca->nsatstrip = (Int_t)clevel2->nsatstrip;
1961 mocchiut 1.1 ca->qtot = clevel2->qtot;
1962     // ca->impx = clevel2->impx;
1963     // ca->impy = clevel2->impy;
1964     ca->tanx[0] = clevel2->tanx;
1965     ca->tany[0] = clevel2->tany;
1966     ca->nx22 = (Int_t)clevel2->nx22;
1967     ca->qx22 = clevel2->qx22;
1968     ca->qmax = clevel2->qmax;
1969     ca->elen = clevel2->elen;
1970     ca->selen = clevel2->selen;
1971     memcpy(ca->qq,clevel2->qq,sizeof(clevel2->qq));
1972     memcpy(ca->planemax,clevel2->planemax,sizeof(clevel2->planemax));
1973 mocchiut 1.6 memcpy(ca->selfdelay,clevel2->selfdelay,sizeof(clevel2->selfdelay));
1974 mocchiut 1.1 ca->varcfit[0] = clevel2->varcfit[0];
1975     ca->varcfit[1] = clevel2->varcfit[1];
1976     ca->npcfit[0] = clevel2->npcfit[0];
1977     ca->npcfit[1] = clevel2->npcfit[1];
1978     ca->fitmode[0] = clevel2->fmode[0];
1979     ca->fitmode[1] = clevel2->fmode[1];
1980     // memcpy(ca->varcfit,clevel2->varcfit,sizeof(clevel2->varcfit));
1981     // memcpy(ca->npcfit,clevel2->npcfit,sizeof(clevel2->npcfit));
1982     memcpy(ca->cibar,clevel2->cibar,sizeof(clevel2->cibar));
1983     memcpy(ca->cbar,clevel2->cbar,sizeof(clevel2->cbar));
1984     //
1985     if ( c1 ){
1986     c1->istrip = istrip;
1987     c1->estrip = TArrayI(istrip,svstrip);
1988     };
1989     //
1990     }
1991    
1992     void CaloLevel0::ClearStructs(){
1993     ClearTrkVar();
1994     ClearCommonVar();
1995     }
1996    
1997 mocchiut 1.22 void CaloLevel0::Delete(Option_t *t){
1998     if ( de ) delete de;
1999     delete this;
2000     }
2001    
2002    
2003 mocchiut 1.1 void CaloLevel0::RunClose(){
2004     l0tr->Delete();
2005     ClearStructs();
2006     //
2007     memset(dexy, 0, 2*22*96*sizeof(Float_t));
2008     memset(dexyc, 0, 2*22*96*sizeof(Float_t));
2009     memset(base, 0, 2*22*6*sizeof(Float_t));
2010     memset(sbase, 0, 2*22*6*sizeof(Float_t));
2011 mocchiut 1.9 memset(ctprecor, 0, 2*22*6*sizeof(Float_t));
2012 mocchiut 1.22 memset(ctsicor, 0, 2*22*9*sizeof(Float_t));
2013 mocchiut 1.9 memset(ctneigcor, 0, 2*22*6*sizeof(Float_t));
2014 mocchiut 1.1 //
2015     }
2016    
2017     //
2018     // Private methods
2019     //
2020    
2021     void CaloLevel0::ClearTrkVar(){
2022     clevel2->ncore = 0;
2023     clevel2->qcore = 0.;
2024     clevel2->noint = 0.;
2025     clevel2->ncyl = 0.;
2026     clevel2->qcyl = 0.;
2027     clevel2->qtrack = 0.;
2028     clevel2->qtrackx = 0.;
2029     clevel2->qtracky = 0.;
2030     clevel2->dxtrack = 0.;
2031     clevel2->dytrack = 0.;
2032     clevel2->qlast = 0.;
2033     clevel2->nlast = 0.;
2034     clevel2->qpre = 0.;
2035     clevel2->npre = 0.;
2036     clevel2->qpresh = 0.;
2037     clevel2->npresh = 0.;
2038     clevel2->qlow = 0.;
2039     clevel2->nlow = 0.;
2040     clevel2->qtr = 0.;
2041     clevel2->ntr = 0.;
2042     clevel2->planetot = 0.;
2043     clevel2->qmean = 0.;
2044     clevel2->dX0l = 0.;
2045     clevel2->elen = 0.;
2046     clevel2->selen = 0.;
2047     memset(clevel1->al_p, 0, 5*2*sizeof(Double_t));
2048     memset(clevel2->tibar, 0, 2*22*sizeof(Int_t));
2049     memset(clevel2->tbar, 0, 2*22*sizeof(Float_t));
2050     }
2051    
2052     void CaloLevel0::ClearCommonVar(){
2053     istrip = 0;
2054     clevel2->trigty = -1.;
2055     clevel2->wartrig = 0.;
2056     clevel2->good = 0;
2057     clevel2->nstrip = 0.;
2058 mocchiut 1.13 clevel2->nsatstrip = 0.;
2059 mocchiut 1.1 clevel2->qtot = 0.;
2060     // clevel2->impx = 0.;
2061     // clevel2->impy = 0.;
2062     clevel2->tanx = 0.; // this is correct since it refers to the fortran structure
2063     clevel2->tany = 0.; // this is correct since it refers to the fortran structure
2064     clevel2->qmax = 0.;
2065     clevel2->nx22 = 0.;
2066     clevel2->qx22 = 0.;
2067     memset(clevel2->perr, 0, 4*sizeof(Int_t));
2068     memset(clevel2->swerr, 0, 4*sizeof(Int_t));
2069     memset(clevel2->crc, 0, 4*sizeof(Int_t));
2070     memset(clevel2->qq, 0, 4*sizeof(Int_t));
2071     memset(clevel2->varcfit, 0, 4*sizeof(Float_t));
2072     memset(clevel2->npcfit, 0, 4*sizeof(Int_t));
2073     memset(clevel2->planemax, 0, 2*sizeof(Int_t));
2074 mocchiut 1.6 memset(clevel2->selfdelay, 0, 4*7*sizeof(Int_t));
2075 mocchiut 1.1 memset(clevel2->fmode, 0, 2*sizeof(Int_t));
2076     memset(clevel2->cibar, 0, 2*22*sizeof(Int_t));
2077     memset(clevel2->cbar, 0, 2*22*sizeof(Float_t));
2078     }
2079    
2080     void CaloLevel0::ClearCalibVals(Int_t s){
2081     //
2082     for ( Int_t d=0 ; d<11 ;d++ ){
2083     Int_t pre = -1;
2084     for ( Int_t j=0; j<96 ;j++){
2085     if ( j%16 == 0 ) pre++;
2086     if ( s == 2 ){
2087     calped[0][2*d+1][j] = 0.;
2088     cstwerr[3] = 0.;
2089     cperror[3] = 0.;
2090     calgood[0][2*d+1][j] = 0.;
2091     calthr[0][2*d+1][pre] = 0.;
2092     calrms[0][2*d+1][j] = 0.;
2093     calbase[0][2*d+1][pre] = 0.;
2094     calvar[0][2*d+1][pre] = 0.;
2095     };
2096     if ( s == 3 ){
2097     calped[0][2*d][j] = 0.;
2098     cstwerr[1] = 0.;
2099     cperror[1] = 0.;
2100     calgood[0][2*d][j] = 0.;
2101     calthr[0][2*d][pre] = 0.;
2102     calrms[0][2*d][j] = 0.;
2103     calbase[0][2*d][pre] = 0.;
2104     calvar[0][2*d][pre] = 0.;
2105     };
2106     if ( s == 0 ){
2107     calped[1][2*d][j] = 0.;
2108     cstwerr[0] = 0.;
2109     cperror[0] = 0.;
2110     calgood[1][2*d][j] = 0.;
2111     calthr[1][2*d][pre] = 0.;
2112     calrms[1][2*d][j] = 0.;
2113     calbase[1][2*d][pre] = 0.;
2114     calvar[1][2*d][pre] = 0.;
2115     };
2116     if ( s == 1 ){
2117     calped[1][2*d+1][j] = 0.;
2118     cstwerr[2] = 0.;
2119     cperror[2] = 0.;
2120     calgood[1][2*d+1][j] = 0.;
2121     calthr[1][2*d+1][pre] = 0.;
2122     calrms[1][2*d+1][j] = 0.;
2123     calbase[1][2*d+1][pre] = 0.;
2124     calvar[1][2*d+1][pre] = 0.;
2125     };
2126     };
2127     };
2128     return;
2129     }
2130    
2131 mocchiut 1.7 Int_t CaloLevel0::Update(GL_TABLES *glt, UInt_t atime, Int_t s){
2132 mocchiut 1.1 //
2133 mocchiut 1.7 const TString host = glt->CGetHost();
2134     const TString user = glt->CGetUser();
2135     const TString psw = glt->CGetPsw();
2136     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
2137     if ( !dbc->IsConnected() ) throw -116;
2138 mocchiut 1.8 stringstream myquery;
2139     myquery.str("");
2140     myquery << "SET time_zone='+0:00'";
2141     dbc->Query(myquery.str().c_str());
2142 mocchiut 1.1 Int_t sgnl = 0;
2143     //
2144     GL_CALO_CALIB *glcalo = new GL_CALO_CALIB();
2145     //
2146     sgnl = 0;
2147     //
2148     idcalib[s] = 0;
2149     fromtime[s] = 0;
2150     totime[s] = 0;
2151     calibno[s] = 0;
2152     ClearCalibVals(s);
2153     //
2154     UInt_t uptime = 0;
2155     //
2156     sgnl = glcalo->Query_GL_CALO_CALIB(atime,uptime,s,dbc);
2157     if ( sgnl < 0 ){
2158     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
2159     return(sgnl);
2160     };
2161     //
2162     idcalib[s] = glcalo->ID_ROOT_L0;
2163     fromtime[s] = glcalo->FROM_TIME;
2164     if ( glcalo->TO_TIME < atime ){ // calibration is corrupted and we are using the one that preceed the good one
2165     totime[s] = uptime;
2166     } else {
2167     totime[s] = glcalo->TO_TIME;
2168     };
2169     // totime[s] = glcalo->TO_TIME;
2170     calibno[s] = glcalo->EV_ROOT;
2171     //
2172     if ( totime[s] == 0 ){
2173     if ( verbose ) printf(" CALORIMETER - WARNING: data with no associated calibration\n");
2174     ClearCalibVals(s);
2175     sgnl = 100;
2176     };
2177     //
2178     // determine path and name and entry of the calibration file
2179     //
2180     GL_ROOT *glroot = new GL_ROOT();
2181     if ( verbose ) printf("\n");
2182     if ( verbose ) printf(" ** SECTION %i **\n",s);
2183     //
2184     sgnl = glroot->Query_GL_ROOT(idcalib[s],dbc);
2185     if ( sgnl < 0 ){
2186     if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
2187     return(sgnl);
2188     };
2189     //
2190     stringstream name;
2191     name.str("");
2192     name << glroot->PATH.Data() << "/";
2193     name << glroot->NAME.Data();
2194     //
2195     fcalname[s] = (TString)name.str().c_str();
2196     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]);
2197     //
2198     sgnl = LoadCalib(s);
2199     //
2200     if ( sgnl != 0 ) return(sgnl);
2201     delete glcalo;
2202     delete glroot;
2203     //
2204     return(0);
2205     //
2206     }
2207    
2208     Int_t CaloLevel0::LoadCalib(Int_t s){
2209     //
2210     ifstream myfile;
2211     myfile.open(fcalname[s].Data());
2212     if ( !myfile ){
2213     return(-107);
2214     };
2215     myfile.close();
2216     //
2217     TFile *File = new TFile(fcalname[s].Data());
2218     if ( !File ) return(-108);
2219     TTree *tr = (TTree*)File->Get("CalibCalPed");
2220     if ( !tr ) return(-109);
2221     //
2222     TBranch *calo = tr->GetBranch("CalibCalPed");
2223     //
2224     pamela::CalibCalPedEvent *ce = 0;
2225     tr->SetBranchAddress("CalibCalPed", &ce);
2226     //
2227     Long64_t ncalibs = calo->GetEntries();
2228     //
2229     if ( !ncalibs ) return(-110);
2230     //
2231 mocchiut 1.30 if ( calo->GetEntry(calibno[s]) <= 0 ) throw -36;
2232 mocchiut 1.1 //
2233     if (ce->cstwerr[s] != 0 && ce->cperror[s] == 0 ) {
2234     for ( Int_t d=0 ; d<11 ;d++ ){
2235     Int_t pre = -1;
2236     for ( Int_t j=0; j<96 ;j++){
2237     if ( j%16 == 0 ) pre++;
2238     if ( s == 2 ){
2239     calped[0][2*d+1][j] = ce->calped[3][d][j];
2240     cstwerr[3] = ce->cstwerr[3];
2241     cperror[3] = ce->cperror[3];
2242     calgood[0][2*d+1][j] = ce->calgood[3][d][j];
2243     calthr[0][2*d+1][pre] = ce->calthr[3][d][pre];
2244     calrms[0][2*d+1][j] = ce->calrms[3][d][j];
2245     calbase[0][2*d+1][pre] = ce->calbase[3][d][pre];
2246     calvar[0][2*d+1][pre] = ce->calvar[3][d][pre];
2247     };
2248     if ( s == 3 ){
2249     calped[0][2*d][j] = ce->calped[1][d][j];
2250     cstwerr[1] = ce->cstwerr[1];
2251     cperror[1] = ce->cperror[1];
2252     calgood[0][2*d][j] = ce->calgood[1][d][j];
2253     calthr[0][2*d][pre] = ce->calthr[1][d][pre];
2254     calrms[0][2*d][j] = ce->calrms[1][d][j];
2255     calbase[0][2*d][pre] = ce->calbase[1][d][pre];
2256     calvar[0][2*d][pre] = ce->calvar[1][d][pre];
2257     };
2258     if ( s == 0 ){
2259     calped[1][2*d][j] = ce->calped[0][d][j];
2260     cstwerr[0] = ce->cstwerr[0];
2261     cperror[0] = ce->cperror[0];
2262     calgood[1][2*d][j] = ce->calgood[0][d][j];
2263     calthr[1][2*d][pre] = ce->calthr[0][d][pre];
2264     calrms[1][2*d][j] = ce->calrms[0][d][j];
2265     calbase[1][2*d][pre] = ce->calbase[0][d][pre];
2266     calvar[1][2*d][pre] = ce->calvar[0][d][pre];
2267     };
2268     if ( s == 1 ){
2269     calped[1][2*d+1][j] = ce->calped[2][d][j];
2270     cstwerr[2] = ce->cstwerr[2];
2271     cperror[2] = ce->cperror[2];
2272     calgood[1][2*d+1][j] = ce->calgood[2][d][j];
2273     calthr[1][2*d+1][pre] = ce->calthr[2][d][pre];
2274     calrms[1][2*d+1][j] = ce->calrms[2][d][j];
2275     calbase[1][2*d+1][pre] = ce->calbase[2][d][pre];
2276     calvar[1][2*d+1][pre] = ce->calvar[2][d][pre];
2277     };
2278     };
2279     };
2280     } else {
2281     if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");
2282     return(-111);
2283     };
2284     File->Close();
2285     return(0);
2286     }

  ViewVC Help
Powered by ViewVC 1.1.23