warning fixed
[u/mrichter/AliRoot.git] / START / AliSTARTDigitizer.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
18 #include <TTree.h> 
19 #include <TFile.h>
20 #include <TDirectory.h>
21 #include <TRandom.h>
22 #include <TArrayI.h>
23 #include <TError.h>
24
25
26 #include "AliSTARTDigitizer.h"
27 #include "AliSTART.h"
28 #include "AliSTARThit.h"
29 #include "AliSTARThitPhoton.h"
30 #include "AliSTARTdigit.h"
31 #include "AliRunDigitizer.h"
32 #include <AliDetector.h>
33 #include "AliRun.h"
34 #include <AliLoader.h>
35 #include <AliRunLoader.h>
36 #include <stdlib.h>
37 #include <Riostream.h>
38 #include <Riostream.h>
39
40 ClassImp(AliSTARTDigitizer)
41
42 //___________________________________________
43   AliSTARTDigitizer::AliSTARTDigitizer()  :AliDigitizer()
44 {
45 // Default ctor - don't use it
46   ;
47 }
48
49 //___________________________________________
50 AliSTARTDigitizer::AliSTARTDigitizer(AliRunDigitizer* manager) 
51     :AliDigitizer(manager) 
52 {
53   //    cout<<"AliSTARTDigitizer::AliSTARTDigitizer"<<endl;
54 // ctor which should be used
55 //  fDebug =0;
56   if (GetDebug())
57     Info("(AliRunDigitizer* manager)" ,"processed");
58
59   ftimeRightTDC = new TArrayI(12); 
60   ftimeLeftTDC = new TArrayI(12); 
61   fRightADC = new TArrayI(12); 
62   fLeftADC = new TArrayI(12); 
63 }
64
65 //------------------------------------------------------------------------
66 AliSTARTDigitizer::~AliSTARTDigitizer()
67 {
68 // Destructor
69   if(GetDebug()) Info("dtor","START"); 
70   delete ftimeRightTDC;
71   delete ftimeLeftTDC;
72   delete fRightADC;
73   delete fLeftADC;
74 }
75
76  //------------------------------------------------------------------------
77 Bool_t AliSTARTDigitizer::Init()
78 {
79 // Initialization
80 // cout<<"AliSTARTDigitizer::Init"<<endl;
81  return kTRUE;
82 }
83  
84
85 //---------------------------------------------------------------------
86
87 void AliSTARTDigitizer::Exec(Option_t* /*option*/)
88 {
89
90   /*
91     Produde digits from hits
92         digits is TObject and includes
93         We are writing array if left & right  TDC
94         left & right  ADC (will need for slow simulation)
95         TOF first particle left & right
96         mean time and time difference (vertex position)
97         
98   */
99
100   AliRunLoader *inRL, *outRL;//in and out Run Loaders
101   AliLoader *pInStartLoader, *pOutStartLoader;// in and out STARTLoaders
102
103   outRL = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
104   pOutStartLoader = outRL->GetLoader("STARTLoader");
105
106 #ifdef DEBUG
107   cout<<"AliSTARTDigitizer::>Hits2Digits start...\n";
108 #endif
109   //
110   // From hits to digits
111   //
112   Int_t hit, nhits;
113   Float_t meanTime;
114   Int_t countEr[13],countEl[13];
115   Int_t volume,pmt,tr,tl,sumRight;
116   Int_t  bestRightADC,bestLeftADC;
117   Float_t besttimeleftGaus, besttimerightGaus;
118   Float_t timeright[13]={13*0};
119   Float_t timeleft[13]={13*0};
120   Float_t channelWidth=2.5; //ps
121   Int_t channelWidthADC=1; //ps
122   //  Int_t thresholdAmpl=10;
123
124   
125   inRL = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
126   
127
128   AliSTART *fSTART  = (AliSTART*) inRL->GetAliRun()->GetDetector("START");
129   AliSTARThit  *startHit;
130   TBranch *brHits=0;
131   TBranch *brHitPhoton=0;
132   fdigits= new AliSTARTdigit();
133
134   Int_t nFiles=fManager->GetNinputs();
135   for (Int_t inputFile=0; inputFile<nFiles;  inputFile++) {
136
137     Float_t besttimeright=9999.;
138     Float_t besttimeleft=9999.;
139     Int_t iTimeDiff=0;
140     Int_t iTimeAv=0;
141     Float_t timeDiff,timeAv; 
142     sumRight=0;
143     for (Int_t i0=0; i0<13; i0++)
144       {
145         timeright[i0]=0; timeleft[i0]=0;
146         countEr[i0]=0;   countEl[i0]=0;
147       }
148
149     inRL = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
150     pInStartLoader = inRL->GetLoader("STARTLoader");
151     pInStartLoader->LoadHits("READ");//probably it is necessary to load them before
152     pOutStartLoader->LoadDigits("UPDATE");//probably it is necessary to load them before
153     TClonesArray *fHits = fSTART->Hits ();
154
155     TTree *th = pInStartLoader->TreeH();
156     brHits = th->GetBranch("START");
157     brHitPhoton = th->GetBranch("STARThitPhoton");
158     if (brHits) {
159       fSTART->SetHitsAddressBranch(brHits,brHitPhoton);
160     }else{
161       cerr<<"EXEC Branch START hit not found"<<endl;
162       exit(111);
163     } 
164     Int_t ntracks    = (Int_t) th->GetEntries();
165 #ifdef DEBUG
166    Info("Digitizer",ntracks);
167 #endif
168      if (ntracks<=0) return;
169     // Start loop on tracks in the hits containers
170     for (Int_t track=0; track<ntracks;track++) {
171       brHits->GetEntry(track);
172       nhits = fHits->GetEntriesFast();
173       for (hit=0;hit<nhits;hit++) {
174         startHit   = (AliSTARThit*) fHits->UncheckedAt(hit);
175         if (!startHit) {
176           ::Error("Exec","The unchecked hit doesn't exist");
177           break;
178         }
179         pmt=startHit->Pmt();
180         volume = startHit->Volume();
181         if(volume==1){
182           timeright[pmt] = startHit->Time();
183           if(timeright[pmt]<besttimeright)
184             {
185               besttimeright=timeright[pmt];
186           } //timeright
187         }//time for right shoulder
188         if(volume==2){            
189           timeleft[pmt] = startHit->Time();
190           if(timeleft[pmt]<besttimeleft)
191             {
192               besttimeleft=timeleft[pmt];
193             
194           } //timeleftbest
195         }//time for left shoulder
196       } //hit loop
197     } //track loop
198   
199     // z position
200
201     //folding with experimental time distribution
202     
203     Float_t koef=69.7/350.;
204     besttimeright=koef*besttimeright;
205     besttimeleftGaus=gRandom->Gaus(besttimeleft,0.05);
206     bestLeftADC=Int_t (besttimeleftGaus*1000/channelWidth);
207     besttimerightGaus=gRandom->Gaus(besttimeright,0.05);
208     bestRightADC=Int_t (besttimerightGaus*1000/channelWidth);
209     timeDiff=besttimerightGaus-besttimeleftGaus;
210 #ifdef DEBUG
211     cout<<" timediff in ns "<<timeDiff<<" z= "<<timeDiff*30<<endl;
212 #endif
213     meanTime=(besttimerightGaus+besttimeleftGaus)/2.;
214     if ( TMath::Abs(timeDiff)<TMath::Abs(0.3) ) 
215       {
216         Float_t t1=1000.*besttimeleftGaus;
217         Float_t t2=1000.*besttimerightGaus;
218         t1=t1/channelWidth;   //time in ps to channelWidth
219         t2=t2/channelWidth;   //time in ps to channelWidth
220         timeAv=(t1+t2)/2.;// time  channel numbres
221         
222         // Time to TDC signal
223         // 256 channels for timediff, range 1ns
224         iTimeAv=(Int_t)timeAv; 
225         timeDiff= 512+1000*timeDiff/channelWidth; // time  channel numbres 
226         iTimeDiff=(Int_t)timeDiff;
227         //       fill digits
228         fdigits->SetTimeBestLeft(bestLeftADC);
229         fdigits->SetTimeBestRight(bestRightADC);
230         fdigits->SetMeanTime(iTimeAv);
231         fdigits->SetTimeDiff(iTimeDiff);
232         for (Int_t i=0; i<12; i++)
233           {
234             //  fill TDC
235             timeright[i+1]=gRandom->Gaus(timeright[i+1],0.05);
236             timeleft[i+1]=gRandom->Gaus(timeleft[i+1],0.05);
237             tr= Int_t (timeright[i+1]*1000/channelWidth); 
238             if(tr<200) tr=0;
239             tl= Int_t (timeleft[i+1]*1000/channelWidth); 
240             if(tl<1000) tl=0;
241             
242             ftimeRightTDC->AddAt(tr,i);
243             ftimeLeftTDC->AddAt(tl,i);
244             //fill ADC
245             Int_t al=( Int_t ) countEl[i+1]/ channelWidthADC;
246             Int_t ar=( Int_t ) countEr[i+1]/ channelWidthADC;
247             fRightADC->AddAt(ar,i);
248             fLeftADC ->AddAt(al,i);
249             sumRight+=countEr[i+1];
250           }
251         fdigits->SetTimeRight(*ftimeRightTDC);
252         fdigits->SetTimeLeft(*ftimeLeftTDC);
253         fdigits->SetADCRight(*fRightADC);
254         fdigits->SetADCLeft(*fLeftADC);
255         fdigits->SetSumADCRight(sumRight);
256       }
257     else
258       {timeAv=999999; timeDiff=99999;}
259
260 // trick to find out output dir:
261
262     TDirectory *wd = gDirectory;
263     pOutStartLoader->GetDigitsDataLoader()->GetDirectory()->cd();
264     fdigits->Write("START_D");
265     wd->cd();
266     pInStartLoader->UnloadHits();
267     pOutStartLoader->UnloadDigits();
268   } //event loop
269 }
270
271
272 //------------------------------------------------------------------------
273 Bool_t AliSTARTDigitizer::RegisterPhotoE(/*AliSTARThitPhoton *hit*/)
274 {
275     Double_t    pP = 0.2;    
276     Double_t    p;
277     
278     p = gRandom->Rndm();
279     if (p > pP)
280       return kFALSE;
281     
282     return kTRUE;
283 }
284 //----------------------------------------------------------------------------