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