]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0Digitizer.cxx
Introducing TGeoMatrix where needed
[u/mrichter/AliRoot.git] / T0 / AliT0Digitizer.cxx
1
2 /**************************************************************************
3  * Copyright(c) 1998-2000, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /* $Id$ */
18
19 #include <TArrayI.h>
20 #include <TDirectory.h>
21 #include <TError.h>
22 #include <TFile.h>
23 #include <TGraph.h>
24 #include <TH1F.h>
25 #include <TMath.h>
26 #include <TRandom.h>
27 #include <TTree.h> 
28
29 #include "AliLog.h"
30 #include "AliT0Digitizer.h"
31 #include "AliT0.h"
32 #include "AliT0hit.h"
33 #include "AliT0digit.h"
34 #include "AliRunDigitizer.h"
35 #include <AliDetector.h>
36 #include "AliRun.h"
37 #include <AliLoader.h>
38 #include <AliRunLoader.h>
39 #include <stdlib.h>
40 #include <Riostream.h>
41 #include <Riostream.h>
42 #include "AliT0Parameters.h"
43 #include "AliCDBLocal.h"
44 #include "AliCDBStorage.h"
45 #include "AliCDBManager.h"
46 #include "AliCDBEntry.h"
47
48 ClassImp(AliT0Digitizer)
49
50 //___________________________________________
51   AliT0Digitizer::AliT0Digitizer()  :AliDigitizer(),
52                                      fT0(0),
53                                      fHits(0),
54                                      fdigits(0),
55                                      ftimeCFD(new TArrayI(24)), 
56                                      ftimeLED (new TArrayI(24)), 
57                                      fADC(new TArrayI(24)), 
58                                      fADC0 (new TArrayI(24)),
59                                      fSumMult(0),
60                                      fAmpLED(0),
61                                      fParam(0)
62
63
64 {
65 // Default ctor - don't use it
66   ;
67 }
68
69 //___________________________________________
70 AliT0Digitizer::AliT0Digitizer(AliRunDigitizer* manager) 
71   :AliDigitizer(manager),
72    fT0(0),
73    fHits(0),
74    fdigits(0),
75    ftimeCFD(new TArrayI(24)), 
76    ftimeLED (new TArrayI(24)), 
77    fADC(new TArrayI(24)), 
78    fADC0 (new TArrayI(24)),
79    fSumMult(0),
80    fAmpLED(0),
81    fParam(0)
82 {
83 // ctor which should be used
84
85   AliDebug(1,"processed");
86   fParam = AliT0Parameters::Instance();
87   fParam->Init();
88
89   for (Int_t i=0; i<24; i++){
90     TGraph* gr = fParam ->GetAmpLEDRec(i);
91     Int_t np = gr->GetN();
92     Double_t *x = gr->GetX();
93     Double_t *y = gr->GetY();
94
95     Double_t *x1 = new Double_t[np];
96     Double_t *y1 = new Double_t[np];
97     for (Int_t ii=0; ii<np; ii++) {
98       y1[ii]=y[np-ii-1]; x1[ii]=x[np-ii-1];
99     }
100     
101     TGraph *grInverse = new TGraph(np,y1,x1);
102     delete [] x1;
103     delete [] y1;
104     fAmpLED.AddAtAndExpand(grInverse,i);
105   }
106 }
107 //------------------------------------------------------------------------
108 AliT0Digitizer::~AliT0Digitizer()
109 {
110 // Destructor
111
112   AliDebug(1,"T0");
113
114   delete ftimeCFD;
115   delete fADC;
116   delete ftimeLED;
117   delete  fADC0;
118   //  delete fAmpLED;
119 }
120
121 //------------------------------------------------------------------------
122 Bool_t AliT0Digitizer::Init()
123 {
124 // Initialization
125   AliDebug(1," Init");
126  return kTRUE;
127 }
128  
129 //---------------------------------------------------------------------
130 void AliT0Digitizer::Exec(Option_t* /*option*/)
131 {
132
133   /*
134     Produde digits from hits
135         digits is TObject and includes
136         We are writing array if C & A  TDC
137         C & A  ADC (will need for slow simulation)
138         TOF first particle C & A
139         mean time and time difference (vertex position)
140         
141   */
142
143   //output loader 
144   AliRunLoader *outRL = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
145   AliLoader * pOutStartLoader = outRL->GetLoader("T0Loader");
146
147   AliDebug(1,"start...");
148   //input loader
149   //
150   // From hits to digits
151   //
152   Int_t hit, nhits;
153   Int_t countE[24];
154   Int_t volume, pmt, trCFD, trLED; 
155   Float_t sl, qt;
156   Int_t  bestATDC, bestCTDC, qtCh;
157   Float_t time[24], besttime[24], timeGaus[24] ;
158   //Q->T-> coefficients !!!! should be asked!!!
159   Float_t timeDelayCFD[24];
160   Int_t threshold =50; //photoelectrons
161   Float_t zdetA, zdetC;
162   Int_t sumMultCoeff = 100;
163   Int_t refpoint=0;
164   TH1F *hr;
165
166
167   
168   Int_t ph2Mip = fParam->GetPh2Mip();     
169   Float_t channelWidth = fParam->GetChannelWidth() ;  
170   Float_t delayVertex = fParam->GetTimeDelayTVD();
171    
172   zdetC = TMath::Abs(fParam->GetZPosition("T0/C/PMT1"));
173   zdetA  = TMath::Abs(fParam->GetZPosition("T0/A/PMT15"));
174   
175   AliT0hit  *startHit;
176   TBranch *brHits=0;
177   
178   Int_t nFiles=fManager->GetNinputs();
179   for (Int_t inputFile=0; inputFile<nFiles;  inputFile++) {
180     if (inputFile < nFiles-1) {
181       AliWarning(Form("ignoring input stream %d", inputFile));
182       continue;
183       
184     }
185     
186     Float_t besttimeC=99999.;
187     Float_t besttimeA=99999.;
188     Int_t pmtBestC=9999;
189     Int_t pmtBestA=9999;
190     Int_t timeDiff=999, meanTime=0;
191     Int_t sumMult =0;   fSumMult=0;
192     bestATDC = 99999;  bestCTDC = 99999;
193  
194
195     ftimeCFD -> Reset();
196     fADC -> Reset();
197     fADC0 -> Reset();
198     ftimeLED ->Reset();
199     for (Int_t i0=0; i0<24; i0++)
200       {
201         time[i0]=besttime[i0]=timeGaus[i0]=99999; countE[i0]=0;
202       }
203     AliRunLoader * inRL = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
204     AliLoader * pInStartLoader = inRL->GetLoader("T0Loader");
205     if (!inRL->GetAliRun()) inRL->LoadgAlice();
206     AliT0 *fT0  = (AliT0*)inRL ->GetAliRun()->GetDetector("T0");
207
208        //read Hits 
209     pInStartLoader->LoadHits("READ");//probably it is necessary to load them before
210     TClonesArray *fHits = fT0->Hits ();
211     TTree *th = pInStartLoader->TreeH();
212     brHits = th->GetBranch("T0");
213     if (brHits) {
214       fT0->SetHitsAddressBranch(brHits);
215     }else{
216       AliError("Branch T0 hit not found");
217       exit(111);
218     } 
219     Int_t ntracks    = (Int_t) th->GetEntries();
220     
221     if (ntracks<=0) return;
222     // Start loop on tracks in the hits containers
223     for (Int_t track=0; track<ntracks;track++) {
224       brHits->GetEntry(track);
225       nhits = fHits->GetEntriesFast();
226       for (hit=0;hit<nhits;hit++) 
227         {
228           startHit   = (AliT0hit*) fHits->UncheckedAt(hit);
229           if (!startHit) {
230             AliError("The unchecked hit doesn't exist");
231             break;
232           }
233           pmt=startHit->Pmt();
234           Int_t numpmt=pmt-1;
235           Double_t e=startHit->Etot();
236           volume = startHit->Volume();
237           
238           //      if(e>0 && RegisterPhotoE(numpmt,e)) {
239           if(e>0 ) {
240             countE[numpmt]++;
241             besttime[numpmt] = startHit->Time();
242             if(besttime[numpmt]<time[numpmt])
243               {
244                 time[numpmt]=besttime[numpmt];
245               }
246           } //photoelectron accept 
247         } //hits loop
248     } //track loop
249     
250     //spread time A&C by 25ps   && besttime
251     Float_t c = 0.0299792; // cm/ps
252     
253     Float_t koef=(zdetA-zdetC)/c; //correction position difference by cable
254     for (Int_t ipmt=0; ipmt<12; ipmt++){
255       if(countE[ipmt] > threshold) {
256         timeGaus[ipmt]=gRandom->Gaus(time[ipmt],25)+koef;
257         if(timeGaus[ipmt]<besttimeC){
258           besttimeC=timeGaus[ipmt]; //timeC
259           pmtBestC=ipmt;}
260       }
261     }
262     for ( Int_t ipmt=12; ipmt<24; ipmt++){
263       if(countE[ipmt] > threshold) {
264         timeGaus[ipmt]=gRandom->Gaus(time[ipmt],25); 
265         if(timeGaus[ipmt]<besttimeA) {
266           besttimeA=timeGaus[ipmt]; //timeA
267           pmtBestA=ipmt;}
268       } 
269     }
270
271     timeDelayCFD[0] = fParam->GetTimeDelayCFD(0);
272     Int_t meanTimeDelay=200;
273
274     for (Int_t i=0; i<24; i++)
275       {
276         Float_t  al = countE[i]; 
277         if (al>threshold && timeGaus[i]<50000 ) {
278           //fill ADC
279           // QTC procedure:
280           // phe -> mV 0.3; 1MIP ->500phe -> ln (amp (mV)) = 5;
281           // max 200ns, HIJING  mean 50000phe -> 15000mv -> ln = 15 (s zapasom)
282           // channel 25ps
283           qt= 50.*al/ph2Mip;  // 50mv/Mip amp in mV 
284           //  fill TDC
285           timeDelayCFD[i] = fParam->GetTimeDelayCFD(i);
286           trCFD = Int_t (timeGaus[i]/channelWidth + (timeDelayCFD[i]-timeDelayCFD[0])); 
287           TGraph* gr = ((TGraph*)fAmpLED.At(i));
288           sl = gr->Eval(qt);
289
290           trLED = Int_t(( timeGaus[i] + 1000*sl )/channelWidth);
291           qtCh=Int_t (1000.*TMath::Log(qt) / channelWidth);
292           fADC0->AddAt(0,i);
293           fADC->AddAt(qtCh,i);
294           ftimeLED->AddAt(trLED,i); 
295           //      sumMult += Int_t ((al*gain[i]/ph2Mip)*50) ;
296           sumMult += Int_t (qt/sumMultCoeff)  ;
297          
298           // put slewing 
299           TGraph *fu=(TGraph*) fParam ->GetWalk(i) ;
300           Float_t slew=fu->Eval(Float_t(qtCh));
301           hr=fu->GetHistogram();
302           Float_t maxValue=hr->GetMaximum(50);
303           trCFD=trCFD-Int_t((maxValue-slew)/channelWidth);
304           ftimeCFD->AddAt(Int_t (trCFD),i);
305           AliDebug(10,Form("  pmt %i : time in ns %f time in channels %i   ",
306                            i, timeGaus[i],trCFD ));
307           AliDebug(10,Form(" qt in mV %f qt in ns %f qt in channels %i   ",qt, 
308                            TMath::Log(qt), qtCh));
309
310         }
311       } //pmt loop
312
313     //folding with alignmentz position distribution  
314     if( besttimeC > 10000. && besttimeC <15000)
315       bestCTDC=Int_t ((besttimeC+timeDelayCFD[pmtBestC])
316                          /channelWidth);
317  
318     if( besttimeA > 10000. && besttimeA <15000)
319       bestATDC=Int_t ((besttimeA+timeDelayCFD[pmtBestA])
320                         /channelWidth);
321
322     if (bestATDC < 99999 && bestCTDC < 99999)
323       {
324         timeDiff=Int_t (((besttimeC-besttimeA)+1000*delayVertex)
325                         /channelWidth);
326         meanTime=Int_t (((besttimeC+besttimeA)/2. + meanTimeDelay)/channelWidth);
327       }
328         AliDebug(10,Form(" time A& C %i %i  time diff && mean time in channels %i %i",bestATDC,bestCTDC, timeDiff, meanTime));
329
330     if (sumMult > threshold){
331       fSumMult =  Int_t (1000.* TMath::Log(Double_t(sumMult) / Double_t(sumMultCoeff))
332                          /channelWidth);
333       AliDebug(10,Form("summult mv %i   mult  in chammens %i in ps %i ", 
334                       sumMult, fSumMult, fSumMult*channelWidth));
335     }
336
337     fT0->AddDigit(bestATDC,bestCTDC,meanTime,timeDiff,fSumMult, refpoint,
338                        ftimeCFD,fADC0,ftimeLED,fADC);
339      
340     
341       AliDebug(10,Form(" Digits wrote refpoint %i bestATDC %i bestCTDC %i  meanTime %i  timeDiff %i fSumMult %i ",refpoint ,bestATDC,bestCTDC,meanTime,timeDiff,fSumMult ));
342     pOutStartLoader->UnloadHits();
343   } //input streams loop
344   
345     //load digits    
346     pOutStartLoader->LoadDigits("UPDATE");
347     TTree *treeD  = pOutStartLoader->TreeD();
348     if (treeD == 0x0) {
349       pOutStartLoader->MakeTree("D");
350       treeD = pOutStartLoader->TreeD();
351     }
352     treeD->Reset();
353     fT0  = (AliT0*)outRL ->GetAliRun()->GetDetector("T0");
354     // Make a branch in the tree 
355     fT0->MakeBranch("D");
356      treeD->Fill();
357   
358      pOutStartLoader->WriteDigits("OVERWRITE");
359      
360      fT0->ResetDigits();
361      pOutStartLoader->UnloadDigits();
362      
363 }