]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFSDigitizer.cxx
Code Optimization, Review on SDigitization, Test macro to run SDigitization, QA macro...
[u/mrichter/AliRoot.git] / TOF / AliTOFSDigitizer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 //_________________________________________________________________________
17 // This is a TTask that constructs SDigits out of Hits
18 // A Summable Digits is the "sum" of all hits in a pad
19 // Detector response has been simulated via the method
20 // SimulateDetectorResponse
21 //
22 //-- Authors: F. Pierella, A. De Caro
23 // Use case: see AliTOFhits2sdigits.C macro in the CVS
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "TBenchmark.h"
27 #include "TTask.h"
28 #include "TTree.h"
29 #include "TSystem.h"
30 #include "TFile.h"
31 #include "TParticle.h"
32 #include "TH1.h"
33
34 #include "AliTOFHitMap.h"
35 #include "AliTOFSDigit.h"
36 #include "AliTOFConstants.h"
37 #include "AliTOFhit.h"
38 #include "AliTOF.h"
39 #include "AliTOFv1.h"
40 #include "AliTOFv2.h"
41 #include "AliTOFv3.h"
42 #include "AliTOFv4.h"
43 #include "AliTOFSDigitizer.h"
44 #include "AliRun.h"
45 #include "AliDetector.h"
46 #include "AliMC.h"
47
48 #include "TFile.h"
49 #include "TTask.h"
50 #include "TTree.h"
51 #include "TSystem.h"
52 #include "TROOT.h"
53 #include "TFolder.h"
54 #include <TF1.h>
55 #include <stdlib.h>
56 #include <iostream.h>
57 #include <fstream.h>
58
59 ClassImp(AliTOFSDigitizer)
60
61 //____________________________________________________________________________ 
62   AliTOFSDigitizer::AliTOFSDigitizer():TTask("AliTOFSDigitizer","") 
63 {
64   // ctor
65   fSDigits = 0 ;
66   fEvent1=0;
67   fEvent2=0;
68   ftail    = 0;
69 }
70            
71 //____________________________________________________________________________ 
72   AliTOFSDigitizer::AliTOFSDigitizer(char* HeaderFile,char *SdigitsFile, Int_t evNumber1, Int_t nEvents):TTask("AliTOFSDigitizer","") 
73 {
74   fEvent1=evNumber1;
75   fEvent2=fEvent1+nEvents;
76   fSDigits = 0;
77   ftail    = 0;
78
79   fHeadersFile = HeaderFile ; // input filename (with hits)
80   fSDigitsFile = SdigitsFile; // output filename for sdigits
81   TFile * file = (TFile*) gROOT->GetFile(fHeadersFile.Data() ) ;
82
83   //File was not opened yet
84   // open file and get alirun object
85   if(file == 0){
86       file =    TFile::Open(fHeadersFile.Data(),"update") ;
87       gAlice = (AliRun *) file->Get("gAlice") ;
88   }
89
90   // init parameters for sdigitization
91   InitParameters();
92
93   // add Task to //root/Tasks folder
94   TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ; 
95   roottasks->Add(this) ; 
96 }
97
98 //____________________________________________________________________________ 
99   AliTOFSDigitizer::~AliTOFSDigitizer()
100 {
101   // dtor
102   if (ftail)
103     {
104       delete ftail;
105       ftail = 0;
106     }
107 }
108
109 //____________________________________________________________________________ 
110 void AliTOFSDigitizer::InitParameters()
111 {
112   // set parameters for detector simulation
113
114   fTimeResolution =0.120;
115   fpadefficiency  =0.99 ;
116   fEdgeEffect     = 2   ;
117   fEdgeTails      = 0   ;
118   fHparameter     = 0.4 ;
119   fH2parameter    = 0.15;
120   fKparameter     = 0.5 ;
121   fK2parameter    = 0.35;
122   fEffCenter      = fpadefficiency;
123   fEffBoundary    = 0.65;
124   fEff2Boundary   = 0.90;
125   fEff3Boundary   = 0.08;
126   fResCenter      = 50. ;
127   fResBoundary    = 70. ;
128   fResSlope       = 40. ;
129   fTimeWalkCenter = 0.  ;
130   fTimeWalkBoundary=0.  ;
131   fTimeWalkSlope  = 0.  ;
132   fTimeDelayFlag  = 1   ;
133   fPulseHeightSlope=2.0 ;
134   fTimeDelaySlope =0.060;
135   // was fMinimumCharge = TMath::Exp(fPulseHeightSlope*fKparameter/2.);
136   fMinimumCharge = TMath::Exp(-fPulseHeightSlope*fHparameter);
137   fChargeSmearing=0.0   ;
138   fLogChargeSmearing=0.13;
139   fTimeSmearing   =0.022;
140   fAverageTimeFlag=0    ;
141   fTdcBin   = 50.;      // 1 TDC bin = 50 ps
142   fAdcBin   = 0.25;     // 1 ADC bin = 0.25 pC (or 0.03 pC)
143   fAdcMean  = 50.;     // ADC distribution mpv value for Landau (in bins)
144                        // it corresponds to a mean value of ~100 bins
145   fAdcRms   = 25.;     // ADC distribution rms value (in bins)
146                        // it corresponds to distribution rms ~50 bins
147 }
148
149 //__________________________________________________________________
150 Double_t TimeWithTail(Double_t* x, Double_t* par)
151 {
152   // sigma - par[0], alpha - par[1], part - par[2]
153   //  at x<part*sigma - gauss
154   //  at x>part*sigma - TMath::Exp(-x/alpha)
155   Float_t xx =x[0];
156   Double_t f;
157   if(xx<par[0]*par[2]) {
158     f = TMath::Exp(-xx*xx/(2*par[0]*par[0]));
159   } else {
160     f = TMath::Exp(-(xx-par[0]*par[2])/par[1]-0.5*par[2]*par[2]);
161   }
162   return f;
163 }
164
165
166 //____________________________________________________________________________
167 void AliTOFSDigitizer::Exec(Option_t *verboseOption, Option_t *allEvents) { 
168
169   if(strstr(verboseOption,"tim") || strstr(verboseOption,"all"))
170     gBenchmark->Start("TOFSDigitizer");
171
172   AliTOF *TOF = (AliTOF *) gAlice->GetDetector ("TOF");
173
174   if (!TOF) {
175     Error("AliTOFSDigitizer","TOF not found");
176     return;
177   }
178
179   if (fEdgeTails) ftail = new TF1("tail",TimeWithTail,-2,2,3);
180
181   Int_t nselectedHits=0;
182   Int_t ntotalsdigits=0;
183   Int_t ntotalupdates=0;
184   Int_t nnoisesdigits=0;
185   Int_t nsignalsdigits=0;
186   Int_t nHitsFromPrim=0;
187   Int_t nHitsFromSec=0;
188   Int_t nlargeTofDiff=0;
189
190   if (strstr(allEvents,"all")){
191     fEvent1=0;
192     fEvent2= (Int_t) gAlice->TreeE()->GetEntries();
193   }
194
195   for (Int_t ievent = fEvent1; ievent < fEvent2; ievent++) {
196     cout << "------------------- "<< GetName() << " -------------" << endl ;
197     cout << "Sdigitizing event " << ievent << endl;
198
199     Int_t nselectedHitsinEv=0;
200     Int_t ntotalsdigitsinEv=0;
201     Int_t ntotalupdatesinEv=0;
202     Int_t nnoisesdigitsinEv=0;
203     Int_t nsignalsdigitsinEv=0;
204
205     gAlice->GetEvent(ievent);
206     TTree *TH = gAlice->TreeH ();
207     if (!TH)
208       return;
209     if (gAlice->TreeS () == 0)
210       gAlice->MakeTree ("S");
211
212       
213     //Make branches
214     char branchname[20];
215     sprintf (branchname, "%s", TOF->GetName ());
216     //Make branch for digits
217     TOF->MakeBranch ("S");
218     
219     //Now made SDigits from hits
220
221     Int_t    vol[5];       // location for a digit
222     Float_t  digit[2];     // TOF digit variables
223     TParticle *particle;
224     AliTOFhit *tofHit;
225     TClonesArray *TOFhits = TOF->Hits();
226
227     // create hit map
228     AliTOFHitMap *hitMap = new AliTOFHitMap(TOF->SDigits());
229
230     Int_t ntracks = static_cast<Int_t>(TH->GetEntries());
231     for (Int_t track = 0; track < ntracks; track++)
232     {
233       gAlice->ResetHits();
234       TH->GetEvent(track);
235       particle = gAlice->Particle(track);
236       Int_t nhits = TOFhits->GetEntriesFast();
237       // cleaning all hits of the same track in the same pad volume
238       // it is a rare event, however it happens
239
240       Int_t previousTrack =0;
241       Int_t previousSector=0;
242       Int_t previousPlate =0;
243       Int_t previousStrip =0;
244       Int_t previousPadX  =0;
245       Int_t previousPadZ  =0;
246
247       for (Int_t hit = 0; hit < nhits; hit++)
248       {
249         tofHit = (AliTOFhit *) TOFhits->UncheckedAt(hit);
250         Int_t tracknum = tofHit->GetTrack();
251         vol[0] = tofHit->GetSector();
252         vol[1] = tofHit->GetPlate();
253         vol[2] = tofHit->GetStrip();
254         vol[3] = tofHit->GetPadx();
255         vol[4] = tofHit->GetPadz();
256
257         Bool_t dummy=((tracknum==previousTrack) && (vol[0]==previousSector) && (vol[1]==previousPlate) && (vol[2]==previousStrip));
258
259         Bool_t isCloneOfThePrevious=dummy && ((vol[3]==previousPadX) && (vol[4]==previousPadZ));
260
261         // much stronger check to be inserted here
262
263         if(!isCloneOfThePrevious){
264           // update "previous" values
265           // in fact, we are yet in the future, so the present is past
266           previousTrack=tracknum;
267           previousSector=vol[0];
268           previousPlate=vol[1];
269           previousStrip=vol[2];
270           previousPadX=vol[3];
271           previousPadZ=vol[4];
272
273           nselectedHits++;
274           nselectedHitsinEv++;
275           if (particle->GetFirstMother() < 0){
276             nHitsFromPrim++;
277           } // counts hits due to primary particles
278
279           Float_t Xpad = tofHit->GetDx();
280           Float_t Zpad = tofHit->GetDz();
281           Float_t xStrip=AliTOFConstants::fgkXPad*(vol[3]-0.5-0.5*AliTOFConstants::fgkNpadX)+Xpad;
282           Float_t zStrip=AliTOFConstants::fgkZPad*(vol[4]-0.5-0.5*AliTOFConstants::fgkNpadZ)+Zpad;
283           Float_t geantTime = tofHit->GetTof(); // unit [s]
284           geantTime *= 1.e+09;  // conversion from [s] to [ns]
285
286           //cout << "geantTime " << geantTime << " [ns]" << endl;
287           Int_t nActivatedPads = 0, nFiredPads = 0;
288           Bool_t isFired[4] = {kFALSE, kFALSE, kFALSE, kFALSE};
289           Float_t tofAfterSimul[4] = {0., 0., 0., 0.};
290           Float_t qInduced[4] = {0.,0.,0.,0.};
291           Int_t nPlace[4] = {0, 0, 0, 0};
292           Float_t averageTime = 0.;
293           SimulateDetectorResponse(zStrip,xStrip,geantTime,nActivatedPads,nFiredPads,isFired,nPlace,qInduced,tofAfterSimul,averageTime);
294           if(nFiredPads) {
295             for(Int_t indexOfPad=0; indexOfPad<nActivatedPads; indexOfPad++) {
296               if(isFired[indexOfPad]){ // the pad has fired
297                 Float_t timediff=geantTime-tofAfterSimul[indexOfPad];
298
299                 if(timediff>=0.2) nlargeTofDiff++;
300
301                 digit[0] = (Int_t) ((tofAfterSimul[indexOfPad]*1.e+03)/fTdcBin); // TDC bin number (each bin -> 25. ps)
302
303                 Float_t landauFactor = gRandom->Landau(fAdcMean, fAdcRms); 
304                 digit[1] = (Int_t) (qInduced[indexOfPad] * landauFactor); // ADC bins (each bin -> 0.25 (or 0.03) pC)
305
306                 // recalculate the volume only for neighbouring pads
307                 if(indexOfPad){
308                   (nPlace[indexOfPad]<=AliTOFConstants::fgkNpadX) ? vol[4] = 1 : vol[4] = 2;
309                   (nPlace[indexOfPad]<=AliTOFConstants::fgkNpadX) ? vol[3] = nPlace[indexOfPad] : vol[3] = nPlace[indexOfPad] - AliTOFConstants::fgkNpadX;
310                 }
311
312                 // check if two sdigit are on the same pad; in that case we sum
313                 // the two or more sdigits
314                 if (hitMap->TestHit(vol) != kEmpty) {
315                   AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(hitMap->GetHit(vol));
316                   Int_t tdctime = (Int_t) digit[0];
317                   Int_t adccharge = (Int_t) digit[1];
318                   sdig->Update(tdctime,adccharge,tracknum);
319                   ntotalupdatesinEv++;
320                   ntotalupdates++;
321                 } else {
322
323                   TOF->AddSDigit(tracknum, vol, digit);
324
325                   if(indexOfPad){
326                     nnoisesdigits++;
327                     nnoisesdigitsinEv++;
328                   } else {
329                     nsignalsdigits++;
330                     nsignalsdigitsinEv++;
331                   }
332                   ntotalsdigitsinEv++;  
333                   ntotalsdigits++;
334                   hitMap->SetHit(vol);
335                 } // if (hitMap->TestHit(vol) != kEmpty)
336               } // if(isFired[indexOfPad])
337             } // end loop on nActivatedPads
338           } // if(nFiredPads) i.e. if some pads has fired
339         } // close if(!isCloneOfThePrevious)
340       } // end loop on hits for the current track
341     } // end loop on ntracks
342
343     delete hitMap;
344       
345     gAlice->TreeS()->Reset();
346     gAlice->TreeS()->Fill();
347     //gAlice->TreeS()->Write(0,TObject::kOverwrite) ;
348     gAlice->TreeS()->AutoSave();
349
350     if(strstr(verboseOption,"all")){
351       cout << "----------------------------------------" << endl;
352       cout << "       <AliTOFSDigitizer>     " << endl;
353       cout << "After sdigitizing " << nselectedHitsinEv << " hits" << " in event " << ievent << endl;
354       //" (" << nHitsFromPrim << " from primaries and " << nHitsFromSec << " from secondaries) TOF hits, " 
355       cout << ntotalsdigitsinEv << " digits have been created " << endl;
356       cout << "(" << nsignalsdigitsinEv << " due to signals and " <<  nnoisesdigitsinEv << " due to border effect)" << endl;
357       cout << ntotalupdatesinEv << " total updates of the hit map have been performed in current event" << endl;
358       cout << "----------------------------------------" << endl;
359     }
360
361   } //event loop on events
362
363   // free used memory
364   if (ftail){
365     delete ftail;
366     ftail = 0;
367   }
368   
369   nHitsFromSec=nselectedHits-nHitsFromPrim;
370   if(strstr(verboseOption,"all")){
371     cout << "----------------------------------------" << endl;
372     cout << "----------------------------------------" << endl;
373     cout << "-----------SDigitization Summary--------" << endl;
374     cout << "       <AliTOFSDigitizer>     " << endl;
375     cout << "After sdigitizing " << nselectedHits << " hits" << endl;
376     cout << "in " << (fEvent2-fEvent1) << " events" << endl;
377 //" (" << nHitsFromPrim << " from primaries and " << nHitsFromSec << " from secondaries) TOF hits, " 
378     cout << ntotalsdigits << " sdigits have been created " << endl;
379     cout << "(" << nsignalsdigits << " due to signals and " <<  nnoisesdigits << " due to border effect)" << endl;
380     cout << ntotalupdates << " total updates of the hit map have been performed" << endl;
381     cout << "in " << nlargeTofDiff << " cases the time of flight difference is greater than 200 ps" << endl;
382   }
383
384
385   if(strstr(verboseOption,"tim") || strstr(verboseOption,"all")){
386     gBenchmark->Stop("TOFSDigitizer");
387     cout << "AliTOFSDigitizer:" << endl ;
388     cout << "   took " << gBenchmark->GetCpuTime("TOFSDigitizer") << " seconds in order to make sdigits " 
389          <<  gBenchmark->GetCpuTime("TOFSDigitizer")/(fEvent2-fEvent1) << " seconds per event " << endl ;
390     cout << endl ;
391   }
392
393   Print("");
394 }
395  
396 //__________________________________________________________________
397 void AliTOFSDigitizer::SetSDigitsFile(char * file ){
398   if(!fSDigitsFile.IsNull())
399     cout << "Changing SDigits file from " <<(char *)fSDigitsFile.Data() << " to " << file << endl ;
400   fSDigitsFile=file ;
401 }
402
403 //__________________________________________________________________
404 void AliTOFSDigitizer::Print(Option_t* opt)const
405 {
406   cout << "------------------- "<< GetName() << " -------------" << endl ;
407   if(fSDigitsFile.IsNull())
408     cout << " Writing SDigits to file with hits "<< endl ;
409   else
410     cout << "    Writing SDigits to file  " << (char*) fSDigitsFile.Data() << endl ;
411   cout << "--------------------------------------------------" << endl;
412 }
413
414 //__________________________________________________________________
415 void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t geantTime, Int_t& nActivatedPads, Int_t& nFiredPads, Bool_t* isFired, Int_t* nPlace, Float_t* qInduced, Float_t* tofTime, Float_t& averageTime)
416 {
417   // Description:
418   // Input:  z0, x0 - hit position in the strip system (0,0 - center of the strip), cm
419   //         geantTime - time generated by Geant, ns
420   // Output: nActivatedPads - the number of pads activated by the hit (1 || 2 || 4)
421   //         nFiredPads - the number of pads fired (really activated) by the hit (nFiredPads <= nActivatedPads)
422   //         qInduced[iPad]- charge induced on pad, arb. units
423   //                         this array is initialized at zero by the caller
424   //         tofAfterSimul[iPad] - time calculated with edge effect algorithm, ns
425   //                                   this array is initialized at zero by the caller
426   //         averageTime - time given by pad hited by the Geant track taking into account the times (weighted) given by the pads fired for edge effect also.
427   //                       The weight is given by the qInduced[iPad]/qCenterPad
428   //                                   this variable is initialized at zero by the caller
429   //         nPlace[iPad] - the number of the pad place, iPad = 0, 1, 2, 3
430   //                                   this variable is initialized at zero by the caller
431   //
432   // Description of used variables:
433   //         eff[iPad] - efficiency of the pad
434   //         res[iPad] - resolution of the pad, ns
435   //         timeWalk[iPad] - time walk of the pad, ns
436   //         timeDelay[iPad] - time delay for neighbouring pad to hited pad, ns
437   //         PadId[iPad] - Pad Identifier
438   //                    E | F    -->   PadId[iPad] = 5 | 6
439   //                    A | B    -->   PadId[iPad] = 1 | 2
440   //                    C | D    -->   PadId[iPad] = 3 | 4
441   //         nTail[iPad] - the tail number, = 1 for tailA, = 2 for tailB
442   //         qCenterPad - charge extimated for each pad, arb. units
443   //         weightsSum - sum of weights extimated for each pad fired, arb. units
444   
445   const Float_t kSigmaForTail[2] = {AliTOFConstants::fgkSigmaForTail1,AliTOFConstants::fgkSigmaForTail2}; //for tail                                                   
446   Int_t iz = 0, ix = 0;
447   Float_t dX = 0., dZ = 0., x = 0., z = 0.;
448   Float_t h = fHparameter, h2 = fH2parameter, k = fKparameter, k2 = fK2parameter;
449   Float_t effX = 0., effZ = 0., resX = 0., resZ = 0., timeWalkX = 0., timeWalkZ = 0.;
450   Float_t logOfqInd = 0.;
451   Float_t weightsSum = 0.;
452   Int_t nTail[4]  = {0,0,0,0};
453   Int_t padId[4]  = {0,0,0,0};
454   Float_t eff[4]  = {0.,0.,0.,0.};
455   Float_t res[4]  = {0.,0.,0.,0.};
456   //  Float_t qCenterPad = fMinimumCharge * fMinimumCharge;
457   Float_t qCenterPad = 1.;
458   Float_t timeWalk[4]  = {0.,0.,0.,0.};
459   Float_t timeDelay[4] = {0.,0.,0.,0.};
460   
461   nActivatedPads = 0;
462   nFiredPads = 0;
463   
464   (z0 <= 0) ? iz = 0 : iz = 1;
465   dZ = z0 + (0.5 * AliTOFConstants::fgkNpadZ - iz - 0.5) * AliTOFConstants::fgkZPad; // hit position in the pad frame, (0,0) - center of the pad
466   z = 0.5 * AliTOFConstants::fgkZPad - TMath::Abs(dZ);                               // variable for eff., res. and timeWalk. functions
467   iz++;                                                                              // z row: 1, ..., AliTOFConstants::fgkNpadZ = 2
468   ix = (Int_t)((x0 + 0.5 * AliTOFConstants::fgkNpadX * AliTOFConstants::fgkXPad) / AliTOFConstants::fgkXPad);
469   dX = x0 + (0.5 * AliTOFConstants::fgkNpadX - ix - 0.5) * AliTOFConstants::fgkXPad; // hit position in the pad frame, (0,0) - center of the pad
470   x = 0.5 * AliTOFConstants::fgkXPad - TMath::Abs(dX);                               // variable for eff., res. and timeWalk. functions;
471   ix++;                                                                              // x row: 1, ..., AliTOFConstants::fgkNpadX = 48
472   
473   ////// Pad A:
474   nActivatedPads++;
475   nPlace[nActivatedPads-1] = (iz - 1) * AliTOFConstants::fgkNpadX + ix;
476   qInduced[nActivatedPads-1] = qCenterPad;
477   padId[nActivatedPads-1] = 1;
478   
479   if (fEdgeEffect == 0) {
480     eff[nActivatedPads-1] = fEffCenter;
481     if (gRandom->Rndm() < eff[nActivatedPads-1]) {
482       nFiredPads = 1;
483       res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + fResCenter * fResCenter); // 10400=30^2+20^2+40^2+50^2+50^2+50^2  ns;
484       isFired[nActivatedPads-1] = kTRUE;
485       tofTime[nActivatedPads-1] = gRandom->Gaus(geantTime + fTimeWalkCenter, res[0]);
486       averageTime = tofTime[nActivatedPads-1];
487     }
488   } else {
489      
490     if(z < h) {
491       if(z < h2) {
492         effZ = fEffBoundary + (fEff2Boundary - fEffBoundary) * z / h2;
493       } else {
494         effZ = fEff2Boundary + (fEffCenter - fEff2Boundary) * (z - h2) / (h - h2);
495       }
496       resZ = fResBoundary + (fResCenter - fResBoundary) * z / h;
497       timeWalkZ = fTimeWalkBoundary + (fTimeWalkCenter - fTimeWalkBoundary) * z / h;
498       nTail[nActivatedPads-1] = 1;
499     } else {
500       effZ = fEffCenter;
501       resZ = fResCenter;
502       timeWalkZ = fTimeWalkCenter;
503     }
504     
505     if(x < h) {
506       if(x < h2) {
507         effX = fEffBoundary + (fEff2Boundary - fEffBoundary) * x / h2;
508       } else {
509         effX = fEff2Boundary + (fEffCenter - fEff2Boundary) * (x - h2) / (h - h2);
510       }
511       resX = fResBoundary + (fResCenter - fResBoundary) * x / h;
512       timeWalkX = fTimeWalkBoundary + (fTimeWalkCenter - fTimeWalkBoundary) * x / h;
513       nTail[nActivatedPads-1] = 1;
514     } else {
515       effX = fEffCenter;
516       resX = fResCenter;
517       timeWalkX = fTimeWalkCenter;
518     }
519     
520     (effZ<effX) ? eff[nActivatedPads-1] = effZ : eff[nActivatedPads-1] = effX;
521     (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2  ns
522     (timeWalkZ<timeWalkX) ? timeWalk[nActivatedPads-1] = 0.001 *  timeWalkZ : timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
523
524
525     ////// Pad B:
526     if(z < k2) {
527       effZ = fEffBoundary - (fEffBoundary - fEff3Boundary) * (z / k2);
528     } else {
529       effZ = fEff3Boundary * (k - z) / (k - k2);
530     }
531     resZ = fResBoundary + fResSlope * z / k;
532     timeWalkZ = fTimeWalkBoundary + fTimeWalkSlope * z / k;
533     
534     if(z < k && z > 0) {
535       if( (iz == 1 && dZ > 0) || (iz == 2 && dZ < 0) ) {
536         nActivatedPads++;
537         nPlace[nActivatedPads-1] = nPlace[0] + (3 - 2 * iz) * AliTOFConstants::fgkNpadX;
538         eff[nActivatedPads-1] = effZ;
539         res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns 
540         timeWalk[nActivatedPads-1] = 0.001 * timeWalkZ; // ns
541         nTail[nActivatedPads-1] = 2;
542         if (fTimeDelayFlag) {
543           //      qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * z / 2.);
544           //      qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * z / 2.);
545           qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * z);
546           logOfqInd = gRandom->Gaus(-fPulseHeightSlope * z, fLogChargeSmearing);
547           timeDelay[nActivatedPads-1] = gRandom->Gaus(-fTimeDelaySlope * logOfqInd, fTimeSmearing);
548         } else {
549           timeDelay[nActivatedPads-1] = 0.;
550         }
551         padId[nActivatedPads-1] = 2;
552       }
553     }
554
555     
556     ////// Pad C, D, E, F:
557     if(x < k2) {
558       effX = fEffBoundary - (fEffBoundary - fEff3Boundary) * (x / k2);
559     } else {
560       effX = fEff3Boundary * (k - x) / (k - k2);
561     }
562     resX = fResBoundary + fResSlope*x/k;
563     timeWalkX = fTimeWalkBoundary + fTimeWalkSlope*x/k;
564     
565     if(x < k && x > 0) {
566       //   C:
567       if(ix > 1 && dX < 0) {
568         nActivatedPads++;
569         nPlace[nActivatedPads-1] = nPlace[0] - 1;
570         eff[nActivatedPads-1] = effX;
571         res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns 
572         timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
573         nTail[nActivatedPads-1] = 2;
574         if (fTimeDelayFlag) {
575           //      qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * x / 2.);
576           //      qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * x / 2.);
577           qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * x);
578           logOfqInd = gRandom->Gaus(-fPulseHeightSlope * x, fLogChargeSmearing);
579           timeDelay[nActivatedPads-1] = gRandom->Gaus(-fTimeDelaySlope * logOfqInd, fTimeSmearing);
580         } else {
581           timeDelay[nActivatedPads-1] = 0.;
582         }
583         padId[nActivatedPads-1] = 3;
584
585         //     D:
586         if(z < k && z > 0) {
587           if( (iz == 1 && dZ > 0) || (iz == 2 && dZ < 0) ) {
588             nActivatedPads++;
589             nPlace[nActivatedPads-1] = nPlace[0] + (3 - 2 * iz) * AliTOFConstants::fgkNpadX - 1;
590             eff[nActivatedPads-1] = effX * effZ;
591             (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns
592             (timeWalkZ<timeWalkX) ? timeWalk[nActivatedPads-1] = 0.001 * timeWalkZ : timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
593             
594             nTail[nActivatedPads-1] = 2;
595             if (fTimeDelayFlag) {
596               if (TMath::Abs(x) < TMath::Abs(z)) {
597                 //              qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * z / 2.);
598                 //              qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * z / 2.);
599                 qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * z);
600                 logOfqInd = gRandom->Gaus(-fPulseHeightSlope * z, fLogChargeSmearing);
601               } else {
602                 //              qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * x / 2.);
603                 //              qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * x / 2.);
604                 qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * x);
605                 logOfqInd = gRandom->Gaus(-fPulseHeightSlope * x, fLogChargeSmearing);
606               }
607               timeDelay[nActivatedPads-1] = gRandom->Gaus(-fTimeDelaySlope * logOfqInd, fTimeSmearing);
608             } else {
609               timeDelay[nActivatedPads-1] = 0.;
610             }
611             padId[nActivatedPads-1] = 4;
612           }
613         }  // end D
614       }  // end C
615       
616       //   E:
617       if(ix < AliTOFConstants::fgkNpadX && dX > 0) {
618         nActivatedPads++;
619         nPlace[nActivatedPads-1] = nPlace[0] + 1;
620         eff[nActivatedPads-1] = effX;
621         res[nActivatedPads-1] = 0.001 * (TMath::Sqrt(10400 + resX * resX)); // ns
622         timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
623         nTail[nActivatedPads-1] = 2;
624         if (fTimeDelayFlag) {
625           //      qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * x / 2.);
626           //      qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * x / 2.);
627           qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * x);
628           logOfqInd = gRandom->Gaus(-fPulseHeightSlope * x, fLogChargeSmearing);
629           timeDelay[nActivatedPads-1] = gRandom->Gaus(-fTimeDelaySlope * logOfqInd, fTimeSmearing);
630         } else {
631           timeDelay[nActivatedPads-1] = 0.;
632         }
633         padId[nActivatedPads-1] = 5;
634
635
636         //     F:
637         if(z < k && z > 0) {
638           if( (iz == 1 && dZ > 0) || (iz == 2 && dZ < 0) ) {
639             nActivatedPads++;
640             nPlace[nActivatedPads - 1] = nPlace[0] + (3 - 2 * iz) * AliTOFConstants::fgkNpadX + 1;
641             eff[nActivatedPads - 1] = effX * effZ;
642             (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns
643             (timeWalkZ<timeWalkX) ? timeWalk[nActivatedPads-1] = 0.001 * timeWalkZ : timeWalk[nActivatedPads-1] = 0.001*timeWalkX; // ns
644             nTail[nActivatedPads-1] = 2;
645             if (fTimeDelayFlag) {
646               if (TMath::Abs(x) < TMath::Abs(z)) {
647                 //              qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * z / 2.);
648                 //              qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * z / 2.);
649                 qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * z);
650                 logOfqInd = gRandom->Gaus(-fPulseHeightSlope * z, fLogChargeSmearing);
651               } else {
652                 //              qInduced[0] = fMinimumCharge * TMath::Exp(fPulseHeightSlope * x / 2.);
653                 //              qInduced[nActivatedPads-1] = fMinimumCharge * TMath::Exp(-fPulseHeightSlope * x / 2.);
654                 qInduced[nActivatedPads-1] = TMath::Exp(-fPulseHeightSlope * x);
655                 logOfqInd = gRandom->Gaus(-fPulseHeightSlope * x, fLogChargeSmearing);
656               }
657               timeDelay[nActivatedPads-1] = gRandom->Gaus(-fTimeDelaySlope * logOfqInd, fTimeSmearing);
658             } else {
659               timeDelay[nActivatedPads-1] = 0.;
660             }
661             padId[nActivatedPads-1] = 6;
662           }
663         }  // end F
664       }  // end E
665     } // end if(x < k)
666
667
668     for (Int_t iPad = 0; iPad < nActivatedPads; iPad++) {
669       if (res[iPad] < fTimeResolution) res[iPad] = fTimeResolution;
670       if(gRandom->Rndm() < eff[iPad]) {
671         isFired[iPad] = kTRUE;
672         nFiredPads++;
673         if(fEdgeTails) {
674           if(nTail[iPad] == 0) {
675             tofTime[iPad] = gRandom->Gaus(geantTime + timeWalk[iPad] + timeDelay[iPad], res[iPad]);
676           } else {
677             ftail->SetParameters(res[iPad], 2. * res[iPad], kSigmaForTail[nTail[iPad]-1]);
678             Double_t timeAB = ftail->GetRandom();
679             tofTime[iPad] = geantTime + timeWalk[iPad] + timeDelay[iPad] + timeAB;
680           }
681         } else {
682           tofTime[iPad] = gRandom->Gaus(geantTime + timeWalk[iPad] + timeDelay[iPad], res[iPad]);
683         }
684         if (fAverageTimeFlag) {
685           averageTime += tofTime[iPad] * qInduced[iPad];
686           weightsSum += qInduced[iPad];
687         } else {
688           averageTime += tofTime[iPad];
689           weightsSum += 1.;
690         }
691       }
692     }
693     if (weightsSum!=0) averageTime /= weightsSum;
694   } // end else (fEdgeEffect != 0)
695 }
696
697 //__________________________________________________________________
698 void AliTOFSDigitizer::PrintParameters()const
699 {
700   //
701   // Print parameters used for sdigitization
702   //
703   cout << " ------------------- "<< GetName() << " -------------" << endl ;
704   cout << " Parameters used for TOF SDigitization " << endl ;
705   //  Printing the parameters
706   
707   cout << " Number of events:                        " << (fEvent2-fEvent1) << endl; 
708   cout << " from event " << fEvent1 << " to event " << (fEvent2-1) << endl; 
709   cout << " Time Resolution (ns) "<< fTimeResolution <<" Pad Efficiency: "<< fpadefficiency << endl;
710   cout << " Edge Effect option:  "<<  fEdgeEffect<< endl;
711
712   cout << " Boundary Effect Simulation Parameters " << endl;
713   cout << " Hparameter: "<< fHparameter<<"  H2parameter:"<< fH2parameter <<"  Kparameter:"<< fKparameter<<"  K2parameter: "<< fK2parameter << endl;
714   cout << " Efficiency in the central region of the pad: "<< fEffCenter << endl;
715   cout << " Efficiency at the boundary region of the pad: "<< fEffBoundary << endl;
716   cout << " Efficiency value at H2parameter "<< fEff2Boundary << endl;
717   cout << " Efficiency value at K2parameter "<< fEff3Boundary << endl;
718   cout << " Resolution (ps) in the central region of the pad: "<< fResCenter << endl;
719   cout << " Resolution (ps) at the boundary of the pad      : "<< fResBoundary << endl;
720   cout << " Slope (ps/K) for neighbouring pad               : "<< fResSlope <<endl;
721   cout << " Time walk (ps) in the central region of the pad : "<< fTimeWalkCenter << endl;
722   cout << " Time walk (ps) at the boundary of the pad       : "<< fTimeWalkBoundary<< endl;
723   cout << " Slope (ps/K) for neighbouring pad               : "<< fTimeWalkSlope<<endl;
724   cout << " Pulse Heigth Simulation Parameters " << endl;
725   cout << " Flag for delay due to the PulseHeightEffect: "<< fTimeDelayFlag <<endl;
726   cout << " Pulse Height Slope                           : "<< fPulseHeightSlope<<endl;
727   cout << " Time Delay Slope                             : "<< fTimeDelaySlope<<endl;
728   cout << " Minimum charge amount which could be induced : "<< fMinimumCharge<<endl;
729   cout << " Smearing in charge in (q1/q2) vs x plot      : "<< fChargeSmearing<<endl;
730   cout << " Smearing in log of charge ratio              : "<< fLogChargeSmearing<<endl;
731   cout << " Smearing in time in time vs log(q1/q2) plot  : "<< fTimeSmearing<<endl;
732   cout << " Flag for average time                        : "<< fAverageTimeFlag<<endl;
733   cout << " Edge tails option                            : "<< fEdgeTails << endl;
734   
735 }