/[PAMELA software]/DarthVader/ToFLevel2/src/ToFLevel2.cpp
ViewVC logotype

Diff of /DarthVader/ToFLevel2/src/ToFLevel2.cpp

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

revision 1.6 by pam-fi, Fri Jul 21 11:02:28 2006 UTC revision 1.36 by mocchiut, Thu Apr 12 12:27:10 2012 UTC
# Line 1  Line 1 
1  #include <TObject.h>  /**
2     * \file ToFLevel2.cpp
3     * \author Gianfranca DeRosa, Wolfgang Menn
4     *
5     * WM dec 2008: Description of "GetdEdx" changed
6     * WM dec 2008: "GetdEdxPaddle" modified: Now includes saturation limit
7     *              PMTs higher than the saturation limit are not used for dEdx
8     * WM apr 2009: bug found by Nicola in method "GetPaddlePlane"
9     */
10    
11  #include <ToFLevel2.h>  #include <ToFLevel2.h>
 #include <iostream>  
12  using namespace std;  using namespace std;
13  ClassImp(ToFPMT);  ClassImp(ToFPMT);
14    ClassImp(ToFdEdx);
15    ClassImp(ToFGeom);
16  ClassImp(ToFTrkVar);  ClassImp(ToFTrkVar);
17  ClassImp(ToFLevel2);  ClassImp(ToFLevel2);
18    
# Line 10  ToFPMT::ToFPMT(){ Line 20  ToFPMT::ToFPMT(){
20    pmt_id = 0;    pmt_id = 0;
21    adc = 0.;    adc = 0.;
22    tdc_tw = 0.;    tdc_tw = 0.;
23      tdc = 0.;
24      l0flag_adc = 0.;
25      l0flag_tdc = 0.;
26  }  }
27    
28  ToFPMT::ToFPMT(const ToFPMT &t){  ToFPMT::ToFPMT(const ToFPMT &t){
29    pmt_id = t.pmt_id;    pmt_id = t.pmt_id;
30    adc = t.adc;    adc = t.adc;
31    tdc_tw = t.tdc_tw;    tdc_tw = t.tdc_tw;
32      tdc = t.tdc;
33  }  }
34    
35  void ToFPMT::Clear(){  void ToFPMT::Clear(Option_t *t){
36    pmt_id = 0;    pmt_id = 0;
37    adc = 0.;    adc = 0.;
38    tdc_tw = 0.;    tdc_tw = 0.;
39      tdc = 0.;
40  }  }
41    
42    
# Line 32  ToFTrkVar::ToFTrkVar() { Line 47  ToFTrkVar::ToFTrkVar() {
47    npmtadc = 0;    npmtadc = 0;
48    pmttdc = TArrayI(48);    pmttdc = TArrayI(48);
49    pmtadc = TArrayI(48);    pmtadc = TArrayI(48);
50      tdcflag = TArrayI(48); // gf: 30 Nov 2006
51      adcflag = TArrayI(48); // gf: 30 Nov 2006
52    dedx = TArrayF(48);    dedx = TArrayF(48);
53    //    //
54    //    //
55    memset(beta,  0, 13*sizeof(Float_t));    memset(beta,  0, 13*sizeof(Float_t));
56    memset(xtofpos,  0, 3*sizeof(Float_t));    memset(xtofpos,  0, 3*sizeof(Float_t));
57    memset(ytofpos,  0, 3*sizeof(Float_t));    memset(ytofpos,  0, 3*sizeof(Float_t));
58      memset(xtr_tof,  0, 6*sizeof(Float_t));
59      memset(ytr_tof,  0, 6*sizeof(Float_t));
60    //    //
61  };  };
62    
63  void ToFTrkVar::Clear() {  void ToFTrkVar::Clear(Option_t *t) {
64    trkseqno = 0;    trkseqno = 0;
65    npmttdc = 0;    npmttdc = 0;
66    npmtadc = 0;    npmtadc = 0;
67    pmttdc.Reset();    pmttdc.Reset();
68    pmtadc.Reset();    pmtadc.Reset();
69      tdcflag.Reset(); // gf: 30 Nov 2006
70      adcflag.Reset(); // gf: 30 Nov 2006
71    dedx.Reset();    dedx.Reset();
72    //    //
73    memset(beta,  0, 13*sizeof(Float_t));    memset(beta,  0, 13*sizeof(Float_t));
74    memset(xtofpos,  0, 3*sizeof(Float_t));    memset(xtofpos,  0, 3*sizeof(Float_t));
75    memset(ytofpos,  0, 3*sizeof(Float_t));    memset(ytofpos,  0, 3*sizeof(Float_t));
76      memset(xtr_tof,  0, 6*sizeof(Float_t));
77      memset(ytr_tof,  0, 6*sizeof(Float_t));
78    //    //
79  };  };
80    
# Line 63  ToFTrkVar::ToFTrkVar(const ToFTrkVar &t) Line 86  ToFTrkVar::ToFTrkVar(const ToFTrkVar &t)
86    npmtadc = t.npmtadc;    npmtadc = t.npmtadc;
87    (t.pmttdc).Copy(pmttdc);    (t.pmttdc).Copy(pmttdc);
88    (t.pmtadc).Copy(pmtadc);    (t.pmtadc).Copy(pmtadc);
89      (t.tdcflag).Copy(tdcflag); // gf: 30 Nov 2006
90      (t.adcflag).Copy(adcflag); // gf: 30 Nov 2006
91    (t.dedx).Copy(dedx);    (t.dedx).Copy(dedx);
92    //    //
93    memcpy(beta,t.beta,sizeof(beta));    memcpy(beta,t.beta,sizeof(beta));
94    memcpy(xtofpos,t.xtofpos,sizeof(xtofpos));    memcpy(xtofpos,t.xtofpos,sizeof(xtofpos));
95    memcpy(ytofpos,t.ytofpos,sizeof(ytofpos));    memcpy(ytofpos,t.ytofpos,sizeof(ytofpos));
96      memcpy(xtr_tof,t.xtr_tof,sizeof(xtr_tof));
97      memcpy(ytr_tof,t.ytr_tof,sizeof(ytr_tof));
98    //    //
99  };  };
100    
101  ToFLevel2::ToFLevel2() {      ToFLevel2::ToFLevel2() {    
102    //    //
103    PMT = new TClonesArray("ToFPMT",12);  //  PMT = new TClonesArray("ToFPMT",12); //ELENA
104    ToFTrk = new TClonesArray("ToFTrkVar",2);  //  ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA
105      PMT = 0; //ELENA
106      ToFTrk = 0; //ELENA
107      //
108      this->Clear();
109    //    //
   memset(tof_j_flag, 0, 6*sizeof(Int_t));  
110  };  };
111    
112  void ToFLevel2::Clear(){  void ToFLevel2::Set(){//ELENA
113        if(!PMT)PMT = new TClonesArray("ToFPMT",12); //ELENA
114        if(!ToFTrk)ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA
115    }//ELENA
116    
117    void ToFLevel2::Clear(Option_t *t){
118    //    //
119    ToFTrk->Clear();    if(ToFTrk)ToFTrk->Delete(); //ELENA
120    PMT->Clear();    if(PMT)PMT->Delete(); //ELENA
121    memset(tof_j_flag, 0, 6*sizeof(Int_t));    memset(tof_j_flag, 0, 6*sizeof(Int_t));
122      unpackError = 0;
123      unpackWarning = 0;
124    //    //
125  };  };
126    
127    void ToFLevel2::Delete(Option_t *t){ //ELENA
128      //
129      if(ToFTrk){
130          ToFTrk->Delete(); //ELENA
131          delete ToFTrk;  //ELENA
132      }
133      if(PMT){
134          PMT->Delete(); //ELENA
135          delete PMT; //ELENA
136      } //ELENA
137      //
138    }; //ELENA
139    
140  ToFTrkVar *ToFLevel2::GetToFTrkVar(Int_t itrk){  ToFTrkVar *ToFLevel2::GetToFTrkVar(Int_t itrk){
141    //        //    
142    if(itrk >= ntrk()){    if(itrk >= ntrk()){
# Line 95  ToFTrkVar *ToFLevel2::GetToFTrkVar(Int_t Line 145  ToFTrkVar *ToFLevel2::GetToFTrkVar(Int_t
145      return(NULL);      return(NULL);
146    }      }  
147    //    //
148      if(!ToFTrk)return 0; //ELENA
149    TClonesArray &t = *(ToFTrk);    TClonesArray &t = *(ToFTrk);
150    ToFTrkVar *toftrack = (ToFTrkVar*)t[itrk];    ToFTrkVar *toftrack = (ToFTrkVar*)t[itrk];
151    return toftrack;    return toftrack;
# Line 108  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit) Line 159  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit)
159      return(NULL);      return(NULL);
160    }      }  
161    //    //
162      if(!PMT)return 0; //ELENA
163    TClonesArray &t = *(PMT);    TClonesArray &t = *(PMT);
164    ToFPMT *tofpmt = (ToFPMT*)t[ihit];    ToFPMT *tofpmt = (ToFPMT*)t[ihit];
165    return tofpmt;    return tofpmt;
# Line 118  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit) Line 170  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit)
170  //--------------------------------------  //--------------------------------------
171  /**  /**
172   * Method to get the plane ID (11 12 21 22 31 32) from the plane index (0 1 2 3 4 5)   * Method to get the plane ID (11 12 21 22 31 32) from the plane index (0 1 2 3 4 5)
173     * @param Plane index (0,1,2,3,4,5).
174   */   */
175    Int_t  ToFLevel2::GetToFPlaneID(Int_t ip){    Int_t  ToFLevel2::GetToFPlaneID(Int_t ip){
176        if(ip>=0 && ip<6)return 10*((int)(ip/2+1.1))+(ip%2)+1;        if(ip>=0 && ip<6)return 10*((int)(ip/2+1.1))+(ip%2)+1;
# Line 125  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit) Line 178  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit)
178    };    };
179  /**  /**
180   * Method to get the plane index (0 1 2 3 4 5) from the plane ID (11 12 21 22 31 32)   * Method to get the plane index (0 1 2 3 4 5) from the plane ID (11 12 21 22 31 32)
181     * @param plane Plane ID (11, 12, 21, 22, 31, 32)
182   */   */
183    Int_t  ToFLevel2::GetToFPlaneIndex(Int_t plane_id){    Int_t  ToFLevel2::GetToFPlaneIndex(Int_t plane_id){
184        if(        if(
# Line 138  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit) Line 192  ToFPMT *ToFLevel2::GetToFPMT(Int_t ihit)
192        else return -1;        else return -1;
193    };    };
194  /**  /**
195   * Method to know if a given ToF paddle was hit, that is there is a TDC signal from both PMTs   * Method to know if a given ToF paddle was hit, that is there is a TDC signal
196     * from both PMTs. The method uses the "tof_j_flag" variable.
197   * @param plane Plane ID (11, 12, 21, 22, 31, 32) or Plane index (0,1,2,3,4,5).   * @param plane Plane ID (11, 12, 21, 22, 31, 32) or Plane index (0,1,2,3,4,5).
198   * @param paddle_id Paddle ID.   * @param paddle_id Paddle ID.
199   * @return 1 if the paddle was hit.   * @return 1 if the paddle was hit.
# Line 169  Int_t ToFLevel2::GetNHitPaddles(Int_t pl Line 224  Int_t ToFLevel2::GetNHitPaddles(Int_t pl
224      return npad;      return npad;
225  };  };
226    
227    /**
228     * Method to get the number of hit paddles on a ToF plane.
229     * @param plane Plane ID (11, 12, 21, 22, 31, 32) or Plane index (0,1,2,3,4,5).
230     */
231    Int_t ToFLevel2::GetTrueNHitPaddles(Int_t plane){
232        Int_t npad=0;
233        TClonesArray* Pmt = this->PMT;
234        int paddle[24];
235        memset(paddle,0, 24*sizeof(int));
236        for(int i=0; i<Pmt->GetEntries(); i++) {  //loop per vedere quale TOF è colpito
237          ToFPMT* pmthit = (ToFPMT*)Pmt->At(i);
238          int pplane = -1;
239          int ppaddle = -1;
240          GetPMTPaddle(pmthit->pmt_id,pplane,ppaddle);
241          if ( pplane == plane ) paddle[ppaddle]++;
242        }
243        for(int i=0;i<24;i++) if ( paddle[i]>0 ) npad++;
244    
245        return npad;
246    };
247    
248  Float_t ToFLevel2::GetdEdx(Int_t notrack, Int_t plane){  //wm Nov 08
249    //gf Apr 07
250    /**
251     * Method to get the mean dEdx from a ToF layer - ATTENTION:
252     * It will sum up the dEdx of all the paddles, but since by definition
253     * only the paddle hitted by the track gets a dEdx value and the other
254     * paddles are set to zero, the output is just the dEdx of the hitted
255     * paddle in each layer!
256     * The "adcfl" option is not very useful (an artificial dEdx is per
257     * definition= 1 mip and not a real measurement), anyway left in the code
258     * @param notrack Track Number
259     * @param plane Plane index (0,1,2,3,4,5)
260     * @param adcflag in the plane (100<-> independent of the adcflag; !=0&&!=100 <-> at least one PMT with adcflag!=0; )
261     */
262    Float_t ToFLevel2::GetdEdx(Int_t notrack, Int_t plane, Int_t adcfl){
263      //  printf("fiffi\n");
264    Float_t dedx = 0.;    Float_t dedx = 0.;
265    Int_t ip = 0;    Float_t PadEdx =0.;
266    Int_t pmt_id = 0;    Int_t SatWarning;
267    Int_t pl = 0;    Int_t pad=-1;
   if ( plane >= 6 ){  
     ip = GetToFPlaneIndex(plane);  
   } else {  
     ip = plane;  
   };  
268    //    //
269    ToFTrkVar *trk = GetToFTrkVar(notrack);    ToFTrkVar *trk = GetToFTrkVar(notrack);
270      if(!trk) return 0; //ELENA
271    //    //
272    for (Int_t i=0; i<trk->npmtadc; i++){    if ( trk->trkseqno == -1 ){ //standalone, only paddles along the track, or about...
273      //      //    printf("ciccio\n");
274      pmt_id = (trk->pmtadc).At(i);      Float_t xleft=0;
275      //      Float_t xright=0;
276      pl = GetPlaneIndex(pmt_id);      Float_t yleft=0;
277      //      Float_t yright=0;
278      if ( pl == ip ) dedx += (trk->dedx).At(i);        Float_t xtof_temp[6]={100.,100.,100.,100.,100.,100.};
279      //      Float_t ytof_temp[6]={100.,100.,100.,100.,100.,100.};
280    };      ToFTrkVar *t_tof = trk;
281        
282        xtof_temp[0]=t_tof->xtofpos[0];
283        ytof_temp[0]=t_tof->ytofpos[0];
284        xtof_temp[1]=t_tof->xtofpos[0];
285        ytof_temp[1]=t_tof->ytofpos[0];
286    
287        xtof_temp[2]=t_tof->xtofpos[1];
288        ytof_temp[2]=t_tof->ytofpos[1];
289        xtof_temp[3]=t_tof->xtofpos[1];
290        ytof_temp[3]=t_tof->ytofpos[1];
291    
292        xtof_temp[4]=t_tof->xtofpos[2];
293        ytof_temp[4]=t_tof->ytofpos[2];
294        xtof_temp[5]=t_tof->xtofpos[2];
295        ytof_temp[5]=t_tof->ytofpos[2];
296    
297        if(t_tof->xtofpos[0]<100. && t_tof->ytofpos[0]<100.){
298          xtof_temp[1]=t_tof->xtofpos[0];
299          ytof_temp[0]=t_tof->ytofpos[0];
300        }else if(t_tof->xtofpos[0]>=100. && t_tof->ytofpos[0]<100.){
301          ytof_temp[0]=t_tof->ytofpos[0];
302          this->GetPaddleGeometry(0,(Int_t)log2(this->tof_j_flag[0]),xleft, xright, yleft, yright);
303          xtof_temp[1]=xleft+2.55;
304        }else if(t_tof->ytofpos[0]>=100. && t_tof->xtofpos[0]<100.){
305          xtof_temp[1]=t_tof->xtofpos[0];
306          this->GetPaddleGeometry(1,(Int_t)log2(this->tof_j_flag[1]),xleft, xright, yleft, yright);
307          ytof_temp[0]=yleft+2.75;
308        }
309        
310        if(t_tof->xtofpos[1]<100. && t_tof->ytofpos[1]<100.){
311          xtof_temp[2]=t_tof->xtofpos[1];
312          ytof_temp[3]=t_tof->ytofpos[1];
313        }else if(t_tof->xtofpos[1]>=100. && t_tof->ytofpos[1]<100.){
314          ytof_temp[3]=t_tof->ytofpos[1];
315          this->GetPaddleGeometry(3,(Int_t)log2(this->tof_j_flag[3]),xleft, xright, yleft, yright);
316          xtof_temp[2]=xleft+4.5;
317        }else if(t_tof->ytofpos[1]>=100. && t_tof->xtofpos[1]<100.){
318          xtof_temp[2]=t_tof->xtofpos[1];
319          this->GetPaddleGeometry(2,(Int_t)log2(this->tof_j_flag[2]),xleft, xright, yleft, yright);
320          ytof_temp[3]=yleft+3.75;
321        }
322        
323        if(t_tof->xtofpos[2]<100. && t_tof->ytofpos[2]<100.){
324          xtof_temp[5]=t_tof->xtofpos[2];
325          ytof_temp[4]=t_tof->ytofpos[2];
326        }else if(t_tof->xtofpos[2]>=100. && t_tof->ytofpos[2]<100.){
327          ytof_temp[4]=t_tof->ytofpos[2];
328          this->GetPaddleGeometry(4,(Int_t)log2(this->tof_j_flag[4]),xleft, xright, yleft, yright);
329          xtof_temp[5]=xleft+3;
330        }else if(t_tof->ytofpos[2]>=100. && t_tof->xtofpos[2]<100.){
331          xtof_temp[5]=t_tof->xtofpos[2];
332          this->GetPaddleGeometry(5,(Int_t)log2(this->tof_j_flag[5]),xleft, xright, yleft, yright);
333          ytof_temp[4]=yleft+2.5;
334        }
335    
336        if ( (xtof_temp[0])> 17.85 && fabs(xtof_temp[0])<17.85+4. ) xtof_temp[0] =17.84;
337        if ( (xtof_temp[1])> 17.85 && fabs(xtof_temp[1])<17.85+4. ) xtof_temp[1] =17.84;
338        if ( (ytof_temp[0])> 13.75 && fabs(ytof_temp[0])<13.75+4. ) ytof_temp[0] =13.74;
339        if ( (ytof_temp[1])> 13.75 && fabs(ytof_temp[1])<13.75+4. ) ytof_temp[1] =13.74;
340        if ( (xtof_temp[0])< -17.85 && fabs(xtof_temp[0])>-(17.85+4.) ) xtof_temp[0] =-17.84;
341        if ( (xtof_temp[1])< -17.85 && fabs(xtof_temp[1])>-(17.85+4.) ) xtof_temp[1] =-17.84;
342        if ( (ytof_temp[0])< -13.75 && fabs(ytof_temp[0])>-(13.75+4.) ) ytof_temp[0] =-13.74;
343        if ( (ytof_temp[1])< -13.75 && fabs(ytof_temp[1])>-(13.75+4.) ) ytof_temp[1] =-13.74;
344    
345        if ( (xtof_temp[2])> 4.5 && (xtof_temp[2])<4.5+4. ) xtof_temp[2] =4.4;
346        if ( (xtof_temp[3])> 4.45 && (xtof_temp[3])<4.5+4. ) xtof_temp[3] =4.4;
347        if ( (ytof_temp[2])> 3.75 && (ytof_temp[2])<3.75+4. ) ytof_temp[2] =3.74;
348        if ( (ytof_temp[3])> 3.75 && (ytof_temp[3])<3.75+4. ) ytof_temp[3] =3.74;
349        if ( (xtof_temp[2])< -4.5 && (xtof_temp[2])>-(4.5+4.) ) xtof_temp[2] =-4.4;
350        if ( (xtof_temp[3])< -4.45 && (xtof_temp[3])>-(4.5+4.) ) xtof_temp[3] =-4.4;
351        if ( (ytof_temp[2])< -3.75 && (ytof_temp[2])>-(3.75+4.) ) ytof_temp[2] =-3.74;
352        if ( (ytof_temp[3])< -3.75 && (ytof_temp[3])>-(3.75+4.) ) ytof_temp[3] =-3.74;
353    
354        if ( (xtof_temp[4])> 6. && (xtof_temp[4])<6.+4. ) xtof_temp[2] =5.9;
355        if ( (xtof_temp[5])> 6. && (xtof_temp[5])<6.+4. ) xtof_temp[3] =5.9;
356        if ( (ytof_temp[4])> 5. && (ytof_temp[4])<5.+4. ) ytof_temp[2] =4.9;
357        if ( (ytof_temp[5])> 5. && (ytof_temp[5])<5.+4. ) ytof_temp[3] =4.9;
358        if ( (xtof_temp[4])< -6. && (xtof_temp[4])>-(6.+4.) ) xtof_temp[2] =-5.9;
359        if ( (xtof_temp[5])< -6. && (xtof_temp[5])>-(6.+4.) ) xtof_temp[3] =-5.9;
360        if ( (ytof_temp[4])< -5. && (ytof_temp[4])>-(5.+4.) ) ytof_temp[2] =-4.9;
361        if ( (ytof_temp[5])< -5. && (ytof_temp[5])>-(5.+4.) ) ytof_temp[3] =-4.9;
362    
363    
364        for (Int_t ii=0; ii<GetNPaddle(plane); ii++){
365          Int_t paddleid=ii;
366          pad = GetPaddleid(plane,paddleid);
367          Int_t IpaddleT=-1;
368          IpaddleT=this->GetPaddleIdOfTrack(xtof_temp[plane],ytof_temp[plane], plane,0.0);
369    //      printf("SSS %i %i %f %f  %f %f \n",IpaddleT,paddleid,xtof_temp[plane],ytof_temp[plane],t_tof->xtofpos[plane],t_tof->ytofpos[plane]);
370          //      if ( IpaddleT == paddleid || IpaddleT-1 == paddleid || IpaddleT+1 == paddleid ){
371          if ( IpaddleT == paddleid || GetTrueNHitPaddles(plane) == 1 ){
372            //IpaddleT-1 == paddleid || IpaddleT+1 == paddleid ){
373            GetdEdxPaddle(notrack, pad, adcfl, PadEdx, SatWarning);
374            dedx += PadEdx;
375          }
376        };
377      } else {
378        for (Int_t ii=0; ii<GetNPaddle(plane); ii++){
379          Int_t paddleid=ii;
380          pad = GetPaddleid(plane,paddleid);
381          GetdEdxPaddle(notrack, pad, adcfl, PadEdx, SatWarning);
382          dedx += PadEdx;
383    //      printf("TTT %i %i %f\n",paddleid,plane,PadEdx);
384        };
385      }
386    //    //
387    return(dedx);    return(dedx);
388  };  };
389    
390    /**
391     * Method to fill the ADC_C 4x12 matrix with the dEdx values and the TDC 4x12 matrix
392     * with the time-walk corrected TDC values.
393     * @param notrack Track Number
394     * @param adc  ADC_C matrix with dEdx values
395     * @param tdc  TDC matrix
396     */
397  void ToFLevel2::GetMatrix(Int_t notrack, Float_t adc[4][12], Float_t tdc[4][12]){  void ToFLevel2::GetMatrix(Int_t notrack, Float_t adc[4][12], Float_t tdc[4][12]){
398    //    //
399    for (Int_t aa=0; aa<4;aa++){    for (Int_t aa=0; aa<4;aa++){
# Line 211  void ToFLevel2::GetMatrix(Int_t notrack, Line 408  void ToFLevel2::GetMatrix(Int_t notrack,
408    Int_t kk = 0;    Int_t kk = 0;
409    //    //
410    ToFTrkVar *trk = GetToFTrkVar(notrack);    ToFTrkVar *trk = GetToFTrkVar(notrack);
411      if(!trk)return; //ELENA
412    //    //
413    for (Int_t i=0; i<trk->npmtadc; i++){    for (Int_t i=0; i<trk->npmtadc; i++){
414      //      //
# Line 224  void ToFLevel2::GetMatrix(Int_t notrack, Line 422  void ToFLevel2::GetMatrix(Int_t notrack,
422    for (Int_t i=0; i<npmt(); i++){    for (Int_t i=0; i<npmt(); i++){
423      //      //
424      ToFPMT *pmt = GetToFPMT(i);      ToFPMT *pmt = GetToFPMT(i);
425        if(!pmt)break; //ELENA
426      //      //
427      GetPMTIndex(pmt->pmt_id,hh,kk);      GetPMTIndex(pmt->pmt_id,hh,kk);
428      //      //
# Line 235  void ToFLevel2::GetMatrix(Int_t notrack, Line 434  void ToFLevel2::GetMatrix(Int_t notrack,
434  };  };
435    
436    
437    /**
438     * Method to get the plane index (0 - 5) for the PMT_ID as input
439     * @param pmt_id  PMT_ID (0 - 47)
440     */
441  Int_t ToFLevel2::GetPlaneIndex(Int_t pmt_id){  Int_t ToFLevel2::GetPlaneIndex(Int_t pmt_id){
442    TString pmtname = GetPMTName(pmt_id);    TString pmtname = GetPMTName(pmt_id);
443    pmtname.Resize(3);    pmtname.Resize(3);
# Line 249  Int_t ToFLevel2::GetPlaneIndex(Int_t pmt Line 451  Int_t ToFLevel2::GetPlaneIndex(Int_t pmt
451  };  };
452    
453    
454    /**
455     * Method to get the PMT_ID if the index (4,12) is given. We have 4 channels on
456     * each of the 12 half-boards, this method decodes which PMT is cables to which
457     * channel.
458     * @param hh Channel
459     * @param kk HalfBoard
460     */
461  Int_t ToFLevel2::GetPMTid(Int_t hh, Int_t kk){  Int_t ToFLevel2::GetPMTid(Int_t hh, Int_t kk){
462    //    //
463    short tof[4][24] = {    short tof[4][24] = {
# Line 277  Int_t ToFLevel2::GetPMTid(Int_t hh, Int_ Line 486  Int_t ToFLevel2::GetPMTid(Int_t hh, Int_
486    return ind;    return ind;
487  };  };
488    
 TString ToFLevel2::GetPMTName(Int_t ind){  
     
   TString pmtname = " ";  
     
   TString photoS[48] = {  
     "S11_1A", "S11_1B", "S11_2A", "S11_2B", "S11_3A", "S11_3B", "S11_4A", "S11_4B",  
     "S11_5A", "S11_5B", "S11_6A", "S11_6B", "S11_7A", "S11_7B", "S11_8A", "S11_8B",  
     "S12_1A", "S12_1B", "S12_2A", "S12_2B", "S12_3A", "S12_3B", "S12_4A", "S12_4B", "S12_5A",  "S12_5B", "S12_6A", "S12_6B",  
     "S21_1A", "S21_1B", "S21_2A", "S21_2B",  
     "S22_1A", "S22_1B", "S22_2A", "S22_2B",  
     "S31_1A", "S31_1B", "S31_2A", "S31_2B", "S31_3A", "S31_3B",  
     "S32_1A", "S32_1B", "S32_2A", "S32_2B", "S32_3A", "S32_3B"  
   };  
     
   
   pmtname = photoS[ind].Data();  
   
   return pmtname;  
 };  
   
489    
490    /**
491     * Method to get the PMT index if the PMT ID is given. This method is the
492     * "reverse" of method "GetPMTid"
493     * @param ind  PMT_ID (0 - 47)
494     * @param hb   HalfBoard
495     * @param ch   Channel
496     */
497  void ToFLevel2::GetPMTIndex(Int_t ind, Int_t &hb, Int_t &ch){  void ToFLevel2::GetPMTIndex(Int_t ind, Int_t &hb, Int_t &ch){
498    //    //
499    short tof[4][24] = {    short tof[4][24] = {
# Line 324  void ToFLevel2::GetPMTIndex(Int_t ind, I Line 520  void ToFLevel2::GetPMTIndex(Int_t ind, I
520    return;    return;
521  };  };
522    
523    
524    
525    //  wm Nov 08 revision - saturation values included
526    /// gf Apr 07
527    /**
528     * Method to get the dEdx from a given ToF paddle.
529     * If two PMTs are good, the mean dEdx of both PMTs is taken, otherwise
530     * just the dEdx of the "good" PMT. If both PMTs are above saturation => dEdx=1000
531     * @param notrack Track Number
532     * @param Paddle index (0,1,...,23).
533     * @param adcflag in the paddle (100<-> independent of the adcflag; !=0&&!=100 <-> at least one PMT with adcflag!=0; )
534     * @param PadEdx dEdx from a given ToF paddle
535     * @param SatWarning 1 if the PMT ios near saturation region (adcraw ~3000)
536     */
537    void ToFLevel2::GetdEdxPaddle(Int_t notrack, Int_t paddleid, Int_t adcfl, Float_t &PadEdx, Int_t &SatWarning){
538    
539      /*
540        Float_t  PMTsat[48] = {
541        3162.14, 3165.48, 3153.85, 3085.73, 3089.65, 3107.64, 3097.52, 3078.37,
542        3130.05, 3087.07, 3112.22, 3102.92, 3080.58, 3092.55, 3087.94, 3125.03,
543        3094.09, 3143.16, 3125.51, 3181.27, 3092.09, 3124.98, 3069.3, 3095.53,
544        3097.11, 3133.53, 3114.73, 3113.01, 3091.19, 3097.99, 3033.84, 3134.98,
545        3081.37, 3111.04, 3066.77, 3108.17, 3133, 3111.06, 3052.52, 3140.66,
546        3106.33, 3094.85, 3150.85, 3118.8, 3096.24, 3118.47,3111.36, 3117.11 } ;
547      */
548    
549      // new values from Napoli dec 2008
550      Float_t  PMTsat[48] = {
551        3176.35,3178.19,3167.38,3099.73,3117.00,3126.29,3111.44,3092.27,
552        3146.48,3094.41,3132.13,3115.37,3099.32,3110.97,3111.80,3143.14,
553        3106.72,3153.44,3136.00,3188.96,3104.73,3140.45,3073.18,3106.62,
554        3112.48,3146.92,3127.24,3136.52,3109.59,3112.89,3045.15,3147.26,
555        3095.92,3121.05,3083.25,3123.62,3150.92,3125.30,3067.60,3160.18,
556        3119.36,3108.92,3164.77,3133.64,3111.47,3131.98,3128.87,3135.56 };
557    
558      for (Int_t i=0; i<48;i++) PMTsat[i] = PMTsat[i] - 5.;  // safety margin
559    
560    
561      PadEdx = 0.;
562      //  SatWarning = 1000;
563      SatWarning = 0;   // 0=good, increase for each bad PMT
564    
565      Float_t dEdx[48] = {0};
566      Int_t pmt_id = -1;
567      Float_t adcraw[48];
568      //
569      ToFTrkVar *trk = GetToFTrkVar(notrack);
570      if(!trk) return; //ELENA
571      //
572    
573      Int_t pmtleft=-1;
574      Int_t pmtright=-1;
575      GetPaddlePMT(paddleid, pmtleft, pmtright);
576    
577      adcraw[pmtleft] = 4095;
578      adcraw[pmtright] = 4095;
579    
580      
581      for (Int_t jj=0; jj<npmt(); jj++){
582        
583        ToFPMT *pmt = GetToFPMT(jj);
584        if(!pmt)break; //ELENA
585        
586        pmt_id = pmt->pmt_id;
587        if(pmt_id==pmtleft){
588          adcraw[pmtleft] = pmt->adc;
589        }
590        
591        if(pmt_id==pmtright){
592          adcraw[pmtright] = pmt->adc;
593        }
594      }
595    
596      
597      for (Int_t i=0; i<trk->npmtadc; i++){
598    
599        if((trk->adcflag).At(i)==0 || adcfl==100){
600          if((trk->pmtadc).At(i) == pmtleft)dEdx[pmtleft] = (trk->dedx).At(i);
601          if((trk->pmtadc).At(i) == pmtright)dEdx[pmtright] = (trk->dedx).At(i);
602        }else{
603          if((trk->pmtadc).At(i) == pmtleft)dEdx[pmtleft] = 0.;
604          if((trk->pmtadc).At(i) == pmtright)dEdx[pmtright] = 0.;
605        }
606      }
607    
608    
609      //  if( adcraw[pmtleft] >3000 || adcraw[pmtright] >3000)SatWarning=1;  //old version
610    
611      // Increase SatWarning Counter for each PMT>Sat
612      if( adcraw[pmtleft] > PMTsat[pmtleft])SatWarning++;  
613      if( adcraw[pmtright] > PMTsat[pmtright])SatWarning++;
614    
615      // if ADC  > sat set dEdx=1000
616      if( adcraw[pmtleft] > PMTsat[pmtleft]) dEdx[pmtleft] = 1000.;
617      if( adcraw[pmtright] > PMTsat[pmtright]) dEdx[pmtright] = 1000. ;
618    
619      // if two PMT are good, take mean dEdx, otherwise only the good dEdx
620      if(dEdx[pmtleft]<1000 && dEdx[pmtright]<1000) PadEdx = (dEdx[pmtleft]+dEdx[pmtright])*0.5;
621      if(dEdx[pmtleft]==1000 && dEdx[pmtright]<1000) PadEdx = dEdx[pmtright];  
622      if(dEdx[pmtleft]<1000 && dEdx[pmtright]==1000) PadEdx = dEdx[pmtleft];
623      
624    };
625    //
626    
627    
628    // gf Apr 07
629    
630    /**
631     * Method to get the PMT name (like "S11_1A") if the PMT_ID is given.
632     * Indexes of corresponding  plane, paddle and  pmt are also given as output.
633     * @param ind  PMT_ID (0 - 47)
634     * @param iplane plane index (0 - 5)
635     * @param ipaddle paddle index (relative to the plane)
636     * @param ipmt pmt index (0(A), 1(B))
637     */
638    TString ToFLevel2::GetPMTName(Int_t ind, Int_t &iplane, Int_t &ipaddle,Int_t &ipmt){
639      
640      TString pmtname = " ";
641      
642      TString photoS[48] = {
643        "S11_1A", "S11_1B", "S11_2A", "S11_2B", "S11_3A", "S11_3B", "S11_4A",
644        "S11_4B",
645        "S11_5A", "S11_5B", "S11_6A", "S11_6B", "S11_7A", "S11_7B", "S11_8A",
646        "S11_8B",
647        "S12_1A", "S12_1B", "S12_2A", "S12_2B", "S12_3A", "S12_3B", "S12_4A",
648        "S12_4B", "S12_5A",  "S12_5B", "S12_6A", "S12_6B",
649        "S21_1A", "S21_1B", "S21_2A", "S21_2B",
650        "S22_1A", "S22_1B", "S22_2A", "S22_2B",
651        "S31_1A", "S31_1B", "S31_2A", "S31_2B", "S31_3A", "S31_3B",
652        "S32_1A", "S32_1B", "S32_2A", "S32_2B", "S32_3A", "S32_3B"
653      };
654      
655      
656      pmtname = photoS[ind].Data();
657      
658      TString ss = pmtname(1,2);
659      iplane  = (int)(atoi(ss.Data())/10)*2-3+atoi(ss.Data())%10;
660      ss = pmtname(4);
661      ipaddle = atoi(ss.Data())-1 ;
662      if( pmtname.Contains("A") )ipmt=0;
663      if( pmtname.Contains("B") )ipmt=1;
664      
665      return pmtname;
666    };
667    /**
668     * Method to get the PMT name (like "S11_1A") if the PMT_ID is given
669     * @param ind  PMT_ID (0 - 47)
670     */
671    TString ToFLevel2::GetPMTName(Int_t ind){
672    
673      Int_t iplane  = -1;
674      Int_t ipaddle = -1;
675      Int_t ipmt    = -1;
676      return GetPMTName(ind,iplane,ipaddle,ipmt);
677      
678    };
679    
680    // wm jun 08
681    Int_t ToFLevel2::GetPaddleIdOfTrack(Float_t xtr, Float_t ytr, Int_t plane){
682    return GetPaddleIdOfTrack(xtr ,ytr ,plane, 0.4);
683    }
684    
685    // gf Apr 07
686    Int_t ToFLevel2::GetPaddleIdOfTrack(Float_t xtr, Float_t ytr, Int_t plane, Float_t margin){
687      
688      Double_t xt,yt,xl,xh,yl,yh;
689      
690      Float_t tof11_x[8] = {-17.85,-12.75,-7.65,-2.55,2.55,7.65,12.75,17.85};
691      Float_t tof12_y[6] = { -13.75,-8.25,-2.75,2.75,8.25,13.75};
692      Float_t tof21_y[2] = { 3.75,-3.75};
693      Float_t tof22_x[2] = { -4.5,4.5};
694      Float_t tof31_x[3] = { -6.0,0.,6.0};
695      Float_t tof32_y[3] = { -5.0,0.0,5.0};
696      
697      //  S11 8 paddles  33.0 x 5.1 cm
698      //  S12 6 paddles  40.8 x 5.5 cm
699      //  S21 2 paddles  18.0 x 7.5 cm
700      //  S22 2 paddles  15.0 x 9.0 cm
701      //  S31 3 paddles  15.0 x 6.0 cm
702      //  S32 3 paddles  18.0 x 5.0 cm
703      
704      Int_t paddleidoftrack=-1;
705      //
706      
707      //--- S11 ------
708      
709      if(plane==0){
710        xt = xtr;
711        yt = ytr;
712        paddleidoftrack=-1;
713        yl = -33.0/2. ;
714        yh =  33.0/2. ;
715        if ((yt>yl)&&(yt<yh)) {
716          for (Int_t i1=0; i1<8;i1++){
717            xl = tof11_x[i1] - (5.1-margin)/2. ;
718            xh = tof11_x[i1] + (5.1-margin)/2. ;
719            if ((xt>xl)&&(xt<xh))  paddleidoftrack=i1;
720          }
721        }
722      }
723      //      cout<<"S11  "<<paddleidoftrack[0]<<"\n";
724      
725      //--- S12 -------
726      if(plane==1){
727        xt = xtr;
728        yt = ytr;
729        paddleidoftrack=-1;
730        xl = -40.8/2. ;
731        xh =  40.8/2. ;
732        
733        if ((xt>xl)&&(xt<xh)) {
734          for (Int_t i1=0; i1<6;i1++){
735            yl = tof12_y[i1] - (5.5-margin)/2. ;
736            yh = tof12_y[i1] + (5.5-margin)/2. ;
737            if ((yt>yl)&&(yt<yh))  paddleidoftrack=i1;
738          }
739        }
740      }
741      
742      //--- S21 ------
743    
744      if(plane==2){
745        xt = xtr;
746        yt = ytr;
747        paddleidoftrack=-1;
748        xl = -18./2. ;
749        xh =  18./2. ;
750        
751        if ((xt>xl)&&(xt<xh)) {
752          for (Int_t i1=0; i1<2;i1++){
753            yl = tof21_y[i1] - (7.5-margin)/2. ;
754            yh = tof21_y[i1] + (7.5-margin)/2. ;
755            if ((yt>yl)&&(yt<yh))  paddleidoftrack=i1;
756          }
757        }
758      }
759      
760      //--- S22 ------
761      if(plane==3){
762        xt = xtr;
763        yt = ytr;
764        paddleidoftrack=-1;
765        yl = -15./2. ;
766        yh =  15./2. ;
767        
768        if ((yt>yl)&&(yt<yh)) {
769          for (Int_t i1=0; i1<2;i1++){
770            xl = tof22_x[i1] - (9.0-margin)/2. ;
771            xh = tof22_x[i1] + (9.0-margin)/2. ;
772            if ((xt>xl)&&(xt<xh))  paddleidoftrack=i1;
773          }
774        }
775      }  
776      
777      //--- S31 ------
778      if(plane==4){
779        xt = xtr;
780        yt = ytr;
781        paddleidoftrack=-1;
782        yl = -15.0/2. ;
783        yh =  15.0/2. ;
784        
785        if ((yt>yl)&&(yt<yh)) {
786          for (Int_t i1=0; i1<3;i1++){
787            xl = tof31_x[i1] - (6.0-margin)/2. ;
788            xh = tof31_x[i1] + (6.0-margin)/2. ;
789            if ((xt>xl)&&(xt<xh))  paddleidoftrack=i1;
790          }
791        }
792      }  
793      
794      //---  S32 ------
795      if(plane==5){
796        xt = xtr;
797        yt = ytr;
798        paddleidoftrack=-1;
799        xl = -18.0/2. ;
800        xh =  18.0/2. ;
801        
802        if ((xt>xl)&&(xt<xh)) {
803          for (Int_t i1=0; i1<3;i1++){
804            yl = tof32_y[i1] - (5.0-margin)/2. ;
805            yh = tof32_y[i1] + (5.0-margin)/2. ;
806            if ((yt>yl)&&(yt<yh)) paddleidoftrack=i1;
807          }
808        }
809      }
810      
811      return paddleidoftrack;
812    
813    }  
814    
815    //
816    
817    // gf Apr 07
818    
819    void ToFLevel2::GetPMTPaddle(Int_t pmt_id, Int_t &plane, Int_t &paddle){
820      
821      plane = GetPlaneIndex(pmt_id);
822    
823      if(plane == 0){
824        if(pmt_id==0 || pmt_id==1)paddle=0;
825        if(pmt_id==2 || pmt_id==3)paddle=1;
826        if(pmt_id==4 || pmt_id==5)paddle=2;
827        if(pmt_id==6 || pmt_id==7)paddle=3;
828        if(pmt_id==8 || pmt_id==9)paddle=4;
829        if(pmt_id==10 || pmt_id==11)paddle=5;
830        if(pmt_id==12 || pmt_id==13)paddle=6;
831        if(pmt_id==14 || pmt_id==15)paddle=7;
832      }
833      
834      if(plane == 1){
835        if(pmt_id==16 || pmt_id==17)paddle=0;
836        if(pmt_id==18 || pmt_id==19)paddle=1;
837        if(pmt_id==20 || pmt_id==21)paddle=2;
838        if(pmt_id==22 || pmt_id==23)paddle=3;
839        if(pmt_id==24 || pmt_id==25)paddle=4;
840        if(pmt_id==26 || pmt_id==27)paddle=5;
841      }
842      
843      if(plane == 2){
844        if(pmt_id==28 || pmt_id==29)paddle=0;
845        if(pmt_id==30 || pmt_id==31)paddle=1;
846      }
847      
848      if(plane == 3){
849        if(pmt_id==32 || pmt_id==33)paddle=0;
850        if(pmt_id==34 || pmt_id==35)paddle=1;
851      }
852      
853      if(plane == 4){
854        if(pmt_id==36 || pmt_id==37)paddle=0;
855        if(pmt_id==38 || pmt_id==39)paddle=1;
856        if(pmt_id==40 || pmt_id==41)paddle=2;
857      }
858      
859      if(plane == 5){
860        if(pmt_id==42 || pmt_id==43)paddle=0;
861        if(pmt_id==44 || pmt_id==45)paddle=1;
862        if(pmt_id==46 || pmt_id==47)paddle=2;
863      }
864      return;
865    }
866    
867    //
868    
869    // gf Apr 07
870    
871    void ToFLevel2::GetPaddlePMT(Int_t paddle, Int_t &pmtleft, Int_t &pmtright){
872      pmtleft=paddle*2;
873      pmtright= pmtleft+1;  
874      return;
875    }
876    
877    //
878    
879    
880    
881    // // gf Apr 07
882    
883    void ToFLevel2::GetPaddleGeometry(Int_t plane, Int_t paddle, Float_t &xleft, Float_t &xright, Float_t &yleft, Float_t &yright){
884      
885      Int_t i1;
886    
887      Float_t tof11_x[8] = {-17.85,-12.75,-7.65,-2.55,2.55,7.65,12.75,17.85};
888      Float_t tof12_y[6] = { -13.75,-8.25,-2.75,2.75,8.25,13.75};
889      Float_t tof21_y[2] = { 3.75,-3.75};
890      Float_t tof22_x[2] = { -4.5,4.5};
891      Float_t tof31_x[3] = { -6.0,0.,6.0};
892      Float_t tof32_y[3] = { -5.0,0.0,5.0};
893            
894      //  S11 8 paddles  33.0 x 5.1 cm
895      //  S12 6 paddles  40.8 x 5.5 cm
896      //  S21 2 paddles  18.0 x 7.5 cm
897      //  S22 2 paddles  15.0 x 9.0 cm
898      //  S31 3 paddles  15.0 x 6.0 cm
899      //  S32 3 paddles  18.0 x 5.0 cm
900    
901      if(plane==0)
902        {
903          for (i1=0; i1<8;i1++){
904            if(i1 == paddle){
905              xleft = tof11_x[i1] - 5.1/2.;
906              xright = tof11_x[i1] + 5.1/2.;
907              yleft = -33.0/2.;
908              yright = 33.0/2.;
909            }
910          }
911        }
912      
913      if(plane==1)
914        {
915          for (i1=0; i1<6;i1++){
916            if(i1 == paddle){
917              xleft = -40.8/2.;
918              xright = 40.8/2.;
919              yleft = tof12_y[i1] - 5.5/2.;
920              yright = tof12_y[i1] + 5.5/2.;
921            }
922          }
923        }
924    
925      if(plane==2)
926        {
927          for (i1=0; i1<2;i1++){
928            if(i1 == paddle){
929              xleft =  -18./2.;
930              xright = 18./2.;
931              yleft = tof21_y[i1] - 7.5/2.;
932              yright = tof21_y[i1] + 7.5/2.;
933            }
934          }
935        }
936      
937      if(plane==3)
938        {
939          for (i1=0; i1<2;i1++){
940            if(i1 == paddle){
941              xleft = tof22_x[i1] - 9.0/2.;
942              xright = tof22_x[i1] + 9.0/2.;
943              yleft = -15./2.;
944              yright = 15./2.;
945            }
946          }
947        }
948    
949    
950      if(plane==4)
951        {
952          for (i1=0; i1<3;i1++){
953            if(i1 == paddle){
954              xleft = tof31_x[i1] - 6.0/2.;
955              xright = tof31_x[i1] + 6.0/2.;
956              yleft = -15./2.;
957              yright = 15./2.;
958            }
959          }
960        }
961    
962      if(plane==5)
963        {
964          for (i1=0; i1<3;i1++){
965            if(i1 == paddle){
966              xleft = -18.0/2.;
967              xright = 18.0/2.;
968              yleft = tof32_y[i1] - 5.0/2.;
969              yright = tof32_y[i1] + 5.0/2.;
970            }
971          }
972        }
973      return;
974    }
975    
976    // gf Apr 07
977    /**
978     * Method to get the paddle index (0,...23) if the plane ID and the paddle id in the plane is given.
979     * This method is the
980     * "reverse" of method "GetPaddlePlane"
981     * @param plane    (0 - 5)
982     * @param paddle   (plane=0, paddle = 0,...5)
983     * @param padid    (0 - 23)
984     */
985    Int_t ToFLevel2::GetPaddleid(Int_t plane, Int_t paddle)
986    {
987      Int_t padid=-1;
988      Int_t pads[6]={8,6,2,2,3,3};
989    
990      int somma=0;
991      int np=plane;
992      for(Int_t j=0; j<np; j++){
993        somma+=pads[j];
994      }
995      padid=paddle+somma;
996      return padid;
997    
998    }
999    
1000    
1001    // gf Apr 07
1002    /**
1003     * Method to get the plane ID and the paddle id in the plane if the paddle index (0,...23) is given.
1004     * This method is the
1005     * "reverse" of method "GetPaddleid"
1006     * @param pad      (0 - 23)
1007     * @param plane    (0 - 5)
1008     * @param paddle   (plane=0, paddle = 0,...5)
1009     */
1010    void ToFLevel2::GetPaddlePlane(Int_t pad, Int_t &plane, Int_t &paddle)
1011    {
1012    
1013      Int_t pads11=8;
1014      Int_t pads12=6;
1015      Int_t pads21=2;
1016      Int_t pads22=2;
1017      Int_t pads31=3;
1018      // Int_t pads32=3;
1019    
1020      if(pad<8){
1021        plane=0;
1022        paddle=pad;
1023        return;
1024      }
1025    
1026      if((7<pad)&&(pad<14)){
1027        plane=1;
1028        paddle=pad-pads11;
1029        return;
1030      }
1031      
1032      if((13<pad)&&(pad<16)){
1033        plane=2;
1034        paddle=pad-pads11-pads12;
1035        return;
1036      }
1037    
1038      if((15<pad)&&(pad<18)){
1039        plane=3;
1040        paddle=pad-pads11-pads12-pads21;
1041        return;
1042      }
1043    
1044      if((17<pad)&&(pad<21)){
1045        plane=4;
1046        paddle=pad-pads11-pads12-pads21-pads22;
1047        return;
1048      }
1049    
1050      if((20<pad)&&(pad<24)){
1051        plane=5;
1052        paddle=pad-pads11-pads12-pads21-pads22-pads31;
1053        return;
1054      }  
1055    
1056    }
1057    
1058    
1059    Int_t ToFLevel2::GetNPaddle(Int_t plane){
1060    
1061      Int_t npaddle=-1;
1062    
1063      Int_t pads11=8;
1064      Int_t pads12=6;
1065      Int_t pads21=2;
1066      Int_t pads22=2;
1067      Int_t pads31=3;
1068      Int_t pads32=3;
1069    
1070      if(plane==0)npaddle=pads11;
1071      if(plane==1)npaddle=pads12;
1072      if(plane==2)npaddle=pads21;
1073      if(plane==3)npaddle=pads22;
1074      if(plane==4)npaddle=pads31;
1075      if(plane==5)npaddle=pads32;
1076    
1077      return npaddle;
1078    
1079    }
1080    
1081    
1082    
1083    /// wm feb 08
1084    
1085    /**
1086     * Method to calculate Beta from the 12 single measurements
1087     * we check the individual weights for artificial TDC values, then calculate
1088     * am mean beta for the first time. In a second step we loop again through
1089     * the single measurements, checking for the residual from the mean
1090     * The cut on the residual reject measurements > "x"-sigma. A chi2 value is
1091     * calculated, furthermore a "quality" value by adding the weights which
1092     * are finally used. If all measurements are taken, "quality" will be = 22.47.
1093     * A chi2 cut around 3-4 and a quality-cut > 20 is needed for clean beta
1094     * measurements like antiprotons etc.
1095     * The Level2 output is derived in the fortran routines using: 10.,10.,20.
1096     * @param notrack Track Number
1097     * @param cut on residual: difference between single measurement and mean
1098     * @param cut on "quality"
1099     * @param cut on chi2
1100     */
1101    
1102    Float_t ToFLevel2::CalcBeta(Int_t notrack, Float_t resmax, Float_t qualitycut, Float_t chi2cut){
1103    
1104    //  cout<<" in CalcBeta "<<resmax<<" "<<chi2cut<<" "<<qualitycut<<endl;
1105    
1106      Float_t bxx = 100.;
1107      //
1108      ToFTrkVar *trk = GetToFTrkVar(notrack);
1109      if(!trk) return 0; //ELENA
1110    
1111    
1112      Float_t chi2,xhelp,beta_mean;
1113      Float_t w_i[12],quality,sw,sxw,res,betachi,beta_mean_inv;
1114      Float_t b[12],tdcfl;
1115      Int_t  pmt_id,pmt_plane;
1116    
1117      for (Int_t i=0; i<12; i++){
1118        b[i] = trk->beta[i];
1119                                  }
1120          
1121    
1122    //========================================================================
1123    //---  Find out ToF layers with artificial TDC values & fill vector    ---
1124    //========================================================================
1125    
1126    Float_t  w_il[6];
1127    
1128         for (Int_t jj=0; jj<6;jj++) {
1129             w_il[jj] = 1000.;
1130                                     }
1131    
1132    
1133      for (Int_t i=0; i<trk->npmttdc; i++){
1134        //
1135        pmt_id = (trk->pmttdc).At(i);
1136        pmt_plane = GetPlaneIndex(pmt_id);
1137        tdcfl = (trk->tdcflag).At(i);
1138        if (w_il[pmt_plane] != 1.) w_il[pmt_plane] = tdcfl; //tdcflag
1139                                         };
1140      
1141    //========================================================================
1142    //---  Set weights for the 12 measurements using information for top and bottom:
1143    //---  if no measurements: weight = set to very high value=> not used
1144    //---  top or bottom artificial: weight*sqrt(2)
1145    //---  top and bottom artificial: weight*sqrt(2)*sqrt(2)
1146    //========================================================================
1147    
1148    Int_t itop[12] = {0,0,1,1,2,2,3,3,0,0,1,1};
1149    Int_t ibot[12] = {4,5,4,5,4,5,4,5,2,3,2,3};
1150    
1151         xhelp= 1E09;
1152      
1153         for (Int_t jj=0; jj<12;jj++) {
1154         if (jj<4)           xhelp = 0.11;    // S1-S3
1155         if ((jj>3)&&(jj<8)) xhelp = 0.18;    // S2-S3
1156         if (jj>7)           xhelp = 0.28;    // S1-S2
1157         if ((w_il[itop[jj]] == 1000.) && (w_il[ibot[jj]] == 1000.)) xhelp = 1E09;
1158         if ((w_il[itop[jj]] == 1) || (w_il[ibot[jj]] == 1.)) xhelp = xhelp*1.414 ;
1159         if ((w_il[itop[jj]] == 1) && (w_il[ibot[jj]] == 1.)) xhelp = xhelp*2. ;
1160    
1161         w_i[jj] = 1./xhelp;
1162                                      }
1163    
1164    
1165    //========================================================================
1166    //--- Calculate mean beta for the first time -----------------------------
1167    //--- We are using "1/beta" since its error is gaussian ------------------
1168    //========================================================================
1169    
1170          Int_t icount=0;
1171          sw=0.;
1172          sxw=0.;
1173          beta_mean=100.;
1174    
1175              for (Int_t jj=0; jj<12;jj++){
1176            if ((fabs(1./b[jj])>0.1)&&(fabs(1./b[jj])<15.))
1177             {
1178                icount= icount+1;
1179                sxw=sxw + (1./b[jj])*w_i[jj]*w_i[jj] ;
1180                sw =sw + w_i[jj]*w_i[jj] ;
1181    
1182             }
1183             }
1184    
1185          if (icount>0) beta_mean=1./(sxw/sw);
1186          beta_mean_inv = 1./beta_mean;
1187    
1188    //========================================================================
1189    //--- Calculate beta for the second time, use residuals of the single
1190    //--- measurements to get a chi2 value
1191    //========================================================================
1192    
1193          icount=0;
1194          sw=0.;
1195          sxw=0.;
1196          betachi = 100.;
1197          chi2 = 0.;
1198          quality=0.;
1199    
1200    
1201              for (Int_t jj=0; jj<12;jj++){
1202           if ((fabs(1./b[jj])>0.1)&&(fabs(1./b[jj])<15.)&&(w_i[jj]>0.01)) {
1203                res = beta_mean_inv - (1./b[jj]) ;
1204                if (fabs(res*w_i[jj])<resmax)          {;
1205                chi2 = chi2 + pow((res*w_i[jj]),2) ;
1206                icount= icount+1;
1207                sxw=sxw + (1./b[jj])*w_i[jj]*w_i[jj] ;
1208                sw =sw + w_i[jj]*w_i[jj] ;
1209                                                   }
1210                                                                            }
1211                                          }
1212          quality = sqrt(sw) ;
1213    
1214          if (icount==0) chi2 = 1000.;
1215          if (icount>0) chi2 = chi2/(icount) ;
1216          if (icount>0) betachi=1./(sxw/sw);
1217    
1218       bxx = 100.;
1219       if ((chi2 < chi2cut)&&(quality>qualitycut)) bxx = betachi;
1220      //
1221      return(bxx);
1222    };
1223    
1224    
1225    ////////////////////////////////////////////////////
1226    ////////////////////////////////////////////////////
1227    
1228    
1229  /**  /**
1230   * Fills a struct cToFLevel2 with values from a ToFLevel2 object (to put data into a F77 common).   * Fills a struct cToFLevel2 with values from a ToFLevel2 object (to put data into a F77 common).
1231   */   */
# Line 332  void ToFLevel2::GetLevel2Struct(cToFLeve Line 1234  void ToFLevel2::GetLevel2Struct(cToFLeve
1234    for(Int_t i=0;i<6;i++)    for(Int_t i=0;i<6;i++)
1235      l2->tof_j_flag[i]=tof_j_flag[i];      l2->tof_j_flag[i]=tof_j_flag[i];
1236    
1237    l2->ntoftrk = ToFTrk->GetEntries();    if(ToFTrk){ //ELENA
1238    for(Int_t j=0;j<l2->ntoftrk;j++){        l2->ntoftrk = ToFTrk->GetEntries();
1239      l2->toftrkseqno[j]= ((ToFTrkVar*)ToFTrk->At(j))->toftrkseqno;        for(Int_t j=0;j<l2->ntoftrk;j++){
1240      l2->npmttdc[j]= ((ToFTrkVar*)ToFTrk->At(j))->npmttdc;            l2->toftrkseqno[j]= ((ToFTrkVar*)ToFTrk->At(j))->trkseqno;
1241      for(Int_t i=0;i<l2->npmttdc[j];i++)            l2->npmttdc[j]= ((ToFTrkVar*)ToFTrk->At(j))->npmttdc;
1242        l2->pmttdc[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->pmttdc.At(i);            for(Int_t i=0;i<l2->npmttdc[j];i++){
1243      for(Int_t i=0;i<13;i++)                l2->pmttdc[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->pmttdc.At(i);
1244        l2->beta[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->beta[i];                l2->tdcflag[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->tdcflag.At(i); // gf: 30 Nov 2006
1245              }
1246      l2->npmtadc[j]= ((ToFTrkVar*)ToFTrk->At(j))->npmtadc;            for(Int_t i=0;i<13;i++)
1247      for(Int_t i=0;i<l2->npmtadc[j];i++){                l2->beta[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->beta[i];
1248        l2->pmtadc[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->pmtadc.At(i);            
1249        l2->dedx[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->dedx.At(i);            l2->npmtadc[j]= ((ToFTrkVar*)ToFTrk->At(j))->npmtadc;
1250      }            for(Int_t i=0;i<l2->npmtadc[j];i++){
1251      for(Int_t i=0;i<3;i++){                l2->pmtadc[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->pmtadc.At(i);
1252        l2->xtofpos[i][j]=((ToFTrkVar*)ToFTrk->At(j))->xtofpos[i];                l2->adcflag[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->adcflag.At(i); // gf: 30 Nov 2006
1253        l2->ytofpos[i][j]=((ToFTrkVar*)ToFTrk->At(j))->ytofpos[i];                l2->dedx[i][j] = ((ToFTrkVar*)ToFTrk->At(j))->dedx.At(i);
1254              }
1255              for(Int_t i=0;i<3;i++){
1256                  l2->xtofpos[i][j]=((ToFTrkVar*)ToFTrk->At(j))->xtofpos[i];
1257                  l2->ytofpos[i][j]=((ToFTrkVar*)ToFTrk->At(j))->ytofpos[i];
1258              }
1259              for(Int_t i=0;i<6;i++){
1260                  l2->xtr_tof[i][j]=((ToFTrkVar*)ToFTrk->At(j))->xtr_tof[i];
1261                  l2->ytr_tof[i][j]=((ToFTrkVar*)ToFTrk->At(j))->ytr_tof[i];
1262              }
1263          }
1264      } //ELENA
1265        
1266      if(PMT){ //ELENA
1267          l2->npmt = PMT->GetEntries();
1268          for(Int_t j=0;j<l2->npmt;j++){
1269              l2->pmt_id[j] = ((ToFPMT*)PMT->At(j))->pmt_id;
1270              l2->adc[j] =((ToFPMT*)PMT->At(j))->adc;
1271              l2->tdc_tw[j] =((ToFPMT*)PMT->At(j))->tdc_tw;
1272          }
1273      } //ELENA
1274    }
1275    
1276    
1277    //
1278    // Reprocessing tool // Emiliano 08/04/07
1279    //
1280    Int_t ToFLevel2::Process(TrkLevel2 *trk, TrigLevel2 *trg, GL_RUN *run, OrbitalInfo *orb, Bool_t force){
1281      //
1282      // Copiare qui qualcosa di simile a calonuclei per evitare di riprocessare sempre tutto
1283      //
1284      printf("\n\n\n ERROR: NOT IMPLEMENTED ANYMORE, write Emiliano if you need this method (Emiliano.Mocchiutti@ts.infn.it) \n\n\n");
1285      return(-1);
1286      //   //
1287      //   // structures to communicate with F77
1288      //   //
1289      //   extern struct ToFInput  tofinput_;
1290    //   extern struct ToFOutput tofoutput_;
1291    //   //
1292    //   // DB connection
1293    //   //
1294    //   TString host;
1295    //   TString user;
1296    //   TString psw;
1297    //   const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
1298    //   const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
1299    //   const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
1300    //   if ( !pamdbhost ) pamdbhost = "";
1301    //   if ( !pamdbuser ) pamdbuser = "";
1302    //   if ( !pamdbpsw ) pamdbpsw = "";
1303    //   if ( strcmp(pamdbhost,"") ) host = pamdbhost;
1304    //   if ( strcmp(pamdbuser,"") ) user = pamdbuser;
1305    //   if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
1306    //   //
1307    //   //
1308    //   TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
1309    //   if ( !dbc->IsConnected() ) return 1;
1310    //   stringstream myquery;
1311    //   myquery.str("");
1312    //   myquery << "SET time_zone='+0:00'";
1313    //   dbc->Query(myquery.str().c_str());
1314    //   GL_PARAM *glparam = new GL_PARAM();
1315    //   glparam->Query_GL_PARAM(1,1,dbc); // parameters stored in DB in GL_PRAM table
1316    //   trk->LoadField(glparam->PATH+glparam->NAME);
1317    //   //
1318    //   Bool_t defcal = true;
1319    //   Int_t error=glparam->Query_GL_PARAM(run->RUNHEADER_TIME,201,dbc); // parameters stored in DB in GL_PRAM table
1320    //   if ( error<0 ) {
1321    //     return(1);
1322    //   };
1323    //   printf(" Reading ToF parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
1324    //   if ( (UInt_t)glparam->TO_TIME != (UInt_t)4294967295UL ) defcal = false;
1325    //   //
1326    //   Int_t nlen = (Int_t)(glparam->PATH+glparam->NAME).Length();
1327    //   rdtofcal((char *)(glparam->PATH+glparam->NAME).Data(),&nlen);
1328    //   //
1329    //   Int_t adc[4][12];
1330    //   Int_t tdc[4][12];
1331    //   Float_t tdcc[4][12];
1332    //   //
1333    //   // process tof data
1334    //   //
1335    //   for (Int_t hh=0; hh<12;hh++){
1336    //     for (Int_t kk=0; kk<4;kk++){
1337    //            adc[kk][hh] = 4095;
1338    //            tdc[kk][hh] = 4095;
1339    //            tdcc[kk][hh] = 4095.;
1340    //            tofinput_.adc[hh][kk] = 4095;
1341    //            tofinput_.tdc[hh][kk] = 4095;
1342    //     };
1343    //   };
1344    //   Int_t ntrkentry = 0;
1345    //   Int_t npmtentry = 0;
1346    //   Int_t gg = 0;
1347    //   Int_t hh = 0;
1348    //   Int_t adcf[48];
1349    //   memset(adcf, 0, 48*sizeof(Int_t));
1350    //   Int_t tdcf[48];
1351    //   memset(tdcf, 0, 48*sizeof(Int_t));
1352    //   for (Int_t pm=0; pm < this->ntrk() ; pm++){
1353    //      ToFTrkVar *ttf = this->GetToFTrkVar(pm);
1354    //      for ( Int_t nc=0; nc < ttf->npmttdc; nc++){
1355    //             if ( (ttf->tdcflag).At(nc) != 0 ) tdcf[(ttf->pmttdc).At(nc)] = 1;
1356    //      };
1357    //      for ( Int_t nc=0; nc < ttf->npmtadc; nc++){
1358    //             if ( (ttf->adcflag).At(nc) != 0 ) adcf[(ttf->pmtadc).At(nc)] = 1;
1359    //      };
1360    //   };
1361    //   //
1362    //   for (Int_t pm=0; pm < this->npmt() ; pm++){
1363    //      ToFPMT *pmt = this->GetToFPMT(pm);
1364    //      this->GetPMTIndex(pmt->pmt_id, gg, hh);
1365    //      if ( adcf[pmt->pmt_id] == 0 ){
1366    //              tofinput_.adc[gg][hh] = (int)pmt->adc;
1367    //              adc[hh][gg] = (int)pmt->adc;
1368    //      };
1369    //      if ( tdcf[pmt->pmt_id] == 0 ){
1370    //              tofinput_.tdc[gg][hh] = (int)pmt->tdc;
1371    //              tdc[hh][gg] = (int)pmt->tdc;
1372    //      };
1373    //      tdcc[hh][gg] = (float)pmt->tdc_tw;
1374    //      // Int_t pppid = this->GetPMTid(hh,gg);
1375    //      //      printf(" pm %i pmt_id %i pppid %i hh %i gg %i tdcc %f tdc %f adc %f \n",pm,pmt->pmt_id,pppid,hh,gg,pmt->tdc_tw,pmt->tdc,pmt->adc);
1376    //   };
1377    //   //
1378    //   Int_t unpackError = this->unpackError;
1379    //   //
1380    //   for (Int_t hh=0; hh<5;hh++){
1381    //      tofinput_.patterntrig[hh]=trg->patterntrig[hh];
1382    //   };
1383    //   //
1384    //   this->Clear();
1385    //   //
1386    //       Int_t pmt_id = 0;
1387    //       ToFPMT *t_pmt = new ToFPMT();
1388    //       if(!(this->PMT)) this->PMT = new TClonesArray("ToFPMT",12); //ELENA
1389    //       TClonesArray &tpmt = *this->PMT;
1390    //       ToFTrkVar *t_tof = new ToFTrkVar();
1391    //       if(!(this->ToFTrk)) this->ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA
1392    //       TClonesArray &t = *this->ToFTrk;
1393    //       //
1394    //       //
1395    //       // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related  variables.
1396    //       //
1397    //       npmtentry = 0;
1398    //       //
1399    //       ntrkentry = 0;
1400    //       //
1401    //       // Calculate tracks informations from ToF alone
1402    //       //
1403    //       tofl2com();
1404    //       //
1405    //       memcpy(this->tof_j_flag,tofoutput_.tof_j_flag,6*sizeof(Int_t));
1406    //       //
1407    //       t_tof->trkseqno = -1;
1408    //       //
1409    //       // and now we must copy from the output structure to the level2 class:
1410    //       //
1411    //       t_tof->npmttdc = 0;
1412    //       //
1413    //       for (Int_t hh=0; hh<12;hh++){
1414    //         for (Int_t kk=0; kk<4;kk++){
1415    //           if ( tofoutput_.tofmask[hh][kk] != 0 ){
1416    //             pmt_id = this->GetPMTid(kk,hh);
1417    //             t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
1418    //             t_tof->tdcflag.AddAt(tofoutput_.tdcflagtof[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
1419    //             t_tof->npmttdc++;
1420    //           };
1421    //         };
1422    //       };
1423    //       for (Int_t kk=0; kk<13;kk++){
1424    //         t_tof->beta[kk] = tofoutput_.betatof_a[kk];
1425    //       }
1426    //       //
1427    //       t_tof->npmtadc = 0;
1428    //       for (Int_t hh=0; hh<12;hh++){
1429    //         for (Int_t kk=0; kk<4;kk++){
1430    //           if ( tofoutput_.adctof_c[hh][kk] < 1000 ){
1431    //             t_tof->dedx.AddAt(tofoutput_.adctof_c[hh][kk],t_tof->npmtadc);
1432    //             pmt_id = this->GetPMTid(kk,hh);
1433    //             t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
1434    //             t_tof->adcflag.AddAt(tofoutput_.adcflagtof[hh][kk],t_tof->npmtadc); // gf: Jan 09/07
1435    //             t_tof->npmtadc++;
1436    //           };
1437    //         };
1438    //       };
1439    //       //
1440    //       memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
1441    //       memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
1442    //       memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
1443    //       memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
1444    //       //
1445    //       new(t[ntrkentry]) ToFTrkVar(*t_tof);
1446    //       ntrkentry++;
1447    //       t_tof->Clear();
1448    //       //
1449    //       //
1450    //       //
1451    //       t_pmt->Clear();
1452    //       //
1453    //       for (Int_t hh=0; hh<12;hh++){
1454    //         for (Int_t kk=0; kk<4;kk++){
1455    //          // new WM
1456    //           if ( tofoutput_.tdc_c[hh][kk] < 4095 || adc[kk][hh] < 4095  || tdc[kk][hh] < 4095 ){
1457    // //          if ( tdcc[kk][hh] < 4095. || adc[kk][hh] < 4095  || tdc[kk][hh] < 4095 ){
1458    //             //
1459    //             t_pmt->pmt_id = this->GetPMTid(kk,hh);
1460    //             t_pmt->tdc_tw = tofoutput_.tdc_c[hh][kk];
1461    //             t_pmt->adc = (Float_t)adc[kk][hh];
1462    //             t_pmt->tdc = (Float_t)tdc[kk][hh];
1463    //             //
1464    //             new(tpmt[npmtentry]) ToFPMT(*t_pmt);
1465    //             npmtentry++;
1466    //             t_pmt->Clear();
1467    //           };
1468    //         };
1469    //       };
1470    //       //
1471    //       // Calculate track-related variables
1472    //       //
1473    //       if ( trk->ntrk() > 0 ){
1474    //         //
1475    //         // We have at least one track
1476    //         //
1477    //         //
1478    //         // Run over tracks
1479    //         //
1480    //         for(Int_t nt=0; nt < trk->ntrk(); nt++){
1481    //           //
1482    //           TrkTrack *ptt = trk->GetStoredTrack(nt);
1483    //           //
1484    //           // Copy the alpha vector in the input structure
1485    //           //
1486    //           for (Int_t e = 0; e < 5 ; e++){
1487    //             tofinput_.al_pp[e] = ptt->al[e];
1488    //           };
1489    //           //
1490    //           // Get tracker related variables for this track
1491    //           //
1492    //           toftrk();
1493    //           //
1494    //           // Copy values in the class from the structure (we need to use a temporary class to store variables).
1495    //           //
1496    //           t_tof->npmttdc = 0;
1497    //           for (Int_t hh=0; hh<12;hh++){
1498    //             for (Int_t kk=0; kk<4;kk++){
1499    //               if ( tofoutput_.tofmask[hh][kk] != 0 ){
1500    //                 pmt_id = this->GetPMTid(kk,hh);
1501    //                 t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
1502    //                 t_tof->tdcflag.AddAt(tofoutput_.tdcflag[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
1503    //                 t_tof->npmttdc++;
1504    //               };
1505    //             };
1506    //           };
1507    //           for (Int_t kk=0; kk<13;kk++){
1508    //             t_tof->beta[kk] = tofoutput_.beta_a[kk];
1509    //           };
1510    //           //
1511    //           t_tof->npmtadc = 0;
1512    //           for (Int_t hh=0; hh<12;hh++){
1513    //             for (Int_t kk=0; kk<4;kk++){
1514    //               if ( tofoutput_.adc_c[hh][kk] < 1000 ){
1515    //                 t_tof->dedx.AddAt(tofoutput_.adc_c[hh][kk],t_tof->npmtadc);
1516    //                 pmt_id = this->GetPMTid(kk,hh);
1517    //                 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
1518    //                 t_tof->adcflag.AddAt(tofoutput_.adcflag[hh][kk],t_tof->npmtadc); // gf: Jan 09/07
1519    //                 t_tof->npmtadc++;
1520    //               };
1521    //             };
1522    //           };
1523    //           //
1524    //           memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
1525    //           memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
1526    //           memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
1527    //           memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
1528    //           //
1529    //           // Store the tracker track number in order to be sure to have shyncronized data during analysis
1530    //           //
1531    //           t_tof->trkseqno = nt;
1532    //           //
1533    //           // create a new object for this event with track-related variables
1534    //           //
1535    //           new(t[ntrkentry]) ToFTrkVar(*t_tof);
1536    //           ntrkentry++;
1537    //           t_tof->Clear();
1538    //           //
1539    //         }; // loop on all the tracks
1540    //       //
1541    //       this->unpackError = unpackError;
1542    //       if ( defcal ){
1543    //         this->default_calib = 1;
1544    //       } else {
1545    //         this->default_calib = 0;
1546    //       };
1547    //};
1548    //  return(0);
1549    }
1550    
1551    bool ToFLevel2::bit(int decimal, char pos){
1552      return( (decimal>>pos)%2 );
1553    }
1554    
1555    bool ToFLevel2::checkPMT(TString givenpmt){
1556      TClonesArray* Pmt = this->PMT;
1557      //  printf(" ou %s entries %i \n",givenpmt.Data(),Pmt->GetEntries());
1558      for(int i=0; i<Pmt->GetEntries(); i++) {  
1559        ToFPMT* pmthit = (ToFPMT*)Pmt->At(i);
1560        TString pmtname = this->GetPMTName(pmthit->pmt_id);
1561        //    printf(" name %s \n",pmtname.Data());
1562        if ( !strcmp(pmtname.Data(),givenpmt.Data()) )
1563          return true;
1564      }
1565      //  printf(" PMT %s missing \n",givenpmt.Data());
1566      return false;
1567    }
1568    
1569    bool ToFLevel2::checkPMTpatternPMThit(TrigLevel2 *trg, int &pmtpattern, int &pmtnosignal){
1570      UInt_t *patterntrig = trg->patterntrig;
1571      pmtpattern = 0;
1572      pmtnosignal = 0;
1573      bool good = true;
1574      //S3
1575      if ( this->bit(patterntrig[2],0) ){ pmtpattern++;  if ( !this->checkPMT("S31_1A")){ pmtnosignal++; good = false;}}
1576      if ( this->bit(patterntrig[2],1) ){ pmtpattern++;  if ( !this->checkPMT("S31_2A")){ pmtnosignal++; good = false;}}
1577      if ( this->bit(patterntrig[2],2) ){ pmtpattern++;  if ( !this->checkPMT("S31_3A")){ pmtnosignal++; good = false;}}
1578      if ( this->bit(patterntrig[2],3) ){ pmtpattern++;  if ( !this->checkPMT("S31_1B")){ pmtnosignal++; good = false;}}
1579      if ( this->bit(patterntrig[2],4) ){ pmtpattern++;  if ( !this->checkPMT("S31_2B")){ pmtnosignal++; good = false;}}
1580      if ( this->bit(patterntrig[2],5) ){ pmtpattern++;  if ( !this->checkPMT("S31_3B")){ pmtnosignal++; good = false;}}      
1581      if ( this->bit(patterntrig[2],6) ){ pmtpattern++;  if ( !this->checkPMT("S32_1A")){ pmtnosignal++; good = false;}}
1582      if ( this->bit(patterntrig[2],7) ){ pmtpattern++;  if ( !this->checkPMT("S32_2A")){ pmtnosignal++; good = false;}}
1583      if ( this->bit(patterntrig[2],8) ){ pmtpattern++;  if ( !this->checkPMT("S32_3A")){ pmtnosignal++; good = false;}}
1584      if ( this->bit(patterntrig[2],9) ){ pmtpattern++;  if ( !this->checkPMT("S32_1B")){ pmtnosignal++; good = false;}}
1585      if ( this->bit(patterntrig[2],10) ){ pmtpattern++;  if ( !this->checkPMT("S32_2B")){ pmtnosignal++; good = false;}}
1586      if ( this->bit(patterntrig[2],11) ){ pmtpattern++;  if ( !this->checkPMT("S32_3B")){ pmtnosignal++; good = false;}}      
1587      //S2
1588      if ( this->bit(patterntrig[3],0) ){ pmtpattern++;  if ( !this->checkPMT("S21_1A")){ pmtnosignal++; good = false;}}
1589      if ( this->bit(patterntrig[3],1) ){ pmtpattern++;  if ( !this->checkPMT("S21_2A")){ pmtnosignal++; good = false;}}
1590      if ( this->bit(patterntrig[3],2) ){ pmtpattern++;  if ( !this->checkPMT("S21_1B")){ pmtnosignal++; good = false;}}
1591      if ( this->bit(patterntrig[3],3) ){ pmtpattern++;  if ( !this->checkPMT("S21_2B")){ pmtnosignal++; good = false;}}      
1592      if ( this->bit(patterntrig[3],4) ){ pmtpattern++;  if ( !this->checkPMT("S22_1A")){ pmtnosignal++; good = false;}}
1593      if ( this->bit(patterntrig[3],5) ){ pmtpattern++;  if ( !this->checkPMT("S22_2A")){ pmtnosignal++; good = false;}}
1594      if ( this->bit(patterntrig[3],6) ){ pmtpattern++;  if ( !this->checkPMT("S22_1B")){ pmtnosignal++; good = false;}}
1595      if ( this->bit(patterntrig[3],7) ){ pmtpattern++;  if ( !this->checkPMT("S22_2B")){ pmtnosignal++; good = false;}}      
1596      //S12
1597      if ( this->bit(patterntrig[4],0) ){ pmtpattern++;  if ( !this->checkPMT("S12_1A")){ pmtnosignal++; good = false;}}
1598      if ( this->bit(patterntrig[4],1) ){ pmtpattern++;  if ( !this->checkPMT("S12_2A")){ pmtnosignal++; good = false;}}
1599      if ( this->bit(patterntrig[4],2) ){ pmtpattern++;  if ( !this->checkPMT("S12_3A")){ pmtnosignal++; good = false;}}
1600      if ( this->bit(patterntrig[4],3) ){ pmtpattern++;  if ( !this->checkPMT("S12_4A")){ pmtnosignal++; good = false;}}
1601      if ( this->bit(patterntrig[4],4) ){ pmtpattern++;  if ( !this->checkPMT("S12_5A")){ pmtnosignal++; good = false;}}
1602      if ( this->bit(patterntrig[4],5) ){ pmtpattern++;  if ( !this->checkPMT("S12_6A")){ pmtnosignal++; good = false;}}      
1603      if ( this->bit(patterntrig[4],6) ){ pmtpattern++;  if ( !this->checkPMT("S12_1A")){ pmtnosignal++; good = false;}}
1604      if ( this->bit(patterntrig[4],7) ){ pmtpattern++;  if ( !this->checkPMT("S12_2A")){ pmtnosignal++; good = false;}}
1605      if ( this->bit(patterntrig[4],8) ){ pmtpattern++;  if ( !this->checkPMT("S12_3A")){ pmtnosignal++; good = false;}}
1606      if ( this->bit(patterntrig[4],9) ){ pmtpattern++;  if ( !this->checkPMT("S12_4B")){ pmtnosignal++; good = false;}}
1607      if ( this->bit(patterntrig[4],10) ){ pmtpattern++; if ( !this->checkPMT("S12_5B")){ pmtnosignal++; good = false;}}
1608      if ( this->bit(patterntrig[4],11) ){ pmtpattern++; if ( !this->checkPMT("S12_6B")){ pmtnosignal++; good = false;}}      
1609      //S11
1610      if ( this->bit(patterntrig[5],0) ){ pmtpattern++;  if ( !this->checkPMT("S11_1A")){ pmtnosignal++; good = false;}}
1611      if ( this->bit(patterntrig[5],1) ){ pmtpattern++;  if ( !this->checkPMT("S11_2A")){ pmtnosignal++; good = false;}}
1612      if ( this->bit(patterntrig[5],2) ){ pmtpattern++;  if ( !this->checkPMT("S11_3A")){ pmtnosignal++; good = false;}}
1613      if ( this->bit(patterntrig[5],3) ){ pmtpattern++;  if ( !this->checkPMT("S11_4A")){ pmtnosignal++; good = false;}}
1614      if ( this->bit(patterntrig[5],4) ){ pmtpattern++;  if ( !this->checkPMT("S11_5A")){ pmtnosignal++; good = false;}}
1615      if ( this->bit(patterntrig[5],5) ){ pmtpattern++;  if ( !this->checkPMT("S11_6A")){ pmtnosignal++; good = false;}}
1616      if ( this->bit(patterntrig[5],6) ){ pmtpattern++;  if ( !this->checkPMT("S11_7A")){ pmtnosignal++; good = false;}}
1617      if ( this->bit(patterntrig[5],7) ){ pmtpattern++;  if ( !this->checkPMT("S11_8A")){ pmtnosignal++; good = false;}}      
1618      if ( this->bit(patterntrig[5],8) ){ pmtpattern++;  if ( !this->checkPMT("S11_1B")){ pmtnosignal++; good = false;}}
1619      if ( this->bit(patterntrig[5],9) ){ pmtpattern++;  if ( !this->checkPMT("S11_2B")){ pmtnosignal++; good = false;}}
1620      if ( this->bit(patterntrig[5],10) ){ pmtpattern++; if ( !this->checkPMT("S11_3B")){ pmtnosignal++; good = false;}}
1621      if ( this->bit(patterntrig[5],11) ){ pmtpattern++; if ( !this->checkPMT("S11_4B")){ pmtnosignal++; good = false;}}
1622      if ( this->bit(patterntrig[5],12) ){ pmtpattern++; if ( !this->checkPMT("S11_5B")){ pmtnosignal++; good = false;}}
1623      if ( this->bit(patterntrig[5],13) ){ pmtpattern++; if ( !this->checkPMT("S11_6B")){ pmtnosignal++; good = false;}}
1624      if ( this->bit(patterntrig[5],14) ){ pmtpattern++; if ( !this->checkPMT("S11_7B")){ pmtnosignal++; good = false;}}
1625      if ( this->bit(patterntrig[5],15) ){ pmtpattern++; if ( !this->checkPMT("S11_8B")){ pmtnosignal++; good = false;}}
1626    
1627      return good;
1628    }
1629    
1630    bool ToFLevel2::checkPMTpmttrig(TrigLevel2 *trg){
1631      //  UInt_t *patterntrig = trg->patterntrig;
1632      int rS11 = 0;
1633      int rS12 = 0;
1634      int rS21 = 0;
1635      int rS22 = 0;
1636      int rS31 = 0;
1637      int rS32 = 0;
1638    
1639      // trigger configuration for the event from saved pmts
1640      TClonesArray* Pmt = this->PMT;
1641      for(int i=0; i<Pmt->GetEntries(); i++) {  
1642        ToFPMT* pmthit = (ToFPMT*)Pmt->At(i);
1643        TString pmtname = this->GetPMTName(pmthit->pmt_id);
1644        if ( pmtname.Contains("S11") ) rS11++;
1645        if ( pmtname.Contains("S12") ) rS12++;
1646        if ( pmtname.Contains("S21") ) rS21++;
1647        if ( pmtname.Contains("S22") ) rS22++;
1648        if ( pmtname.Contains("S31") ) rS31++;
1649        if ( pmtname.Contains("S32") ) rS32++;
1650      }
1651      int rTOF1 = (rS11 + rS12) * (rS21 + rS22) * (rS31 + rS32);
1652      int rTOF2 = (rS11 * rS12) * (rS21 * rS22) * (rS31 * rS32);
1653    
1654      int rTOF3 = (rS21 + rS22) * (rS31 + rS32);
1655      int rTOF4 = (rS21 * rS22) * (rS31 * rS32);
1656    
1657      int rTOF5 = rS12 * (rS21 * rS22);
1658    
1659      int rTOF6 = (rS11 + rS12) * (rS31 + rS32);
1660      int rTOF7 = (rS11 * rS12) * (rS31 * rS32);
1661    
1662    
1663      // trigger configuration of the run
1664      bool TCTOF1 = false;
1665      bool TCTOF2 = false;
1666      bool TCTOF3 = false;
1667      bool TCTOF4 = false;
1668      bool TCTOF5 = false;
1669      bool TCTOF6 = false;
1670      bool TCTOF7 = false;
1671      if ( trg->trigconf & (1<<0) ) TCTOF1 = true;
1672      if ( trg->trigconf & (1<<1) ) TCTOF2 = true;
1673      if ( trg->trigconf & (1<<2) ) TCTOF3 = true;
1674      if ( trg->trigconf & (1<<3) ) TCTOF4 = true;
1675      if ( trg->trigconf & (1<<4) ) TCTOF5 = true;
1676      if ( trg->trigconf & (1<<5) ) TCTOF6 = true;
1677      if ( trg->trigconf & (1<<6) ) TCTOF7 = true;
1678    
1679      // do patterntrig pmts match the trigger configuration?
1680      bool pmtsconf_trigconf_match = true;
1681      if ( rTOF1 == 0 && TCTOF1 ) pmtsconf_trigconf_match = false;
1682      if ( rTOF2 == 0 && TCTOF2 ) pmtsconf_trigconf_match = false;
1683      if ( rTOF3 == 0 && TCTOF3 ) pmtsconf_trigconf_match = false;
1684      if ( rTOF4 == 0 && TCTOF4 ) pmtsconf_trigconf_match = false;
1685      if ( rTOF5 == 0 && TCTOF5 ) pmtsconf_trigconf_match = false;
1686      if ( rTOF6 == 0 && TCTOF6 ) pmtsconf_trigconf_match = false;
1687      if ( rTOF7 == 0 && TCTOF7 ) pmtsconf_trigconf_match = false;
1688    
1689      return pmtsconf_trigconf_match;
1690    }
1691    
1692    void ToFLevel2::printPMT(){
1693      TClonesArray* Pmt = this->PMT;
1694      for(int i=0; i<Pmt->GetEntries(); i++) {  
1695        ToFPMT* pmthit = (ToFPMT*)Pmt->At(i);
1696        TString pmtname = this->GetPMTName(pmthit->pmt_id);
1697        printf(" PMT hit: %s \n",pmtname.Data());
1698      }
1699    }
1700    
1701    
1702    ToFdEdx::ToFdEdx()
1703    {
1704      memset(conn,0,12*sizeof(Bool_t));
1705      memset(ts,0,12*sizeof(UInt_t));
1706      memset(te,0,12*sizeof(UInt_t));
1707      eDEDXpmt = new TArrayF(48);
1708      Define_PMTsat();
1709      Clear();
1710    }
1711    
1712    ToFdEdx::~ToFdEdx(){
1713      Clear();
1714      Delete();
1715    }
1716    
1717    void ToFdEdx::Delete(Option_t *option){
1718      if ( eDEDXpmt ){
1719        eDEDXpmt->Set(0);
1720        if ( eDEDXpmt) delete eDEDXpmt;
1721      }
1722    }
1723    
1724    //------------------------------------------------------------------------
1725    void ToFdEdx::CheckConnectors(UInt_t atime, GL_PARAM *glparam, TSQLServer *dbc)
1726    {
1727      for(int i=0; i<12; i++){
1728        if(atime<=ts[i] || atime>te[i]){
1729          Int_t error=glparam->Query_GL_PARAM(atime,210+i,dbc); // parameters stored in DB in GL_PRAM table
1730          if ( error<0 ) {
1731            conn[i]=false;
1732            ts[i]=0;
1733            te[i]=numeric_limits<UInt_t>::max();
1734          };
1735          if ( !error ){
1736            conn[i]=true;
1737            ts[i]=glparam->FROM_TIME;
1738            te[i]=glparam->TO_TIME;
1739          }
1740          if ( error>0 ){
1741            conn[i]=false;
1742            ts[i]=glparam->TO_TIME;
1743            TSQLResult *pResult;
1744            TSQLRow *row;
1745            TString query= Form("SELECT FROM_TIME FROM GL_PARAM WHERE TYPE=%i AND FROM_TIME>=%i ORDER BY FROM_TIME ASC LIMIT 1;",210+i,atime);
1746            pResult=dbc->Query(query.Data());
1747            if(!pResult->GetRowCount()){
1748              te[i]=numeric_limits<UInt_t>::max();
1749            }else{
1750              row=pResult->Next();
1751              te[i]=(UInt_t)atoll(row->GetField(0));
1752            }
1753          }
1754          //
1755          
1756      }      }
1757    }    }
1758    
1759    }
1760    //------------------------------------------------------------------------
1761    void ToFdEdx::Clear(Option_t *option)
1762    {
1763      //
1764      // Set arrays and initialize structure
1765      //  eDEDXpmt.Set(48);    eDEDXpmt.Reset(-1);   // Set array size  and reset structure
1766      eDEDXpmt->Set(48);    eDEDXpmt->Reset(-1);   // Set array size  and reset structure
1767      //
1768    };
1769    
1770    //------------------------------------------------------------------------
1771    void ToFdEdx::Print(Option_t *option)
1772    {
1773      //
1774      printf("========================================================================\n");
1775    
1776    };
1777    
1778    //------------------------------------------------------------------------
1779    void ToFdEdx::Init(pamela::tof::TofEvent *tofl0)
1780    {
1781      //
1782      ToFLevel2 tf;
1783      for (Int_t gg=0; gg<4;gg++){
1784        for (Int_t hh=0; hh<12;hh++){
1785          //          tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh];          
1786          int mm = tf.GetPMTid(gg,hh);        
1787          adc[mm]=tofl0->adc[gg][hh];
1788        };      
1789      };
1790      
1791    };
1792    
1793    //------------------------------------------------------------------------
1794    void ToFdEdx::Init(Int_t gg, Int_t hh, Float_t adce)
1795    {
1796      //
1797      ToFLevel2 tf;
1798      //  for (Int_t gg=0; gg<4;gg++){
1799      //    for (Int_t hh=0; hh<12;hh++){
1800      int mm = tf.GetPMTid(gg,hh);    
1801      adc[mm]=adce;
1802      
1803    };
1804    //------------------------------------------------------------------------
1805    void ToFdEdx::Process(UInt_t atime, Float_t betamean, Float_t *xtr_tof, Float_t *ytr_tof, Int_t exitat)
1806    {
1807      bool debug = false;
1808      if ( debug ) printf(" INSIDE TOFDEDX PROCESS \n");
1809      // the parameters should be already initialised by InitPar()
1810      //  printf(" in process \n");
1811      Clear();
1812    
1813     // define angle:  
1814      double dx   = xtr_tof[1] - xtr_tof[5];
1815      double dy   = ytr_tof[0] - ytr_tof[4];
1816      double dr   = sqrt(dx*dx+dy*dy);
1817      double theta=atan(dr/76.81);
1818      //
1819      if ( xtr_tof[1] > 99. ||  xtr_tof[5] > 99. || ytr_tof[0] > 99. ||  ytr_tof[4] > 99. ) theta = 0.;
1820      for (Int_t ii=0; ii<6; ii++){
1821        if ( xtr_tof[ii] > 99. ) xtr_tof[ii] = 0.;
1822        if ( ytr_tof[ii] > 99. ) ytr_tof[ii] = 0.;
1823      };
1824      //
1825      if ( debug ) printf(" theta %f \n",theta);
1826      if ( debug ) printf(" xtr_tof %.1f %.1f %.1f %.1f %.1f %.1f \n",xtr_tof[0],xtr_tof[1],xtr_tof[2],xtr_tof[3],xtr_tof[4],xtr_tof[5]);
1827      if ( debug ) printf(" ytr_tof %.1f %.1f %.1f %.1f %.1f %.1f \n",ytr_tof[0],ytr_tof[1],ytr_tof[2],ytr_tof[3],ytr_tof[4],ytr_tof[5]);
1828      //--------------------- TABLE OF PERIODS WITH HV PROBLEMS ----------------------------
1829      
1830      int Aconn=conn[0];    // PMT 0,20,22,24
1831      int Bconn=conn[1];    // PMT 6,12,26,34
1832      int Cconn=conn[2];    // PMT 4,14,28,32
1833      int Dconn=conn[3];    // PMT 2,8,10,30
1834      int Econn=conn[4];    // PMT 42,43,44,47
1835      int Fconn=conn[5];    // PMT 7,19,23,27
1836      int Gconn=conn[6];    // PMT 3,11,25,33
1837      int Hconn=conn[7];    // PMT 1,9,13,21
1838      int Iconn=conn[8];    // PMT 5,29,31,35
1839      int Lconn=conn[9];    // PMT 37,40,45,46
1840      int Mconn=conn[10];    // PMT 15,16,17,18
1841      int Nconn=conn[11];    // PMT 36,38,39,41
1842      if( false ) cout << Gconn << Iconn << Lconn <<endl; // to avoid compilation warnings
1843        
1844      //  printf(" size %i \n",eDEDXpmt.GetSize());
1845      for( int ii=0; ii<48; ii++ ) {
1846        //
1847        //    eDEDXpmt.SetAt(-1.,ii);
1848        //    printf(" ii %i beta %f atime %u xtr 1 %f ytr 1 %f adc %f \n",ii,betamean,atime,xtr_tof[0],ytr_tof[0],adc[ii]);
1849        if ( debug ) printf("II %i adc %f \n",ii,adc[ii]);
1850    
1851        if( adc[ii] >= 4095. ){
1852          //      eDEDXpmt[ii] = 0.;
1853          eDEDXpmt->AddAt(0.,ii);
1854          if ( debug ) printf(" %i adc>4095 \n",ii);
1855          continue; // EMILIANO
1856        };
1857    
1858        if( adc[ii] >= (PMTsat[ii]-5.) && adc[ii] < 4095. ){
1859          eDEDXpmt->AddAt(1000.,ii);
1860          if ( debug ) printf(" %i adc> pmtsat && adc<4095 \n",ii);
1861          continue; // EMILIANO
1862        };
1863    
1864        if( adc[ii] <= 0. ) {
1865          eDEDXpmt->AddAt(1500.,ii);
1866          if ( debug ) printf(" %i adc<=0 \n",ii);
1867          continue;
1868        };
1869        //
1870        double adcpC   = f_adcPC( adc[ii] );    // - adc conversion in pC
1871        if ( exitat == 0 ){
1872          eDEDXpmt->AddAt((Float_t)adcpC,ii);
1873          continue;
1874        }
1875        //    printf(" e qua? \n");
1876    
1877        double adccorr = adcpC*fabs(cos(theta));    
1878        if ( debug ) printf(" adccorr %f \n",adccorr);
1879        if(adccorr<=0.){
1880          if ( debug ) printf(" %i adccorr<=0 \n",ii);
1881          //      eDEDXpmt->AddAt((Float_t)adcpC,ii);//?
1882          continue;
1883        }
1884        if ( exitat == 1 ){
1885          eDEDXpmt->AddAt((Float_t)adccorr,ii);
1886          continue;
1887        }
1888        //    printf(" e quo? \n");
1889    
1890        //    int standard=0;
1891        int S115B_ok=0;
1892        int S115B_break=0;
1893    
1894        if(atime<1158720000)S115B_ok=1;
1895        else S115B_break=1;
1896    
1897    
1898        //------------------------------------------------------------------------
1899        //    printf(" e qui? \n");
1900        //---------------------------------------------------- Z reconstruction
1901    
1902        double adcHe, adcnorm, adclin, dEdx, Zeta;
1903    
1904        adcHe=-2;
1905        adcnorm=-2;
1906        adclin=-2;
1907        dEdx=-2;
1908        Zeta=-2;
1909        Double_t correction = 1.;
1910    
1911        if(Aconn==1 && (ii==0 || ii==20 || ii==22 || ii==24)){
1912          correction = 1.675;
1913        }
1914        else if(Bconn==1 && (ii==6 || ii==12 || ii==26 || ii==34)){
1915          correction = 2.482;
1916        }
1917        else if(Cconn==1 && (ii==4 || ii==14 || ii==28 || ii==32)){
1918          correction = 1.464;
1919        }
1920        else if(Dconn==1 && (ii==2 || ii==8 || ii==10 || ii==30)){
1921          correction = 1.995;
1922        }
1923        else if(Econn==1 && (ii==42 || ii==43 || ii==44 || ii==47)){
1924          correction = 1.273;
1925        }
1926        else if(Fconn==1 && (ii==7 || ii==19 || ii==23 || ii==27)){
1927          correction = 1.565;
1928        }
1929        else if(Mconn==1 && (ii==15 || ii==16 || ii==17 || ii==18)){
1930          correction = 1.565;
1931        }
1932        else if(Nconn==1 && (ii==36 || ii==38 || ii==39 || ii==41)){
1933          correction = 1.018;
1934        }
1935        else if(Hconn==1 && (ii==1 || ii==13 || ii==21 || (ii==9&&S115B_ok==1))){
1936          correction = 1.84;
1937        }
1938        else if(S115B_break==1 && ii==9 && Hconn==1){
1939          correction = 1.64;
1940        }
1941        else correction = 1.;
1942        
1943        if( ii==9 && S115B_break==1 ){
1944          adcHe   = f_att5B( ytr_tof[0] )/correction;
1945        } else {
1946          adcHe   = Get_adc_he(ii, xtr_tof, ytr_tof)/correction;
1947        };
1948        if(adcHe<=0){
1949          if ( debug ) printf(" %i adcHe<=0 \n",ii);
1950          //      eDEDXpmt->AddAt((Float_t)adccorr,ii); //?
1951          continue;
1952        }
1953        if ( exitat == 2 ){
1954          if(ii==9 && S115B_break==1)  eDEDXpmt->AddAt(36.*(Float_t)adccorr/adcHe,ii);
1955          else  adclin  = 4.*(Float_t)adccorr/adcHe;
1956          continue;
1957        }
1958    
1959        if(ii==9 && S115B_break==1)  adcnorm = f_pos5B(adccorr);
1960        else adcnorm = f_pos( (parPos[ii]), adccorr);
1961        if(adcnorm<=0){
1962          if ( debug ) printf(" %i adcnorm<=0 \n",ii);
1963          //      eDEDXpmt->AddAt((Float_t)adccorr,ii);//?
1964          continue;
1965        }
1966        if ( debug ) printf(" adcnorm %f \n",adcnorm);
1967    
1968        if(ii==9 && S115B_break==1)  adclin  = 36.*adcnorm/adcHe;
1969        else  adclin  = 4.*adcnorm/adcHe;
1970        if ( debug ) printf(" adclin %f \n",adclin);
1971        if(adclin<=0){
1972          if ( debug ) printf(" %i adclin<=0 \n",ii);
1973          //      eDEDXpmt->AddAt((Float_t)adccorr,ii);//?
1974          continue;
1975        }
1976        if ( exitat == 3 ){
1977          if(ii==9 && S115B_break==1)  eDEDXpmt->AddAt((Float_t)adclin,ii);
1978          else  eDEDXpmt->AddAt((Float_t)adclin,ii);
1979          continue;
1980        }
1981        //
1982        if ( betamean > 99. ){
1983          //      eDEDXpmt.AddAt((Float_t)adclin,ii);
1984          eDEDXpmt->AddAt((Float_t)adclin,ii);
1985          //      printf(" AAPMT IS %i dedx is %f vector is %f \n",ii,adclin,eDEDXpmt[ii]);
1986          if ( debug ) printf(" %i betamean > 99 \n",ii);
1987          continue;
1988        };
1989        //
1990        double dEdxHe=-2;
1991        if(ii==9 && S115B_break==1){
1992          if( betamean <1. ) dEdxHe = f_BB5B( betamean );
1993          else                       dEdxHe = 33;
1994        } else {
1995          if( betamean <1. ) dEdxHe = f_BB( (parBBneg[ii]), betamean );
1996          else                       dEdxHe = parBBpos[ii];
1997        }
1998            
1999    l2->npmt = PMT->GetEntries();      if ( debug ) printf(" dEdxHe %f \n",dEdxHe);
2000     for(Int_t j=0;j<l2->npmt;j++){      
2001       l2->pmt_id[j] = ((ToFPMT*)PMT->At(j))->pmt_id;      if(dEdxHe<=0){
2002       l2->adc[j] =((ToFPMT*)PMT->At(j))->adc;        eDEDXpmt->AddAt((Float_t)adclin,ii);
2003       l2->tdc_tw[j] =((ToFPMT*)PMT->At(j))->tdc_tw;        if ( debug ) printf(" %i dEdxHe<=0 \n",ii);
2004     }        continue;
2005        };
2006    
2007        if(ii==9 && S115B_break==1)  dEdx = f_desatBB5B( adclin );
2008        else  dEdx = f_desatBB((parDesatBB[ii]), adclin );
2009    
2010        if(dEdx<=0){
2011          eDEDXpmt->AddAt((Float_t)adclin,ii);
2012          if ( debug ) printf(" %i dEdx<=0 \n",ii);
2013          continue;
2014        };
2015    
2016        if ( debug ) printf(" dEdx %f \n",dEdx);
2017        eDEDXpmt->AddAt((Float_t)dEdx,ii);
2018        //    eDEDXpmt.AddAt((Float_t)dEdx,ii);
2019    
2020        //    printf(" PMT IS %i dedx is %f vector is %f \n",ii,dEdx,eDEDXpmt[ii]);
2021    
2022      }  //end loop on 48 PMT
2023    
2024    };
2025    
2026    
2027    //------------------------------------------------------------------------
2028    void ToFdEdx::Define_PMTsat()
2029    {
2030      Float_t  sat[48] = {
2031        3176.35,3178.19,3167.38,3099.73,3117.00,3126.29,3111.44,3092.27,
2032        3146.48,3094.41,3132.13,3115.37,3099.32,3110.97,3111.80,3143.14,
2033        3106.72,3153.44,3136.00,3188.96,3104.73,3140.45,3073.18,3106.62,
2034        3112.48,3146.92,3127.24,3136.52,3109.59,3112.89,3045.15,3147.26,
2035        3095.92,3121.05,3083.25,3123.62,3150.92,3125.30,3067.60,3160.18,
2036        3119.36,3108.92,3164.77,3133.64,3111.47,3131.98,3128.87,3135.56 };
2037      PMTsat.Set(48,sat);
2038    }
2039    
2040    //------------------------------------------------------------------------
2041    void ToFdEdx::ReadParBBpos( const char *fname )
2042    {
2043      //  printf("read %s\n",fname);
2044      parBBpos.Set(48);
2045      FILE *fattin = fopen( fname , "r" );
2046      for (int i=0; i<48; i++) {
2047        int   tid=0;
2048        float  tp;
2049        if(fscanf(fattin,"%d %f",
2050                  &tid, &tp )!=2) break;
2051        parBBpos[i]=tp;
2052      }
2053      fclose(fattin);
2054    }
2055    
2056    //------------------------------------------------------------------------
2057    void ToFdEdx::ReadParDesatBB( const char *fname )
2058    {
2059      //  printf("read %s\n",fname);
2060      FILE *fattin = fopen( fname , "r" );
2061      for (int i=0; i<48; i++) {
2062        int   tid=0;
2063        float  tp[3];
2064        if(fscanf(fattin,"%d %f %f %f",
2065                  &tid, &tp[0], &tp[1], &tp[2] )!=4) break;
2066        parDesatBB[i].Set(3,tp);
2067      }
2068      fclose(fattin);
2069    }
2070    
2071    
2072    //------------------------------------------------------------------------
2073    void ToFdEdx::ReadParBBneg( const char *fname )
2074    
2075    {
2076      //  printf("read %s\n",fname);
2077      FILE *fattin = fopen( fname , "r" );
2078      for (int i=0; i<48; i++) {
2079        int   tid=0;
2080        float  tp[3];
2081        if(fscanf(fattin,"%d %f %f %f",
2082                  &tid, &tp[0], &tp[1], &tp[2] )!=4) break;
2083        parBBneg[i].Set(3,tp);
2084      }
2085      fclose(fattin);
2086    }
2087    
2088    //------------------------------------------------------------------------
2089    void ToFdEdx::ReadParPos( const char *fname )
2090    {
2091      //  printf("read %s\n",fname);
2092      FILE *fattin = fopen( fname , "r" );
2093      for (int i=0; i<48; i++) {
2094        int   tid=0;
2095        float  tp[4];
2096        if(fscanf(fattin,"%d %f %f %f %f",
2097                  &tid, &tp[0], &tp[1], &tp[2], &tp[3])!=5) break;
2098        parPos[i].Set(4,tp);
2099      }
2100      fclose(fattin);
2101    }
2102    
2103    //------------------------------------------------------------------------
2104    void ToFdEdx::ReadParAtt( const char *fname )
2105    {
2106      //  printf("read %s\n",fname);
2107      FILE *fattin = fopen( fname , "r" );
2108      for (int i=0; i<48; i++) {
2109        int   tid=0;
2110        float  tp[6];
2111        if(fscanf(fattin,"%d %f %f %f %f %f %f",
2112                  &tid, &tp[0], &tp[1], &tp[2], &tp[3], &tp[4], &tp[5] )!=7) break;
2113        parAtt[i].Set(6,tp);
2114      }
2115      fclose(fattin);
2116  }  }
2117    
2118    
2119    
2120    
2121    
2122    
2123    double ToFdEdx::f_att( TArrayF &p, float x )
2124    {
2125      return
2126        p[0] +
2127        p[1]*x +
2128        p[2]*x*x +
2129        p[3]*x*x*x +
2130        p[4]*x*x*x*x +
2131        p[5]*x*x*x*x*x;
2132    }
2133    //------------------------------------------------------------------------
2134    double ToFdEdx::f_att5B( float x )
2135    {
2136      return
2137        101.9409 +
2138        6.643781*x +
2139        0.2765518*x*x +
2140        0.004617647*x*x*x +
2141        0.0006195132*x*x*x*x +
2142        0.00002813734*x*x*x*x*x;
2143    }
2144    
2145    
2146    double ToFdEdx::f_pos( TArrayF &p, float x )
2147    {
2148      return
2149        p[0] +
2150        p[1]*x +
2151        p[2]*x*x +
2152        p[3]*x*x*x;
2153    }
2154    
2155    double ToFdEdx::f_pos5B( float x )
2156    {
2157      return
2158        15.45132 +
2159        0.8369721*x +
2160        0.0005*x*x;
2161    }
2162    
2163    
2164    
2165    double ToFdEdx::f_adcPC( float x )
2166    {
2167      return 28.12+0.6312*x-5.647e-05*x*x+3.064e-08*x*x*x;
2168    }
2169    
2170    
2171    float ToFdEdx::Get_adc_he( int id, float pl_x[6], float pl_y[6])
2172    {
2173    
2174      //
2175      // input: id - pmt [0:47}
2176      //             pl_x - coord x of the tof plane
2177      //             pl_y - coord y
2178    
2179      adc_he = 0;
2180      if( eGeom.GetXY(id)==1 )  adc_he = f_att( (parAtt[id]), pl_x[eGeom.GetPlane(id)] );
2181      if( eGeom.GetXY(id)==2 )  adc_he = f_att( (parAtt[id]), pl_y[eGeom.GetPlane(id)] );
2182      return adc_he;
2183    }
2184    
2185    //------------------------------------------------------------------------
2186    double ToFdEdx::f_BB( TArrayF &p, float x )
2187    {
2188      return  p[0]/(x*x)*(log(x*x/(1-x*x)) - p[1]*x*x - p[2]);
2189    }
2190    
2191    //------------------------------------------------------------------------
2192    double ToFdEdx::f_BB5B( float x )
2193    {
2194      return  0.165797/(x*x)*(log(x*x/(1-x*x)) + 140.481*x*x + 52.9258);
2195    }
2196    //------------------------------------------------------------------------
2197    double ToFdEdx::f_desatBB( TArrayF &p, float x )
2198    {
2199      return
2200        p[0] +
2201        p[1]*x +
2202        p[2]*x*x;
2203    }
2204    
2205    //------------------------------------------------------------------------
2206    double ToFdEdx::f_desatBB5B( float x )
2207    {
2208      return
2209        -2.4 +
2210        0.75*x +
2211        0.009*x*x;
2212    }
2213    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.36

  ViewVC Help
Powered by ViewVC 1.1.23