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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.19 by mocchiut, Thu Apr 10 11:41:47 2008 UTC revision 1.25 by mocchiut, Fri Dec 12 16:18:17 2008 UTC
# Line 56  CaloLevel0::CaloLevel0(){ Line 56  CaloLevel0::CaloLevel0(){
56    clevel1 = &clevel1_;    clevel1 = &clevel1_;
57    clevel2 = &clevel2_;    clevel2 = &clevel2_;
58    //    //
59    //  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    trkseqno = 0;    trkseqno = 0;
85    ClearStructs();    ClearStructs();
86    //    //
# Line 67  CaloLevel0::CaloLevel0(){ Line 92  CaloLevel0::CaloLevel0(){
92    memset(obadmask, 0, 2*22*96*sizeof(Int_t));    memset(obadmask, 0, 2*22*96*sizeof(Int_t));
93    memset(obadpulsemask, 0, 2*22*6*sizeof(Int_t));    memset(obadpulsemask, 0, 2*22*6*sizeof(Int_t));
94    memset(ctprecor, 0, 2*22*6*sizeof(Float_t));    memset(ctprecor, 0, 2*22*6*sizeof(Float_t));
95      memset(ctsicor, 0, 2*22*9*sizeof(Float_t));
96    memset(ctneigcor, 0, 2*22*6*sizeof(Float_t));    memset(ctneigcor, 0, 2*22*6*sizeof(Float_t));
97    calopar1 = true;    calopar1 = true;
98    calopar2 = true;    calopar2 = true;
99    calopar3 = true;    calopar3 = true;
100      calopar4 = true;
101      calopar5 = true;
102    crosst = true;    crosst = true;
103    ftcalopar1 = 0;    ftcalopar1 = 0;
104    ttcalopar1 = 0;    ttcalopar1 = 0;
# Line 78  CaloLevel0::CaloLevel0(){ Line 106  CaloLevel0::CaloLevel0(){
106    ttcalopar2 = 0;    ttcalopar2 = 0;
107    ftcalopar3 = 0;    ftcalopar3 = 0;
108    ttcalopar3 = 0;    ttcalopar3 = 0;
109      ftcalopar4 = 0;
110      ttcalopar4 = 0;
111      ftcalopar5 = 0;
112      ttcalopar5 = 0;
113  }  }
114    
115  void CaloLevel0::SetCrossTalk(Bool_t ct){  void CaloLevel0::SetCrossTalk(Bool_t ct){
# Line 88  void CaloLevel0::SetCrossTalkType(Bool_t Line 120  void CaloLevel0::SetCrossTalkType(Bool_t
120    ctground = ct;    ctground = ct;
121  }  }
122    
123    void CaloLevel0::SetCrossTalkType(Int_t ct){
124      if ( ct == 0 ) ctground = true;
125      if ( ct == 1 ){
126        ctground = false;
127        noselfct = false;
128      };
129      if ( ct == 2 ){
130        ctground = false;
131        noselfct = true;
132      };
133    }
134    
135  void CaloLevel0::SetVerbose(Bool_t ct){  void CaloLevel0::SetVerbose(Bool_t ct){
136    verbose = ct;    verbose = ct;
137  }  }
# Line 95  void CaloLevel0::SetVerbose(Bool_t ct){ Line 139  void CaloLevel0::SetVerbose(Bool_t ct){
139  /**  /**
140   * Initialize CaloLevel0 object   * Initialize CaloLevel0 object
141  **/  **/
142    void CaloLevel0::ProcessingInit(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
143      if ( !dbc->IsConnected() ) throw -116;  
144      this->InitDo(dbc,hs,sgnl,l0tree,isdeb,isverb);
145    }
146    
147    /**
148     * Initialize CaloLevel0 object
149    **/
150  void CaloLevel0::ProcessingInit(GL_TABLES *glt, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){  void CaloLevel0::ProcessingInit(GL_TABLES *glt, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
151    //    //
152    const TString host = glt->CGetHost();    const TString host = glt->CGetHost();
# Line 102  void CaloLevel0::ProcessingInit(GL_TABLE Line 154  void CaloLevel0::ProcessingInit(GL_TABLE
154    const TString psw = glt->CGetPsw();    const TString psw = glt->CGetPsw();
155    TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());    TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
156    if ( !dbc->IsConnected() ) throw -116;      if ( !dbc->IsConnected() ) throw -116;  
157      this->InitDo(dbc,hs,sgnl,l0tree,isdeb,isverb);
158      dbc->Close();
159      delete dbc;
160    }
161    
162    
163    void CaloLevel0::InitDo(TSQLServer *dbc, UInt_t hs, Int_t &sgnl, TTree *l0tree, Bool_t isdeb, Bool_t isverb){
164    stringstream myquery;    stringstream myquery;
165    myquery.str("");    myquery.str("");
166    myquery << "SET time_zone='+0:00'";    myquery << "SET time_zone='+0:00'";
# Line 182  void CaloLevel0::ProcessingInit(GL_TABLE Line 241  void CaloLevel0::ProcessingInit(GL_TABLE
241    //    //
242    delete glcalo;    delete glcalo;
243    delete glroot;    delete glroot;
   dbc->Close();  
   delete dbc;  
244    //    //
245    return;    return;
246    //    //
# Line 200  Int_t CaloLevel0::ChkCalib(GL_TABLES *gl Line 257  Int_t CaloLevel0::ChkCalib(GL_TABLES *gl
257    return(sgnl);    return(sgnl);
258  }  }
259    
260    Int_t CaloLevel0::ChkParam(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){
261      Int_t sig = this->ChkParamDo(dbc,runheader,mechal);
262      return(sig);
263    }
264    
265  Int_t CaloLevel0::ChkParam(GL_TABLES *glt, UInt_t runheader, Bool_t mechal){  Int_t CaloLevel0::ChkParam(GL_TABLES *glt, UInt_t runheader, Bool_t mechal){
266    const TString host = glt->CGetHost();    const TString host = glt->CGetHost();
267    const TString user = glt->CGetUser();    const TString user = glt->CGetUser();
# Line 211  Int_t CaloLevel0::ChkParam(GL_TABLES *gl Line 273  Int_t CaloLevel0::ChkParam(GL_TABLES *gl
273    myquery << "SET time_zone='+0:00'";    myquery << "SET time_zone='+0:00'";
274    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
275    //    //
276      Int_t sig = this->ChkParamDo(dbc,runheader,mechal);
277      dbc->Close();
278      delete dbc;
279      return(sig);
280    }
281    
282    Int_t CaloLevel0::ChkParamDo(TSQLServer *dbc, UInt_t runheader, Bool_t mechal){
283      //
284    stringstream calfile;    stringstream calfile;
285    stringstream bmfile;    stringstream bmfile;
286    stringstream aligfile;    stringstream aligfile;
# Line 221  Int_t CaloLevel0::ChkParam(GL_TABLES *gl Line 291  Int_t CaloLevel0::ChkParam(GL_TABLES *gl
291    //    //
292    if ( calopar1 || ( ttcalopar1 != 0 && ttcalopar1 < runheader ) ){    if ( calopar1 || ( ttcalopar1 != 0 && ttcalopar1 < runheader ) ){
293      //      //
     //  
     //  
294      if ( debug ) printf(" calopar1 %i ftcalopar1 %u ttcalopar1 %u runheader %u \n",calopar1,ftcalopar1,ttcalopar1,runheader);      if ( debug ) printf(" calopar1 %i ftcalopar1 %u ttcalopar1 %u runheader %u \n",calopar1,ftcalopar1,ttcalopar1,runheader);
295      //      //
296        if ( calopar1 ){
297          //
298          // determine where I can find calorimeter ADC to MIP conversion file  
299          //
300          if ( verbose ) printf(" Querying DB for calorimeter parameters files...\n");
301          //
302          error = 0;
303          error = glparam->Query_GL_PARAM(runheader,101,dbc);
304          if ( error < 0 ) return(error);
305          //
306          calfile.str("");
307          calfile << glparam->PATH.Data() << "/";
308          calfile << glparam->NAME.Data();
309          //
310          if ( verbose ) printf("\n Using ADC to MIP conversion file: \n %s \n",calfile.str().c_str());
311          f = fopen(calfile.str().c_str(),"rb");
312          if ( !f ){
313            if ( verbose ) printf(" CALORIMETER - ERROR: no ADC to MIP file!\n");
314            return(-105);
315          };
316          //
317          for (Int_t m = 0; m < 2 ; m++ ){
318            for (Int_t k = 0; k < 22; k++ ){
319              for (Int_t l = 0; l < 96; l++ ){
320                fread(&mip[m][k][l],sizeof(mip[m][k][l]),1,f);
321                if ( debug ) printf(" %f \n",mip[m][k][l]);
322              };
323            };
324          };
325          fclose(f);      
326        };
327        //
328      calopar1 = false;      calopar1 = false;
329      //      //
330      // determine where I can find calorimeter ADC to MIP conversion file        // flight extra corrections:
331      //      //
332      if ( verbose ) printf(" Querying DB for calorimeter parameters files...\n");      if ( verbose ) printf(" Querying DB for calorimeter flight ADC to MIP files...\n");
333      //      //
334      error = 0;      error = 0;
335      error = glparam->Query_GL_PARAM(runheader,101,dbc);      error = glparam->Query_GL_PARAM(runheader,110,dbc);
336      if ( error < 0 ) return(error);      if ( error < 0 ) return(error);
337      //      //
338      calfile.str("");      calfile.str("");
# Line 241  Int_t CaloLevel0::ChkParam(GL_TABLES *gl Line 341  Int_t CaloLevel0::ChkParam(GL_TABLES *gl
341      ftcalopar1 = glparam->FROM_TIME;      ftcalopar1 = glparam->FROM_TIME;
342      ttcalopar1 = glparam->TO_TIME;      ttcalopar1 = glparam->TO_TIME;
343      //      //
344      if ( verbose ) printf("\n Using ADC to MIP conversion file: \n %s \n",calfile.str().c_str());      if ( verbose ) printf("\n Using ADC to MIP special conversion file: \n %s \n",calfile.str().c_str());
345      f = fopen(calfile.str().c_str(),"rb");      ifstream spfile;
346      if ( !f ){      spfile.open(calfile.str().c_str());
347        if ( verbose ) printf(" CALORIMETER - ERROR: no ADC to MIP file!\n");      if ( !spfile ){
348        return(-105);        if ( verbose ) printf(" CALORIMETER - ERROR: no special calibration file!\n");
349          return(-123);
350      };      };
351        //  
352        Int_t vview = 0;
353        Int_t vplane = 0;
354        Int_t vstrip = 0;
355        Float_t vval = 0.;
356        while ( spfile >> vview && spfile >> vplane && spfile >> vstrip && spfile >> vval){
357          if ( debug ) printf(" Setting ADC to MIP conversion factor: view %i plane %i strip %i mip %f  \n",vview,vplane,vstrip,vval);
358          mip[vview][vplane][vstrip] = vval;
359        };
360      //      //
     for (Int_t m = 0; m < 2 ; m++ ){  
       for (Int_t k = 0; k < 22; k++ ){  
         for (Int_t l = 0; l < 96; l++ ){  
           fread(&mip[m][k][l],sizeof(mip[m][k][l]),1,f);  
           if ( debug ) printf(" %f \n",mip[m][k][l]);  
         };  
       };  
     };  
     fclose(f);  
361    };    };
362    //    //
363      //
364    if ( calopar2 || ( ttcalopar2 != 0 && ttcalopar2 < runheader ) ){    if ( calopar2 || ( ttcalopar2 != 0 && ttcalopar2 < runheader ) ){
365        //
366      if ( debug ) printf(" calopar2 %i ftcalopar2 %u ttcalopar2 %u runheader %u \n",calopar2,ftcalopar2,ttcalopar2,runheader);      if ( debug ) printf(" calopar2 %i ftcalopar2 %u ttcalopar2 %u runheader %u \n",calopar2,ftcalopar2,ttcalopar2,runheader);
367      calopar2 = false;      calopar2 = false;
368      //      //
# Line 366  Int_t CaloLevel0::ChkParam(GL_TABLES *gl Line 469  Int_t CaloLevel0::ChkParam(GL_TABLES *gl
469      badfile.close();      badfile.close();
470    };    };
471    //    //
472      // calopar4
473      //
474      if ( calopar4 || ( ttcalopar4 != 0 && ttcalopar4 < runheader ) ){
475        //
476        if ( debug ) printf(" calopar4 %i ftcalopar4 %u ttcalopar4 %u runheader %u \n",calopar4,ftcalopar4,ttcalopar4,runheader);
477        //
478        calopar4 = false;
479        //
480        // flight extra corrections:
481        //
482        if ( verbose ) printf(" Querying DB for calorimeter max rms file...\n");
483        //
484        error = 0;
485        error = glparam->Query_GL_PARAM(runheader,109,dbc);
486        if ( error < 0 ) return(error);
487        //
488        calfile.str("");
489        calfile << glparam->PATH.Data() << "/";
490        calfile << glparam->NAME.Data();
491        ftcalopar4 = glparam->FROM_TIME;
492        ttcalopar4 = glparam->TO_TIME;
493        //
494        if ( verbose ) printf("\n Using calorimeter max rms file: \n %s \n",calfile.str().c_str());
495        ifstream spfile;
496        spfile.open(calfile.str().c_str());
497        if ( !spfile ){
498          if ( verbose ) printf(" CALORIMETER - ERROR: no max rms file!\n");
499          return(-124);
500        };
501        //  
502        Int_t vview = 0;
503        Int_t vplane = 0;
504        Int_t vval = 0;
505        for (Int_t l=0; l<2; l++){
506          for (Int_t m=0; m<22; m++){
507            maxrms[l][m] = 26;
508          };
509        };
510        while ( spfile >> vview && spfile >> vplane && spfile >> vval){
511          if ( debug ) printf(" Setting view %i plane %i max rms %i  \n",vview,vplane,vval);
512          maxrms[vview][vplane] = vval;
513        };
514        spfile.close();
515        //
516      };
517      //
518      // calopar5
519      //
520      if ( calopar5 || ( ttcalopar5 != 0 && ttcalopar5 < runheader ) ){
521        //
522        if ( debug ) printf(" calopar5 %i ftcalopar5 %u ttcalopar5 %u runheader %u \n",calopar5,ftcalopar5,ttcalopar5,runheader);
523        //
524        calopar5 = false;
525        //
526        // flight extra corrections:
527        //
528        if ( verbose ) printf(" Querying DB for calorimeter noise to signal threshold file...\n");
529        //
530        error = 0;
531        error = glparam->Query_GL_PARAM(runheader,111,dbc);
532        if ( error < 0 ) return(error);
533        //
534        calfile.str("");
535        calfile << glparam->PATH.Data() << "/";
536        calfile << glparam->NAME.Data();
537        ftcalopar5 = glparam->FROM_TIME;
538        ttcalopar5 = glparam->TO_TIME;
539        //
540        if ( verbose ) printf("\n Using calorimeter noise to signal threshold file: \n %s \n",calfile.str().c_str());
541        ifstream spfile;
542        spfile.open(calfile.str().c_str());
543        if ( !spfile ){
544          if ( verbose ) printf(" CALORIMETER - ERROR: no noise to signal threshold file!\n");
545          return(-125);
546        };
547        //  
548        Int_t vview = 0;
549        Int_t vplane = 0;
550        Int_t vstrip = 0;
551        Float_t vval = 0.;
552        for (Int_t l=0; l<2; l++){
553          for (Int_t m=0; m<22; m++){
554            for (Int_t n=0; n<96; n++){
555              memin[l][m][n] = 0.7;
556            };
557          };
558        };
559        while ( spfile >> vview && spfile >> vplane && spfile >> vstrip && spfile >> vval){
560          if ( vstrip == -1 ){
561            for (Int_t ll=0; ll<96; ll++){
562              if ( debug ) printf(" Setting view %i plane %i strip %i noise to signal ratio %f  \n",vview,vplane,ll,vval);
563              memin[vview][vplane][ll] = vval;
564            };
565          } else {
566            if ( debug ) printf(" Setting view %i plane %i strip %i noise to signal ratio %f  \n",vview,vplane,vstrip,vval);
567            memin[vview][vplane][vstrip] = vval;
568          };
569        };
570        spfile.close();
571        //
572      };
573      //
574      //
575    delete glparam;    delete glparam;
   dbc->Close();  
   delete dbc;  
576    //    //
577    return(0);    return(0);
578  }  }
579    
580  Int_t CaloLevel0::CalcCrossTalkCorr(GL_TABLES *glt, UInt_t runheader){  Int_t CaloLevel0::CalcCrossTalkCorr(TSQLServer *dbc, UInt_t runheader, Bool_t ctusetable){
581      Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,ctusetable);
582      return(sig);  
583    };
584    
585    Int_t CaloLevel0::CalcCrossTalkCorr(TSQLServer *dbc, UInt_t runheader){
586      Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,true);
587      return(sig);
588    }
589    
590    Int_t CaloLevel0::CalcCrossTalkCorr(GL_TABLES *glt, UInt_t runheader, Bool_t usetable){
591      const TString host = glt->CGetHost();
592      const TString user = glt->CGetUser();
593      const TString psw = glt->CGetPsw();
594      TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
595      if ( !dbc->IsConnected() ) throw -116;
596      stringstream myquery;
597      myquery.str("");
598      myquery << "SET time_zone='+0:00'";
599      dbc->Query(myquery.str().c_str());
600    //    //
601    if ( ctground ) return(0);    Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,usetable);
602      dbc->Close();
603      delete dbc;
604    //    //
605      return(sig);
606      //  
607    };
608    
609    Int_t CaloLevel0::CalcCrossTalkCorr(GL_TABLES *glt, UInt_t runheader){
610    const TString host = glt->CGetHost();    const TString host = glt->CGetHost();
611    const TString user = glt->CGetUser();    const TString user = glt->CGetUser();
612    const TString psw = glt->CGetPsw();    const TString psw = glt->CGetPsw();
# Line 387  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T Line 617  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T
617    myquery << "SET time_zone='+0:00'";    myquery << "SET time_zone='+0:00'";
618    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
619    //    //
620    stringstream bmfile;    Int_t sig = CalcCrossTalkCorrDo(dbc,runheader,true);
621      dbc->Close();
622      delete dbc;
623      //
624      return(sig);
625      //
626    }
627    
628    Int_t CaloLevel0::CalcCrossTalkCorrDo(TSQLServer *dbc, UInt_t runheader, Bool_t usetable){
629      //
630      if ( ctground ) return(0);
631      //
632    Int_t error = 0;    Int_t error = 0;
633    ifstream badfile;    GL_PARAM *glparam = new GL_PARAM();  
   GL_PARAM *glparam = new GL_PARAM();  
634    //    //
635    // determine where I can find file with offline bad pulser mask    // determine where I can find file with offline bad pulser mask
636    //    //
637      stringstream bmfile;
638    error = 0;    error = 0;
639    error = glparam->Query_GL_PARAM(runheader,105,dbc);    error = glparam->Query_GL_PARAM(runheader,105,dbc);
640    if ( error < 0 ) return(error);    if ( error < 0 ) return(error);
# Line 402  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T Line 643  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T
643    bmfile << glparam->PATH.Data() << "/";    bmfile << glparam->PATH.Data() << "/";
644    bmfile << glparam->NAME.Data();    bmfile << glparam->NAME.Data();
645    //    //
646      ifstream badfile;
647    if ( verbose ) printf("\n Using bad pulser offline mask file: \n %s \n\n",bmfile.str().c_str());    if ( verbose ) printf("\n Using bad pulser offline mask file: \n %s \n\n",bmfile.str().c_str());
648    badfile.open(bmfile.str().c_str());    badfile.open(bmfile.str().c_str());
649    if ( !badfile ){    if ( !badfile ){
# Line 430  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T Line 672  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T
672      };      };
673    };    };
674    //    //
   delete glparam;  
675    badfile.close();    badfile.close();
676    //    if ( !usetable ){
   // Let's start with cross-talk correction calculation  
   //  
   GL_CALOPULSE_CALIB *glp = new GL_CALOPULSE_CALIB();  
   Float_t adcp[2][22][96];  
   Float_t adcpcal[2][22][96];  
   memset(adcp , 0, 2*22*96*sizeof(Float_t));  
   memset(adcpcal , 0, 2*22*96*sizeof(Float_t));  
   //  
   UInt_t pampli = 0;    
   for (Int_t s=0; s<4; s++){  
     //  
     // Save into matrix adcp the values of the highest pulse calibration (pulse amplitude = 2)  
     //  
     pampli = 2;  
     error = 0;  
     error = glp->Query_GL_CALOPULSE_CALIB(runheader,s,pampli,dbc);  
     if ( error < 0 ){  
       if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");  
       return(error);  
     };  
     //  
     UInt_t idcalib = glp->ID_ROOT_L0;  
     UInt_t fromtime = glp->FROM_TIME;  
     UInt_t calibno = glp->EV_ROOT;  
     //  
     // determine path and name and entry of the calibration file  
     //  
     GL_ROOT *glroot = new GL_ROOT();    
     if ( verbose ) printf("\n");  
     if ( verbose ) printf(" ** SECTION %i **\n",s);  
     //  
     error = 0;  
     error = glroot->Query_GL_ROOT(idcalib,dbc);  
     if ( error < 0 ){  
       if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");  
       return(error);  
     };  
     //    
     stringstream name;  
     name.str("");  
     name << glroot->PATH.Data() << "/";  
     name << glroot->NAME.Data();  
     //  
     TString fcalname = (TString)name.str().c_str();  
     ifstream myfile;  
     myfile.open(fcalname.Data());  
     if ( !myfile ){      
       return(-107);  
     };  
     myfile.close();  
     //  
     TFile *File = new TFile(fcalname.Data());  
     if ( !File ) return(-108);  
     TTree *tr = (TTree*)File->Get("CalibCalPulse2");  
     if ( !tr ) return(-119);  
677      //      //
678      TBranch *calo = tr->GetBranch("CalibCalPulse2");      // Let's start with cross-talk correction calculation
679      //      //
680      pamela::CalibCalPulse2Event *ce = 0;      GL_CALOPULSE_CALIB *glp = new GL_CALOPULSE_CALIB();
681      tr->SetBranchAddress("CalibCalPulse2", &ce);      Float_t adcp[2][22][96];
682        Float_t adcpcal[2][22][96];
683        memset(adcp , 0, 2*22*96*sizeof(Float_t));
684        memset(adcpcal , 0, 2*22*96*sizeof(Float_t));
685      //      //
686      Long64_t ncalibs = calo->GetEntries();      UInt_t pampli = 0;  
687        for (Int_t s=0; s<4; s++){
688          //
689          // Save into matrix adcp the values of the highest pulse calibration (pulse amplitude = 2)
690          //
691          pampli = 2;
692          error = 0;
693          error = glp->Query_GL_CALOPULSE_CALIB(runheader,s,pampli,dbc);
694          if ( error < 0 ){
695            if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
696            return(error);
697          };
698          //
699          UInt_t idcalib = glp->ID_ROOT_L0;
700          UInt_t fromtime = glp->FROM_TIME;
701          UInt_t calibno = glp->EV_ROOT;
702          //
703          // determine path and name and entry of the calibration file
704          //
705          GL_ROOT *glroot = new GL_ROOT();  
706          if ( verbose ) printf("\n");
707          if ( verbose ) printf(" ** SECTION %i **\n",s);
708          //
709          error = 0;
710          error = glroot->Query_GL_ROOT(idcalib,dbc);
711          if ( error < 0 ){
712            if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
713            return(error);
714          };
715          //        
716          stringstream name;
717          name.str("");
718          name << glroot->PATH.Data() << "/";
719          name << glroot->NAME.Data();
720          //
721          TString fcalname = (TString)name.str().c_str();
722          ifstream myfile;
723          myfile.open(fcalname.Data());
724          if ( !myfile ){    
725            return(-107);
726          };
727          myfile.close();
728          //
729          TFile *File = new TFile(fcalname.Data());
730          if ( !File ) return(-108);
731          TTree *tr = (TTree*)File->Get("CalibCalPulse2");
732          if ( !tr ) return(-119);
733          //
734          TBranch *calo = tr->GetBranch("CalibCalPulse2");
735          //
736          pamela::CalibCalPulse2Event *ce = 0;
737          tr->SetBranchAddress("CalibCalPulse2", &ce);
738          //
739          Long64_t ncalibs = calo->GetEntries();
740          //
741          if ( !ncalibs ) return(-110);
742          //
743          calo->GetEntry(calibno);
744          if ( verbose ) printf(" PULSE2 using entry %u from file %s",calibno,fcalname.Data());
745          //
746          // retrieve calibration table
747          //
748          if ( ce->pstwerr[s] && ce->pperror[s] == 0 && ce->unpackError == 0 ){
749            for ( Int_t d=0 ; d<11 ;d++  ){
750              for ( Int_t j=0; j<96 ;j++){
751                if ( s == 2 ){
752                  adcp[0][2*d+1][j] = ce->calpuls[3][d][j];
753                };
754                if ( s == 3 ){
755                  adcp[0][2*d][j] = ce->calpuls[1][d][j];
756                };
757                if ( s == 0 ){
758                  adcp[1][2*d][j] = ce->calpuls[0][d][j];
759                };
760                if ( s == 1 ){
761                  adcp[1][2*d+1][j] = ce->calpuls[2][d][j];
762                };
763              };
764            };
765          } else {
766            if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");
767            return(-111);
768          };
769          //
770          File->Close();
771          delete glroot;
772          //
773          // Save into matrix adcpcal the calibrated values of the pulse calibration (subtraction of pulse amplitude = 0 relative to the pulse2 calibration used)
774          //
775          pampli = 0;
776          error = 0;
777          error = glp->Query_GL_CALOPULSE_CALIB(fromtime,s,pampli,dbc);
778          if ( error < 0 ){
779            if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
780            return(error);
781          };
782          //
783          idcalib = glp->ID_ROOT_L0;
784          calibno = glp->EV_ROOT;
785          //
786          // determine path and name and entry of the calibration file
787          //
788          glroot = new GL_ROOT();  
789          if ( verbose ) printf("\n");
790          if ( verbose ) printf(" ** SECTION %i **\n",s);
791          //
792          error = 0;
793          error = glroot->Query_GL_ROOT(idcalib,dbc);
794          if ( error < 0 ){
795            if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");
796            return(error);
797          };
798          //        
799          name.str("");
800          name << glroot->PATH.Data() << "/";
801          name << glroot->NAME.Data();
802          //
803          fcalname = (TString)name.str().c_str();
804          myfile.open(fcalname.Data());
805          if ( !myfile ){    
806            return(-107);
807          };
808          myfile.close();
809          //
810          TFile *File1 = new TFile(fcalname.Data());
811          if ( !File1 ) return(-108);
812          TTree *tr1 = (TTree*)File1->Get("CalibCalPulse1");
813          if ( !tr1 ) return(-120);
814          //
815          TBranch *calo1 = tr1->GetBranch("CalibCalPulse1");
816          //
817          pamela::CalibCalPulse1Event *ce1 = 0;
818          tr1->SetBranchAddress("CalibCalPulse1", &ce1);
819          //
820          ncalibs = calo1->GetEntries();
821          //
822          if ( !ncalibs ) return(-110);
823          //
824          calo1->GetEntry(calibno);
825          if ( verbose ) printf(" PULSE1 using entry %u from file %s",calibno,fcalname.Data());
826          //
827          // retrieve calibration table
828          //
829          if ( ce1->pstwerr[s] && ce1->pperror[s] == 0 && ce1->unpackError == 0 ){
830            for ( Int_t d=0 ; d<11 ;d++  ){
831              for ( Int_t j=0; j<96 ;j++){
832                if ( s == 2 ){
833                  adcpcal[0][2*d+1][j] = adcp[0][2*d+1][j] - ce1->calpuls[3][d][j];
834                };
835                if ( s == 3 ){
836                  adcpcal[0][2*d][j] = adcp[0][2*d][j] - ce1->calpuls[1][d][j];
837                };
838                if ( s == 0 ){
839                  adcpcal[1][2*d][j] = adcp[1][2*d][j] - ce1->calpuls[0][d][j];
840                };
841                if ( s == 1 ){
842                  adcpcal[1][2*d+1][j] = adcp[1][2*d+1][j] - ce1->calpuls[2][d][j];
843                };
844              };
845            };
846          } else {
847            if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");
848            return(-111);
849          };
850          //
851          File1->Close();
852          //
853          delete glroot;
854          //
855        };// loop on the four sections
856      //      //
     if ( !ncalibs ) return(-110);  
857      //      //
858      calo->GetEntry(calibno);      delete glp;
     if ( verbose ) printf(" PULSE2 using entry %u from file %s",calibno,fcalname.Data());  
859      //      //
860      // retrieve calibration table      // Ok, now we can try to calculate the cross-talk correction for each pre-amplifier
861      //      //
862      if ( ce->pstwerr[s] && ce->pperror[s] == 0 && ce->unpackError == 0 ){      for ( Int_t v=0; v<2; v++){
863        for ( Int_t d=0 ; d<11 ;d++  ){        if ( debug ) printf(" \n\n NEW VIEW \n");
864          for ( Int_t j=0; j<96 ;j++){        for ( Int_t p=0; p<22; p++){
865            if ( s == 2 ){          for ( Int_t npre=0; npre<6; npre++){
866              adcp[0][2*d+1][j] = ce->calpuls[3][d][j];            ctprecor[v][p][npre] = 1000.;
867            };            ctneigcor[v][p][npre] = 1000.;
868            if ( s == 3 ){            Int_t str0=npre*16;
869              adcp[0][2*d][j] = ce->calpuls[1][d][j];            Int_t str16= -1 + (1+npre)*16;
870            };            //
871            if ( s == 0 ){            UInt_t neigc = 0;
872              adcp[1][2*d][j] = ce->calpuls[0][d][j];            UInt_t farc = 0;
873              UInt_t pulsc = 0;
874              Float_t sigpulsed = 0.;
875              Float_t neigbase = 0.;
876              Float_t farbase = 0.;
877              //
878              // 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
879              // moreover count the number of strips in each case
880              //
881              for (Int_t s=str0; s<=str16; s++){
882                if ( adcpcal[v][p][s] > 10000.){
883                  sigpulsed = adcpcal[v][p][s];
884                  pulsc++;
885                  if ( s > str0 ){
886                    neigbase += adcpcal[v][p][s-1];
887                    neigc++;
888                    farbase -= adcpcal[v][p][s-1];
889                    farc--;
890                  };
891                  if ( s < str16 ){
892                    neigbase += adcpcal[v][p][s+1];
893                    neigc++;
894                    farbase -= adcpcal[v][p][s+1];
895                    farc--;
896                  };
897                } else {
898                  farc++;
899                  farbase += adcpcal[v][p][s];
900                };
901            };            };
902            if ( s == 1 ){            //
903              adcp[1][2*d+1][j] = ce->calpuls[2][d][j];            // Now calculate the corrections
904              //
905              Float_t avefarbase = 0.;
906              if ( farc ) avefarbase = farbase/(Float_t)farc;
907              Float_t aveneigbase = 0.;
908              if ( neigc ) aveneigbase = neigbase/(Float_t)neigc;
909              //
910              if ( pulsc == 1 && farc && neigc ){
911                ctprecor[v][p][npre] = -avefarbase/(sigpulsed+fabs(avefarbase));
912                ctneigcor[v][p][npre] = fabs(aveneigbase-avefarbase)/(sigpulsed+fabs(avefarbase));    
913                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]);
914              } else {
915                //
916                // did not find the pulsed strip or more than one pulsed strip found!
917                //
918                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);
919                //
920            };            };
921          };          };
922            if ( debug ) printf(" \n ==================== \n");
923        };        };
     } else {  
       if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");  
       return(-111);  
924      };      };
925      } else {
926      //      //
927      File->Close();      // use pre-amply table
928      delete glroot;      //    
929      //      //
930      // Save into matrix adcpcal the calibrated values of the pulse calibration (subtraction of pulse amplitude = 0 relative to the pulse2 calibration used)      // determine where I can find file with offline neighbour correction table
931      //      //
932      pampli = 0;      stringstream bmfile2;
933      error = 0;      error = 0;
934      error = glp->Query_GL_CALOPULSE_CALIB(fromtime,s,pampli,dbc);      error = glparam->Query_GL_PARAM(runheader,106,dbc);
935      if ( error < 0 ){      if ( error < 0 ) return(error);
       if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");  
       return(error);  
     };  
     //  
     idcalib = glp->ID_ROOT_L0;  
     calibno = glp->EV_ROOT;  
936      //      //
937      // determine path and name and entry of the calibration file      bmfile2.str("");
938        bmfile2 << glparam->PATH.Data() << "/";
939        bmfile2 << glparam->NAME.Data();
940        //
941        ifstream badfile2;
942        if ( verbose ) printf("\n Using pre-amply neighbour crosstalk table file: \n %s \n\n",bmfile2.str().c_str());
943        badfile2.open(bmfile2.str().c_str());
944        if ( !badfile2 ){
945          if ( verbose ) printf(" CALORIMETER - ERROR: no pre-amply neighbour crosstalk table file!\n");
946          return(-121);
947        };
948        //  
949        Int_t vview = 0;
950        Int_t vplane = 0;
951        Int_t vpre = 0;
952        Float_t vcorr = 0.;
953        while ( badfile2 >> vview && badfile2 >> vplane && badfile2 >> vpre && badfile2 >> vcorr){
954          if ( debug ) printf(" Pre-amply neighbour correction: view %i plane %i pre %i correction %f  \n",vview,vplane,vpre,vcorr);
955          ctneigcor[vview][vplane][vpre] = vcorr;
956        };
957      //      //
958      glroot = new GL_ROOT();        // determine where I can find file with offline SECOND neighbour correction table
     if ( verbose ) printf("\n");  
     if ( verbose ) printf(" ** SECTION %i **\n",s);  
959      //      //
960        stringstream bmfile3;
961      error = 0;      error = 0;
962      error = glroot->Query_GL_ROOT(idcalib,dbc);      error = glparam->Query_GL_PARAM(runheader,107,dbc);
963      if ( error < 0 ){      if ( error < 0 ) return(error);
       if ( verbose ) printf(" CALORIMETER - ERROR: error from GLTables\n");  
       return(error);  
     };  
     //    
     name.str("");  
     name << glroot->PATH.Data() << "/";  
     name << glroot->NAME.Data();  
964      //      //
965      fcalname = (TString)name.str().c_str();      bmfile3.str("");
966      myfile.open(fcalname.Data());      bmfile3 << glparam->PATH.Data() << "/";
967      if ( !myfile ){          bmfile3 << glparam->NAME.Data();
968        return(-107);      //
969        ifstream badfile3;
970        if ( verbose ) printf("\n Using pre-amply second neighbour crosstalk table file: \n %s \n\n",bmfile3.str().c_str());
971        badfile3.open(bmfile3.str().c_str());
972        if ( !badfile3 ){
973          if ( verbose ) printf(" CALORIMETER - ERROR: no pre-amply second neighbour crosstalk table file!\n");
974          return(-122);
975      };      };
976      myfile.close();      //  
977      //      Int_t pview = 0;
978      TFile *File1 = new TFile(fcalname.Data());      Int_t pplane = 0;
979      if ( !File1 ) return(-108);      Int_t ppre = 0;
980      TTree *tr1 = (TTree*)File1->Get("CalibCalPulse1");      Float_t pcorr = 0.;
981      if ( !tr1 ) return(-120);      while ( badfile3 >> pview && badfile3 >> pplane && badfile3 >> ppre && badfile3 >> pcorr){
982      //        if ( debug ) printf(" Pre-amply second neighbour correction: view %i plane %i pre %i correction %f  \n",pview,pplane,ppre,-pcorr);
983      TBranch *calo1 = tr1->GetBranch("CalibCalPulse1");        ctprecor[pview][pplane][ppre] = -pcorr; // data are saved as negatives in the file
984      //      };
     pamela::CalibCalPulse1Event *ce1 = 0;  
     tr1->SetBranchAddress("CalibCalPulse1", &ce1);  
     //  
     ncalibs = calo1->GetEntries();  
     //  
     if ( !ncalibs ) return(-110);  
985      //      //
986      calo1->GetEntry(calibno);      // determine where to find the file containing the Silicon crosstalk correction table
     if ( verbose ) printf(" PULSE1 using entry %u from file %s",calibno,fcalname.Data());  
987      //      //
988      // retrieve calibration table      stringstream bmfile4;
989        error = 0;
990        error = glparam->Query_GL_PARAM(runheader,108,dbc);
991        if ( error < 0 ) return(error);
992      //      //
993      if ( ce1->pstwerr[s] && ce1->pperror[s] == 0 && ce1->unpackError == 0 ){      bmfile4.str("");
994        for ( Int_t d=0 ; d<11 ;d++  ){      bmfile4 << glparam->PATH.Data() << "/";
995          for ( Int_t j=0; j<96 ;j++){      bmfile4 << glparam->NAME.Data();
996            if ( s == 2 ){      //
997              adcpcal[0][2*d+1][j] = adcp[0][2*d+1][j] - ce1->calpuls[3][d][j];      ifstream badfile4;
998            };      if ( verbose ) printf("\n Using Silicon crosstalk table file: \n %s \n\n",bmfile4.str().c_str());
999            if ( s == 3 ){      badfile4.open(bmfile4.str().c_str());
1000              adcpcal[0][2*d][j] = adcp[0][2*d][j] - ce1->calpuls[1][d][j];      if ( !badfile4 ){
1001            };        if ( verbose ) printf(" CALORIMETER - ERROR: no Silicon crosstalk table file!\n");
1002            if ( s == 0 ){        return(-125);
             adcpcal[1][2*d][j] = adcp[1][2*d][j] - ce1->calpuls[0][d][j];  
           };  
           if ( s == 1 ){  
             adcpcal[1][2*d+1][j] = adcp[1][2*d+1][j] - ce1->calpuls[2][d][j];  
           };  
         };  
       };  
     } else {  
       if ( verbose ) printf(" CALORIMETER - ERROR: problems finding a good calibration in this file! \n\n ");  
       return(-111);  
1003      };      };
1004        //  
1005        Int_t spview = 0;
1006        Int_t spplane = 0;
1007        Int_t psil = 0;
1008        Float_t spcorr = 0.;
1009        memset(ctsicor, 0, 2*22*9*sizeof(Float_t));
1010        while ( badfile4 >> spview && badfile4 >> spplane && badfile4 >> psil && badfile4 >> spcorr){
1011          if ( debug ) printf(" Silicon correction: view %i plane %i silicon %i correction %f  \n",spview,spplane,psil,-spcorr);
1012          ctsicor[spview][spplane][psil] = -spcorr; // data are saved as negatives in the file
1013        };
1014      //      //
     File1->Close();  
     //  
     delete glroot;  
     //  
   };// loop on the four sections  
   //  
   //  
   delete glp;  
   dbc->Close();  
   delete dbc;  
   //  
   // Ok, now we can try to calculate the cross-talk correction for each pre-amplifier  
   //  
   for ( Int_t v=0; v<2; v++){  
     if ( debug ) printf(" \n\n NEW VIEW \n");  
     for ( Int_t p=0; p<22; p++){  
       for ( Int_t npre=0; npre<6; npre++){  
         ctprecor[v][p][npre] = 1000.;  
         ctneigcor[v][p][npre] = 1000.;  
         Int_t str0=npre*16;  
         Int_t str16= -1 + (1+npre)*16;  
         //  
         UInt_t neigc = 0;  
         UInt_t farc = 0;  
         UInt_t pulsc = 0;  
         Float_t sigpulsed = 0.;  
         Float_t neigbase = 0.;  
         Float_t farbase = 0.;  
         //  
         // 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  
         // moreover count the number of strips in each case  
         //  
         for (Int_t s=str0; s<=str16; s++){  
           if ( adcpcal[v][p][s] > 10000.){  
             sigpulsed = adcpcal[v][p][s];  
             pulsc++;  
             if ( s > str0 ){  
               neigbase += adcpcal[v][p][s-1];  
               neigc++;  
               farbase -= adcpcal[v][p][s-1];  
               farc--;  
             };  
             if ( s < str16 ){  
               neigbase += adcpcal[v][p][s+1];  
               neigc++;  
               farbase -= adcpcal[v][p][s+1];  
               farc--;  
             };  
           } else {  
             farc++;  
             farbase += adcpcal[v][p][s];  
           };  
         };  
         //  
         // Now calculate the corrections  
         //  
         Float_t avefarbase = 0.;  
         if ( farc ) avefarbase = farbase/(Float_t)farc;  
         Float_t aveneigbase = 0.;  
         if ( neigc ) aveneigbase = neigbase/(Float_t)neigc;  
         //  
         if ( pulsc == 1 && farc && neigc ){  
           ctprecor[v][p][npre] = -avefarbase/(sigpulsed+fabs(avefarbase));  
           ctneigcor[v][p][npre] = fabs(aveneigbase-avefarbase)/(sigpulsed+fabs(avefarbase));        
           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]);  
         } else {  
           //  
           // did not find the pulsed strip or more than one pulsed strip found!  
           //  
           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);  
         //  
         };  
       };  
       if ( debug ) printf(" \n ==================== \n");  
     };  
1015    };    };
1016    //    //
1017      delete glparam;
1018      //
1019    // Check the calculated corrections    // Check the calculated corrections
1020    //    //
1021    Int_t opre=0;    Int_t opre=0;
# Line 723  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T Line 1056  Int_t CaloLevel0::CalcCrossTalkCorr(GL_T
1056          };          };
1057        };        };
1058      };      };
1059    };    };  
1060    //    //
1061    return(0);    return(0);
1062  }  }
# Line 751  void CaloLevel0::FindBaseRaw(Int_t l, In Line 1084  void CaloLevel0::FindBaseRaw(Int_t l, In
1084    //    //
1085    Float_t minstrip = 100000.;    Float_t minstrip = 100000.;
1086    Float_t rms = 0.;    Float_t rms = 0.;
1087    base[l][m][pre] = 0.;    Int_t process = 0;
1088    qp = 0.;    Int_t onlmask[16];
1089    //    memset(onlmask, 0, 16*sizeof(Int_t));
1090    for (Int_t e = pre*16; e < (pre+1)*16 ; e++){    //
1091      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.) {    while ( process < 2 ){
1092        minstrip = dexy[l][m][e]-calped[l][m][e];      //
1093        rms = calthr[l][m][pre];      minstrip = 100000.;
1094      };      rms = 0.;
1095      qp += (dexy[l][m][e]-calped[l][m][e]-sbase[l][m][e]);      base[l][m][pre] = 0.;
1096    };      qp = 0.;
1097    //      //
1098    if ( debug && l==1 ){      Int_t spos = -1;
1099      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);      Int_t ee = 0;
   };  
   if ( minstrip != 100000. ) {  
     Float_t strip6s = 0.;  
1100      for (Int_t e = pre*16; e < (pre+1)*16 ; e++){      for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1101        if ( (dexy[l][m][e]-calped[l][m][e]) >= minstrip && (dexy[l][m][e]-calped[l][m][e]) <= (minstrip+rms) ) {        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 ) {
1102          strip6s += 1.;          minstrip = dexy[l][m][e]-calped[l][m][e];
1103          base[l][m][pre] += (dexy[l][m][e] - calped[l][m][e]);          rms = calthr[l][m][pre];
1104        };          spos = ee;
       //  
       //  compression  
       //  
       if ( abs((int)(dexy[l][m][e]-calped[l][m][e])) <= (minstrip+rms) ) {  
         dexyc[l][m][e] = 0.;  
       } else {  
         dexyc[l][m][e] = dexy[l][m][e];  
1105        };        };
1106          ee++;
1107          qp += (dexy[l][m][e]-calped[l][m][e]-sbase[l][m][e]);
1108      };      };
1109      //      //
1110      nst = (Int_t)strip6s;      if ( debug && l==0 ){
1111      //        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);
     if ( debug && l==1 ){  
       printf(" strip6s %f \n",strip6s);  
1112      };      };
1113      //  if ( strip6s >= 9. ){            if ( minstrip != 100000. ) {
1114      if ( strip6s >= 2. ){                  Float_t strip6s = 0.;
1115        Double_t arro = base[l][m][pre]/strip6s;        for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1116        Float_t deci = 1000.*((float)arro - float(int(arro)));                            if ( (dexy[l][m][e]-calped[l][m][e]) >= minstrip && (dexy[l][m][e]-calped[l][m][e]) <= (minstrip+rms) ) {
1117        if ( deci < 500. ) {            strip6s += 1.;
1118          arro = double(int(arro));            base[l][m][pre] += (dexy[l][m][e] - calped[l][m][e]);
1119        } else {          };
1120          arro = 1. + double(int(arro));          //
1121            //  compression
1122            //
1123            //      if ( abs((int)(dexy[l][m][e]-calped[l][m][e])) <= (minstrip+rms) ) {
1124            //        dexyc[l][m][e] = 0.;
1125            //      } else {
1126            dexyc[l][m][e] = dexy[l][m][e];
1127              //    };
1128        };        };
       base[l][m][pre] = arro;  
       //  
       // 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  
1129        //        //
1130        if ( debug && l==1 ) printf(" Calculated baseline: base %f sbase*1.02 %f \n",base[l][m][pre],1.02*sbase[l][m][pre]);        if ( strip6s == 1. && process < 1 ){
1131            onlmask[spos] = 1;
1132            process++;
1133            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);
1134            continue;
1135          };
1136          process += 2;
1137          nst = (Int_t)strip6s;
1138        //        //
1139        if ( strip6s < 4 && base[l][m][pre] > 1.02*sbase[l][m][pre] && sbase[l][m][pre] > 0. ){        if ( debug ){
1140          if ( debug ) printf(" Suspicious calculated baseline: base %f sbase*1.02 %f strip6s %i \n",base[l][m][pre],1.02*sbase[l][m][pre],(Int_t)strip6s);          printf(" strip6s %f \n",strip6s);
1141          base[l][m][pre] = 31000.;              };
1142          //        if ( strip6s >= 9. ){      
1143          if ( (strip6s >= 2. && process == 2) || (strip6s >= 9. && process > 2) ){    
1144            //if ( (strip6s >= 4. && process == 2) || (strip6s >= 9. && process > 2) ){        
1145            Double_t arro = base[l][m][pre]/strip6s;
1146            Float_t deci = 1000.*((float)arro - float(int(arro)));                
1147            if ( deci < 500. ) {
1148              arro = double(int(arro));
1149            } else {
1150              arro = 1. + double(int(arro));
1151            };
1152            base[l][m][pre] = arro;
1153            //
1154            // 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
1155            //
1156            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);
1157            if ( debug ) printf(" Calculated baseline: base %f sbase-0.02*qp %f \n",base[l][m][pre],(-qp*0.02+sbase[l][m][pre]));
1158            //
1159            if ( strip6s < 4 && base[l][m][pre] > (-0.015*qp+sbase[l][m][pre]) && sbase[l][m][pre] > 0. ){
1160              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);
1161              base[l][m][pre] = 31000.;    
1162              for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1163                dexyc[l][m][e] = dexy[l][m][e];
1164              };      
1165            };
1166          } else {
1167            base[l][m][pre] = 31000.;
1168            for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1169              dexyc[l][m][e] = dexy[l][m][e];
1170            };
1171        };        };
1172      } else {      } else {
1173          process += 2;
1174        base[l][m][pre] = 31000.;        base[l][m][pre] = 31000.;
1175        for (Int_t e = pre*16; e < (pre+1)*16 ; e++){        for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
1176          dexyc[l][m][e] = dexy[l][m][e];          dexyc[l][m][e] = dexy[l][m][e];
1177        };        };
1178      };      };
   } else {  
     base[l][m][pre] = 31000.;  
1179    };    };
1180  }  }
1181    
# Line 872  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1234  Int_t CaloLevel0::Calibrate(Int_t ei){
1234    Float_t ener;    Float_t ener;
1235    Int_t doneb = 0;    Int_t doneb = 0;
1236    Int_t donec = 0;    Int_t donec = 0;
1237    Int_t ck[6] = {0,0,0,0,0,0};    Int_t ck[2][22][6];
1238      memset(ck, 0, 2*22*6*sizeof(Int_t));
1239    Int_t ipre = 0;    Int_t ipre = 0;
1240    //  Int_t ip[3] = {0};    //  Int_t ip[3] = {0};
1241    Int_t ip[3] = {0,0,0};    Int_t ip[3] = {0,0,0};
1242      Int_t ipp = 0;
1243    Float_t base0, base1, base2;    Float_t base0, base1, base2;
1244    base0 = 0.;    base0 = 0.;
1245    base1 = 0.;    base1 = 0.;
1246    base2 = 0.;    base2 = 0.;
1247    Float_t qpre[6] = {0.,0.,0.,0.,0.,0.};    Float_t qpre[2][22][6];
1248      memset(qpre, 0, 2*22*6*sizeof(Float_t));
1249    Float_t ene[96];    Float_t ene[96];
1250    Int_t chdone[4] = {0,0,0,0};    Int_t chdone[4] = {0,0,0,0};
1251    Int_t pe = 0;    Int_t pe = 0;
# Line 888  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1253  Int_t CaloLevel0::Calibrate(Int_t ei){
1253    Float_t ener0 = 0.;    Float_t ener0 = 0.;
1254    Float_t cbase0 = 0.;    Float_t cbase0 = 0.;
1255    Bool_t pproblem = false;    Bool_t pproblem = false;
1256      Bool_t negbase = false;
1257    //    //
1258    Float_t tim = 0.;    Float_t tim = 0.;
1259    Int_t plo = 0;    Int_t plo = 0;
# Line 901  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1267  Int_t CaloLevel0::Calibrate(Int_t ei){
1267        //        //
1268        // determine the section number          // determine the section number  
1269        //        //
1270          negbase = false;
1271        se = 5;        se = 5;
1272        if (l == 0 && m%2 == 0) se = 3;        if (l == 0 && m%2 == 0) se = 3;
1273        if (l == 0 && m%2 != 0) se = 2;        if (l == 0 && m%2 != 0) se = 2;
# Line 926  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1293  Int_t CaloLevel0::Calibrate(Int_t ei){
1293          };          };
1294          clevel2->perr[se] = 0;          clevel2->perr[se] = 0;
1295          if ( de->perror[se] != 0 ){          if ( de->perror[se] != 0 ){
1296            clevel2->perr[se] = 1;            clevel2->perr[se] = (Int_t)de->perror[se];
1297            pe++;            pe++;
1298          };          };
1299          clevel2->swerr[se] = 0;          clevel2->swerr[se] = 0;
# Line 942  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1309  Int_t CaloLevel0::Calibrate(Int_t ei){
1309          pre = -1;          pre = -1;
1310          //          //
1311          for (Int_t nn = 0; nn < 96; nn++){                            for (Int_t nn = 0; nn < 96; nn++){                  
1312            ene[nn] = 0.;            //      ene[nn] = 0.;
1313            dexy[l][m][nn] = de->dexy[l][m][nn] ;            dexy[l][m][nn] = de->dexy[l][m][nn] ;
1314            dexyc[l][m][nn] = de->dexyc[l][m][nn] ;            dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
1315          };          };
# Line 954  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1321  Int_t CaloLevel0::Calibrate(Int_t ei){
1321          Int_t nstt[2];          Int_t nstt[2];
1322          Float_t rqp[2];          Float_t rqp[2];
1323          for (Int_t i = 0; i < 3; i++){          for (Int_t i = 0; i < 3; i++){
1324            nstt[0] = 0;            nstt[0] = 1000;
1325            nstt[1] = 0;            nstt[1] = 1000;
1326            rqp[0] = 0.;            rqp[0] = 0.;
1327            rqp[1] = 0.;            rqp[1] = 0.;
1328            for (Int_t j = 0; j < 2; j++){            for (Int_t j = 0; j < 2; j++){
# Line 984  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1351  Int_t CaloLevel0::Calibrate(Int_t ei){
1351            //            //
1352            // 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            // 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
1353            //            //
1354            if ( nstt[0] < 4 && nstt[1] >= 4 ) base[l][m][pre-1] = 31000.;            if ( nstt[0] < 4 && nstt[1] >= 4 && nstt[0] != 1000 && nstt[1] != 1000 ) base[l][m][pre-1] = 31000.;
1355            if ( nstt[0] >= 4 && nstt[1] < 4 ) base[l][m][pre] = 31000.;            if ( nstt[0] >= 4 && nstt[1] < 4 && nstt[0] != 1000 && nstt[1] != 1000 ) base[l][m][pre] = 31000.;
1356  //        //            //      //
1357  //        // 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            //      // 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
1358  //        //            //      //
1359  //        if ( nstt[0] < 4 && nstt[1] < 4 ){            //      if ( nstt[0] < 4 && nstt[1] < 4 ){
1360  //          if ( rqp[0] >= rqp[1] ) base[l][m][pre-1] = 31000.;            //        if ( rqp[0] >= rqp[1] ) base[l][m][pre-1] = 31000.;
1361  //          if ( rqp[0] < rqp[1] ) base[l][m][pre] = 31000.;            //        if ( rqp[0] < rqp[1] ) base[l][m][pre] = 31000.;
1362  //        };            //      };
1363          };          };
1364          //          //
1365          // run over strips          // run over strips
# Line 1007  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1374  Int_t CaloLevel0::Calibrate(Int_t ei){
1374                doneb = 0;                doneb = 0;
1375                donec = 0;                donec = 0;
1376                pre++;                pre++;
1377                ck[pre] = 0;                ck[l][m][pre] = 0;
1378                qpre[pre] = 0.;                qpre[l][m][pre] = 0.;
1379              };              };
1380              //              //
1381              // baseline check and calculation              // baseline check and calculation
# Line 1017  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1384  Int_t CaloLevel0::Calibrate(Int_t ei){
1384              //              //
1385              if ( !done ){              if ( !done ){
1386                if ( (base[l][m][pre] == 31000. || base[l][m][pre] == 0.) ){                if ( (base[l][m][pre] == 31000. || base[l][m][pre] == 0.) ){
1387                  ck[pre] = 1;                  ck[l][m][pre] = 1;
1388                  if (pre%2 == 0) {                  if (pre%2 == 0) {
1389                    ip[i] = pre + 1;                    ip[i] = pre + 1;
1390                  } else {                  } else {
# Line 1025  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1392  Int_t CaloLevel0::Calibrate(Int_t ei){
1392                  };                  };
1393                  if ( (base[l][m][ip[i]] == 31000. || base[l][m][ip[i]] == 0. || !crosst ) ){                  if ( (base[l][m][ip[i]] == 31000. || base[l][m][ip[i]] == 0. || !crosst ) ){
1394                    //                    //
1395                    ck[pre] = 2;                    ck[l][m][pre] = 2;
1396                    if ( sbase[l][m][pre] == 31000. || sbase[l][m][pre] == 0. ) {                    if ( sbase[l][m][pre] == 31000. || sbase[l][m][pre] == 0. ) {
1397                      ck[pre] = 3;                      ck[l][m][pre] = 3;
1398                    };                    };
1399                  };                  };
                 done = 1;  
1400                };                };
1401                  done = 1;
1402              };                                };                  
1403              //              //
1404              // CALIBRATION ALGORITHM              // CALIBRATION ALGORITHM
1405              //              //
1406              if ( !doneb ){              if ( !doneb ){
1407                if ( debug ) printf(" ck[pre] is %i \n",ck[pre]);                if ( debug ) printf(" ck[l][m][pre] is %i \n",ck[l][m][pre]);
1408                switch (ck[pre]) {                switch (ck[l][m][pre]) {
1409                case 0:                case 0:
1410                  base0 = base[l][m][pre];                  base0 = base[l][m][pre];
1411                  base2 = calbase[l][m][pre];                  base2 = calbase[l][m][pre];
# Line 1066  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1433  Int_t CaloLevel0::Calibrate(Int_t ei){
1433              ener = dexyc[l][m][n];              ener = dexyc[l][m][n];
1434              ener0 += ener;              ener0 += ener;
1435              clevel1->estrip[n][m][l] = 0.;              clevel1->estrip[n][m][l] = 0.;
1436                if ( de->base[l][m][pre] < 0 ) negbase = true;
1437              if ( base0>0 && base0 < 30000. ){              if ( base0>0 && base0 < 30000. ){
1438                //              if ( !donec && (base0 - base1 + base2) != 0. ){                //
1439                //                sbase[l][m][pre] = base0 - base1 + base2;                // save the baseline only if the energy release is "small"
1440                if ( !donec && (base0 + base1 - base2) != 0. ){                //
1441                  sbase[l][m][pre] = base0 + base1 - base2;                if ( !donec && (base0 + base1 - base2) != 0. && (n+1)%16==0 ){
1442                    if ( qpre[l][m][pre] < 200. ) sbase[l][m][pre] = base0 + base1 - base2;
1443                  donec = 1;                  donec = 1;
1444                };                };
1445                if ( ener > 0. ){                if ( ener > 0. ){
# Line 1078  Int_t CaloLevel0::Calibrate(Int_t ei){ Line 1447  Int_t CaloLevel0::Calibrate(Int_t ei){
1447                  //                  //
1448                  // 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)                  // 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)
1449                  //                  //
1450                  qpre[pre] += clevel1->estrip[n][m][l];                  if ( clevel1->estrip[n][m][l] > 0. ) qpre[l][m][pre] += clevel1->estrip[n][m][l];
1451                  //                  //
1452                  //                  //
1453                };                };
1454              };              };
1455            };            };
1456            if ( crosst ){          };
1457              if (ck[pre] == 1 || ck[pre-1] == 1){          //
1458                if (ck[pre] == 1){          // check if there were problems with 5.7 or glitches in the power supply
1459            //
1460            if ( ((ener0 == 0. && cbase0 == 0.) || negbase ) && !pproblem && clevel2->perr[se] == 0){
1461              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);
1462              pproblem = true;
1463              pe++;
1464            };
1465            //
1466          } else {
1467            for (Int_t nn = 0; nn < 96; nn++){                  
1468              clevel1->estrip[nn][m][l] = 0.;
1469            };
1470          };
1471        };
1472      };
1473      //
1474      // run over views and planes to apply crosstalk corrections
1475      //
1476      for (Int_t l = 0; l < 2; l++){
1477        for (Int_t m = 0; m < 22; m++){
1478          //
1479          // determine the section number  
1480          //
1481          se = 5;
1482          if (l == 0 && m%2 == 0) se = 3;
1483          if (l == 0 && m%2 != 0) se = 2;
1484          if (l == 1 && m%2 != 0) se = 1;
1485          if (l == 1 && m%2 == 0) se = 0;          
1486          //
1487          // check for any error in the event
1488          //
1489          if ( clevel2->crc[se] == 0 && (clevel1->good2 == 1 || clevel2->trigty >= 2) ){
1490            //
1491            // Cross-talk corrections  
1492            //
1493            if ( crosst ){
1494              //
1495              // energy on silicon ladders
1496              //
1497              Float_t qsi[3];
1498              qsi[0] = qpre[l][m][0]+qpre[l][m][1];
1499              qsi[1] = qpre[l][m][2]+qpre[l][m][3];
1500              qsi[2] = qpre[l][m][4]+qpre[l][m][5];      
1501              //
1502              for ( pre = 1; pre < 6; pre += 2 ){  
1503                Int_t ladder = (pre - 1)/2;
1504                //
1505                // If the noselfct flag is set the strip doesn't suffer the self crosstalk due to electronics so we must subtract some energy
1506                //
1507                if ( noselfct ){
1508                  for (Int_t j = ladder*32 ; j < (ladder+1)*32 ; j++){
1509                    ipre = j/16 ;  
1510                    if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] -=  clevel1->estrip[j][m][l] * ctprecor[l][m][ipre];
1511                  };
1512                };
1513                //
1514                // Using the neighbour pre baseline
1515                //
1516                if (ck[l][m][pre] == 1 || ck[l][m][pre-1] == 1){
1517                  //
1518                  // pre-amplifier effect on baseline when using the neighbour pre (ck=1)
1519                  //
1520                  if (ck[l][m][pre] == 1){
1521                  ipre = pre;                  ipre = pre;
1522                  ip[i] = pre - 1;                  ipp = pre - 1;
1523                } else {                } else {
1524                  ipre = pre - 1;                  ipre = pre - 1;
1525                  ip[i] = pre;                  ipp = pre;
1526                };                };
1527                //              if (ip[i]%2 == 0) {                Int_t it = 0;
1528                //                ipre = ip[i] + 1;                Float_t nqpre = 0.;
1529                //              } else {                //
1530                //                ipre = ip[i] - 1;                if ( debug ) printf(" CK1 Limit for while: 0.07 \n");
               //              };  
1531                for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){                for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
1532                  if ( !ctground ){                  if ( !ctground ){
1533                    clevel1->estrip[j][m][l] += (qpre[ipre] - qpre[ip[i]]) * ctprecor[l][m][ip[i]];                    if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += - qpre[l][m][ipp] * ctprecor[l][m][ipp];
1534                  } else {                  } else {
1535                    clevel1->estrip[j][m][l] += (qpre[ipre] - qpre[ip[i]]) * 0.00478;                    if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += - qpre[l][m][ipp] * 0.00478;
1536                    };
1537                    if ( clevel1->estrip[j][m][l] > 0. ) nqpre += clevel1->estrip[j][m][l] ;
1538                  };
1539                  qpre[l][m][ipre] = nqpre;
1540                  nqpre = 0.;
1541                  Float_t deltaqpre = qpre[l][m][ipre];          
1542                  //
1543                  // these values are empirically determined, usually the routine converge due to deltaqsi and the latest applied correction is based on less than 1 mip
1544                  //
1545                  while ( it < 10 && deltaqpre > 0.07 ){
1546                    nqpre = 0.;
1547                    for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
1548                      if ( !ctground ){
1549                        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]);
1550                        if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += deltaqpre * ctprecor[l][m][ipre];
1551                      } else {
1552                        if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] += deltaqpre * 0.00478;
1553                      };
1554                      if ( clevel1->estrip[j][m][l] > 0. ) nqpre += clevel1->estrip[j][m][l] ;
1555                  };                  };
1556                    if ( ctground ) it = 100;
1557                    it++;
1558                    deltaqpre = nqpre - qpre[l][m][ipre];
1559                    if ( debug ) printf(" CK1 BEFORE: qpre %f \n",qpre[l][m][ipre]);  
1560                    qpre[l][m][ipre] = nqpre;
1561                    if ( debug ) printf(" CK1 AFTER: qpre %f \n",qpre[l][m][ipre]);  
1562                };                };
1563                  //
1564              };              };
1565              if (ck[pre] == 2 && ck[pre-1] == 2){              //
1566                for (Int_t j = i*32 ; j < (i+1)*32 ; j++){              // No baseline calculation due to high energy release
1567                  ipre = j/16 + 1;              //
1568                  if ( !ctground ){              if (ck[l][m][pre] == 2 && ck[l][m][pre-1] == 2){
1569                    clevel1->estrip[j][m][l] +=  qpre[ipre] * ctprecor[l][m][ipre];                //
1570                  // y^
1571                  //  |
1572                  //  |    6 7 8
1573                  //  |    3 4 5  
1574                  //  |    0 1 2
1575                  //  | --------------------------------------> x
1576                  //
1577                  Int_t si1 = 0;
1578                  Int_t si2 = 0;
1579                  Int_t si3 = 0;
1580                  if ( l == 0 ){
1581                    if ( ladder == 0 ){
1582                      si1 = 0;
1583                      si2 = 3;
1584                      si3 = 6;
1585                    };
1586                    if ( ladder == 1 ){
1587                      si1 = 1;
1588                      si2 = 4;
1589                      si3 = 7;
1590                    };
1591                    if ( ladder == 2 ){
1592                      si1 = 2;
1593                      si2 = 5;
1594                      si3 = 8;
1595                    };
1596                  } else {
1597                    if ( ladder == 0 ){
1598                      si1 = 0;
1599                      si2 = 1;
1600                      si3 = 2;
1601                    };
1602                    if ( ladder == 1 ){
1603                      si1 = 3;
1604                      si2 = 4;
1605                      si3 = 5;
1606                    };
1607                    if ( ladder == 2 ){
1608                      si1 = 6;
1609                      si2 = 7;
1610                      si3 = 8;
1611                    };
1612                  };
1613                  //
1614                  // Find the energy distribution along the considered plane looking at the two sandwiching plane of the other view.
1615                  //
1616                  Float_t sied[3] = {0.,0.,0.};
1617                  Int_t othv = !l;
1618                  Int_t othpl1 = m - 1;
1619                  Int_t othpl2 = m + 1;
1620                  Float_t oprof[3] = {0.,0.,0.};
1621                  for(Int_t s=0; s<3; s++){
1622                    for(Int_t t=(s*32); t<32*(s + 1); t++){      
1623                      if ( othpl1 > -1 ) {
1624                        oprof[s] += clevel1->estrip[othv][othpl1][t];
1625                      };
1626                      if ( othpl2 < 22 ) {
1627                        oprof[s] += clevel1->estrip[othv][othpl2][t];
1628                      };          
1629                    };
1630                  };
1631                  Float_t otote = fabs(oprof[0]) + fabs(oprof[1]) + fabs(oprof[2]);
1632                  for(Int_t g=0; g<3; g++){
1633                    if ( otote > 0. ){
1634                      sied[g] = fabs(oprof[g])/otote;
1635                  } else {                  } else {
1636                    clevel1->estrip[j][m][l] +=  qpre[ipre] * 0.00478;                    sied[g] = 1./3.;
1637                  };                  };
1638                };                };
1639                  //
1640                  //
1641                  //
1642                  Int_t it = 0;
1643                  Int_t jpre = 0;
1644                  Float_t nqsi = 0.;
1645                  Float_t snqsi = qsi[ladder];
1646                  Float_t nqpre[2] = {0.,0.};
1647                  Float_t deltaqsi = qsi[ladder];
1648                  Float_t deltaqpre[2];
1649                  deltaqpre[0] = qpre[l][m][pre-1];
1650                  deltaqpre[1] = qpre[l][m][pre];
1651                  //
1652                  if ( debug ) printf(" Limit for while: 0.07 it < 10 \n");
1653                  //
1654                  // these values are empirically determined, usually the routine converge due to deltaqsi and the latest applied correction is based on less than 1 mip
1655                  //
1656                  while ( it < 10 && (deltaqsi > 0.07 || deltaqpre[0] > 0.07 || deltaqpre[1] > 0.07) ){
1657                    nqsi = 0.;
1658                    nqpre[0] = 0.;
1659                    nqpre[1] = 0.;
1660                    for (Int_t j = ladder*32 ; j < (ladder+1)*32 ; j++){
1661                      ipre = 0;
1662                      if ( j > (ladder*32)+15 ) ipre = 1;
1663                      jpre = j/16 ;
1664                      //
1665                      // Silicon effect on the baseline when using the same pre previous baseline (ck = 2) + pre-amply effect
1666                      //          
1667                      if ( !ctground ){
1668                        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]));
1669                        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]);
1670                        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];
1671                      } else {
1672                        if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] +=  0. + qpre[l][m][jpre] * 0.00478; // no correction
1673                      };
1674                      if ( clevel1->estrip[j][m][l] > 0. ) nqsi += clevel1->estrip[j][m][l] ;
1675                      if ( clevel1->estrip[j][m][l] > 0. ) nqpre[ipre] += clevel1->estrip[j][m][l] ;
1676                    };      
1677                    if ( ctground ) it = 100;
1678                    deltaqsi = nqsi-snqsi;
1679                    snqsi = nqsi;
1680                    it++;
1681                    deltaqpre[0] = nqpre[0] - qpre[l][m][pre-1];
1682                    deltaqpre[1] = nqpre[1] - qpre[l][m][pre];
1683                    if ( debug ) printf(" BEFORE: qpre 0 %f qpre 1 %f \n",qpre[l][m][pre-1],qpre[l][m][pre]);  
1684                    qpre[l][m][pre-1] = nqpre[0];
1685                    qpre[l][m][pre] = nqpre[1];    
1686                    if ( debug ) printf(" AFTER: qpre 0 %f qpre 1 %f \n",qpre[l][m][pre-1],qpre[l][m][pre]);  
1687                  };
1688                  //
1689                  //
1690                  //
1691    //            for (Int_t j = ladder*32 ; j < (ladder+1)*32 ; j++){
1692    //              ipre = j/16 ;          
1693    //              //
1694    //              // pre-amplifier effect on baseline when using the same pre previous event baseline (ck=2)
1695    //              //
1696    //              if ( !ctground ){
1697    //                if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] +=  qpre[l][m][ipre] * ctprecor[l][m][ipre];
1698    //              } else {
1699    //                if ( clevel1->estrip[j][m][l] != 0. ) clevel1->estrip[j][m][l] +=  qpre[l][m][ipre] * 0.00478;
1700    //              };
1701    //            };
1702              };              };
1703            };            };
1704          };          };
1705          //        };
1706          if ( ener0 == 0. && cbase0 == 0. && !pproblem && clevel2->perr[se] == 0){        //
1707            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);        Int_t j4 = -4;
1708            pproblem = true;        Int_t jjj = -3;
1709            pe++;        Int_t jj = -2;
1710          };        Int_t jjpre = -1;
1711          //        Int_t jjjpre = -1;
1712          Int_t j4 = -4;        memset(ene, 0, 96*sizeof(Float_t));
1713          Int_t jjj = -3;        for (Int_t j = 0 ; j < 100 ; j++){          
1714          Int_t jj = -2;          jj++;
1715          Int_t jjpre = -1;          jjj++;
1716          Int_t jjjpre = -1;          j4++;
1717          for (Int_t j = 0 ; j < 100 ; j++){                  if ( j < 96 ) ene[j] = clevel1->estrip[j][m][l];
1718            jj++;          if ( crosst ){
1719            jjj++;            //
1720            j4++;            // "Real" crosstalk effect on the neighbour strips respect to the one which have seen the energy deposit
1721            if ( j < 96 ) ene[j] = clevel1->estrip[j][m][l];            //
1722            if ( crosst ){            if ( jj >= 0 && jj < 96 ){
1723              if ( jj >= 0 && jj < 96 ){              if ( !ctground ){
1724                if ( !ctground ){                if ( jj%16 == 0 ) jjpre++;              
1725                  if ( jj%16 == 0 ) jjpre++;                            if ( jj != 0 && jj != 32 && jj != 64 && ene[jj-1] != 0. ) ene[jj-1] += -clevel1->estrip[jj][m][l] * ctneigcor[l][m][jjpre];
1726                  if ( jj != 0 && jj != 32 && jj != 64 ) ene[jj-1] += -clevel1->estrip[jj][m][l] * ctneigcor[l][m][jjpre];                if ( jj != 31 && jj != 63 && jj != 95 && ene[jj+1] != 0. ) ene[jj+1] += -clevel1->estrip[jj][m][l] * ctneigcor[l][m][jjpre];                      
1727                  if ( jj != 31 && jj != 63 && jj != 95 ) ene[jj+1] += -clevel1->estrip[jj][m][l] * ctneigcor[l][m][jjpre];                                    } else {
1728                } else {                if ( jj != 0 && jj != 32 && jj != 64 && ene[jj-1] != 0. ) ene[jj-1] += -clevel1->estrip[jj][m][l] * 0.01581;
1729                  if ( jj != 0 && jj != 32 && jj != 64 ) ene[jj-1] += -clevel1->estrip[jj][m][l] * 0.01581;                if ( jj != 31 && jj != 63 && jj != 95 && ene[jj+1] != 0. ) ene[jj+1] += -clevel1->estrip[jj][m][l] * 0.01581;                    
                 if ( jj != 31 && jj != 63 && jj != 95 ) ene[jj+1] += -clevel1->estrip[jj][m][l] * 0.01581;                        
               };  
1730              };              };
1731              if ( jjj >= 0 && jjj < 96 ){            };
1732                if ( !ctground ){            if ( jjj >= 0 && jjj < 96 ){
1733                  if ( jjj%16 == 0 ) jjjpre++;                        if ( !ctground ){
1734                  if ( jjj != 0 && jjj != 32 && jjj != 64 ) clevel1->estrip[jjj-1][m][l] +=  -ene[jjj] * ctneigcor[l][m][jjjpre];                if ( jjj%16 == 0 ) jjjpre++;            
1735                  if ( jjj != 31 && jjj != 63 && jjj != 95 ) clevel1->estrip[jjj+1][m][l] +=  -ene[jjj] * ctneigcor[l][m][jjjpre];                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];
1736                } else {                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];
1737                  if ( jjj != 0 && jjj != 32 && jjj != 64 ) clevel1->estrip[jjj-1][m][l] +=  -ene[jjj] * 0.01581;              } else {
1738                  if ( jjj != 31 && jjj != 63 && jjj != 95 ) clevel1->estrip[jjj+1][m][l] +=  -ene[jjj] * 0.01581;                if ( jjj != 0 && jjj != 32 && jjj != 64 && clevel1->estrip[jjj-1][m][l] != 0. ) clevel1->estrip[jjj-1][m][l] +=  -ene[jjj] * 0.01581;
1739                };                if ( jjj != 31 && jjj != 63 && jjj != 95 && clevel1->estrip[jjj+1][m][l] != 0. ) clevel1->estrip[jjj+1][m][l] +=  -ene[jjj] * 0.01581;
1740              };              };
1741            };            };
1742            if ( j4 >= 0 && j4 < 96 ){          };
1743              //          if ( j4 >= 0 && j4 < 96 ){
1744              // NOTICE: THE FOLLOWING LINE EXCLUDE ALL STRIPS FOR WHICH THE RMS*4 IS GREATER THAN 26 !!! <=============== IMPORTANT! =================>            //
1745              // CALOLEVEL1 CODING AND FILLING
1746              //
1747              //
1748              // NOTICE: THE FOLLOWING LINE EXCLUDE ALL STRIPS FOR WHICH THE RMS*4 IS GREATER THAN 26 !!! <=============== IMPORTANT! =================>
1749              //
1750              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] ){
1751                clevel1->estrip[j4][m][l] = 0.;
1752              };
1753              //
1754              // code and save the energy for each strip in svstrip    
1755              //
1756              if ( clevel1->estrip[j4][m][l] > clevel1->emin ){
1757              //              //
1758              if ( obadmask[l][m][j4] == 1 || clevel1->estrip[j4][m][l] <= clevel1->emin || calrms[l][m][j4] > 26 ){              Float_t savel1 = clevel1->estrip[j4][m][l];
1759                clevel1->estrip[j4][m][l] = 0.;              //      if ( dexyc[l][m][j4] == 32767. ){
1760                if ( dexyc[l][m][j4] > 32000. ){
1761                  savel1 += 5000.;
1762                  clevel2->nsatstrip += 1.;
1763              };              };
             //  
             // code and save the energy for each strip in svstrip  
1764              //              //
1765              if ( clevel1->estrip[j4][m][l] > clevel1->emin ){              tim = 100000.;
1766                //              plo = m;
1767                Float_t savel1 = clevel1->estrip[j4][m][l];              fbi = 0;
1768                if ( dexyc[l][m][j4] == 32767. ){              if ( savel1 > 0.99995 ){
1769                  savel1 += 5000.;                tim = 10000.;
                 clevel2->nsatstrip += 1.;  
               };  
               //  
               tim = 100000.;  
1770                plo = m;                plo = m;
1771                  fbi = 1;
1772                };
1773                if ( savel1 > 9.9995 ){
1774                  tim = 1000.;
1775                  plo = 22 + m;
1776                  fbi = 1;
1777                };
1778                if ( savel1 > 99.995 ){
1779                  tim = 100.;
1780                  plo = 22 + m;
1781                fbi = 0;                fbi = 0;
1782                if ( savel1 > 0.99995 ){              };
1783                  tim = 10000.;              if ( savel1 > 999.95 ){
1784                  plo = m;                tim = 10.;
1785                  fbi = 1;                plo = 44 + m;
1786                };                fbi = 0;
1787                if ( savel1 > 9.9995 ){              };
1788                  tim = 1000.;              if ( savel1 > 9999.5 ){
1789                  plo = 22 + m;                tim = 1.;
1790                  fbi = 1;                plo = 66 + m;
1791                };                fbi = 0;
1792                if ( savel1 > 99.995 ){              };
1793                  tim = 100.;              //
1794                  plo = 22 + m;              cle = (Int_t)lroundf(tim*savel1);
1795                  fbi = 0;              //
1796                };              if ( l == 0 ){
               if ( savel1 > 999.95 ){  
                 tim = 10.;  
                 plo = 44 + m;  
                 fbi = 0;  
               };  
               if ( savel1 > 9999.5 ){  
                 tim = 1.;  
                 plo = 66 + m;  
                 fbi = 0;  
               };  
               //  
               cle = (Int_t)lroundf(tim*savel1);  
               //  
               if ( l == 0 ){  
                 //  
                 // +-PPSSmmmm.mmmm  
                 //  
                 svstrip[istrip] = fbi*1000000000 + plo*10000000 + j4*100000 + cle;  
               } else {  
                 svstrip[istrip] = -(fbi*1000000000 + plo*10000000 + j4*100000 + cle);  
               };  
1797                //                //
1798                //              if ( ei >= -770 ) printf(" j %i l %i m %i estrip %f \n",j4,l,m,clevel1->estrip[j4][m][l]);                // +-PPSSmmmm.mmmm
               //              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);  
               //              if ( ei >= -770 ) printf(" svstrip %i \n",svstrip[istrip]);  
1799                //                //
1800                istrip++;                svstrip[istrip] = fbi*1000000000 + plo*10000000 + j4*100000 + cle;
1801                } else {
1802                  svstrip[istrip] = -(fbi*1000000000 + plo*10000000 + j4*100000 + cle);
1803              };              };
1804                //
1805                istrip++;
1806            };            };
1807          };          };
         //  
       } else {  
         for (Int_t nn = 0; nn < 96; nn++){                    
           clevel1->estrip[nn][m][l] = 0.;  
         };  
1808        };        };
1809          //
1810      };      };
1811    };    };
1812      //
1813      // store goodness flag
1814      //
1815    if ( !pe  ){    if ( !pe  ){
1816      clevel2->good = 1;      clevel2->good = 1;
1817    } else {    } else {
1818      clevel2->good = 0;      clevel2->good = 0;
1819    };    };
1820      //
1821      // done
1822      //
1823    return(0);    return(0);
1824  }  }
1825    
# Line 1362  void CaloLevel0::ClearStructs(){ Line 1946  void CaloLevel0::ClearStructs(){
1946    ClearCommonVar();    ClearCommonVar();
1947  }  }
1948    
1949    void CaloLevel0::Delete(Option_t *t){
1950      if ( de ) delete de;
1951      delete this;
1952    }
1953    
1954    
1955  void CaloLevel0::RunClose(){  void CaloLevel0::RunClose(){
1956    l0tr->Delete();    l0tr->Delete();
1957    ClearStructs();    ClearStructs();
# Line 1371  void CaloLevel0::RunClose(){ Line 1961  void CaloLevel0::RunClose(){
1961    memset(base, 0, 2*22*6*sizeof(Float_t));    memset(base, 0, 2*22*6*sizeof(Float_t));
1962    memset(sbase, 0, 2*22*6*sizeof(Float_t));    memset(sbase, 0, 2*22*6*sizeof(Float_t));
1963    memset(ctprecor, 0, 2*22*6*sizeof(Float_t));    memset(ctprecor, 0, 2*22*6*sizeof(Float_t));
1964      memset(ctsicor, 0, 2*22*9*sizeof(Float_t));
1965    memset(ctneigcor, 0, 2*22*6*sizeof(Float_t));    memset(ctneigcor, 0, 2*22*6*sizeof(Float_t));
1966    //    //
1967  }  }

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.25

  ViewVC Help
Powered by ViewVC 1.1.23