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