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