]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFDigitizer.cxx
Added the w/o holes switch in AliTOF class: in this way, TOF will be indipendent...
[u/mrichter/AliRoot.git] / TOF / AliTOFDigitizer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2000, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 //_________________________________________________________________________//
17 //                                                                         //
18 // This is a TTask that makes TOF-Digits out of TOF-SDigits.               //
19 // The simulation of the detector is performed at sdigits level:           //
20 // during digitization the unique task is the sum of all sdigits in the    //
21 // same pad.                                                               //
22 // Digits are written to TreeD in branch "TOF".                            //
23 //                                                                         //
24 // -- Author :  F. Pierella (Bologna University) pierella@bo.infn.it       //
25 //                                                                         //
26 //_________________________________________________________________________//
27
28 #include "Riostream.h"
29
30 #include "TFile.h"
31 #include "TH1F.h"
32 #include "TTree.h"
33 #include "TRandom.h"
34 #include "TObjArray.h"
35
36 #include "AliLoader.h"
37 #include "AliLog.h"
38 #include "AliRunDigitizer.h"
39 #include "AliRunLoader.h"
40 #include "AliRun.h"
41
42 #include "AliTOFcalib.h"
43 #include "AliTOFChannelOnline.h"
44 #include "AliTOFChannelOffline.h"
45 #include "AliTOFDigitizer.h"
46 #include "AliTOFdigit.h"
47 #include "AliTOFHitMap.h"
48 #include "AliTOFGeometry.h"
49 #include "AliTOFSDigit.h"
50 #include "AliTOF.h"
51
52 extern TDirectory *gDirectory;
53 extern TFile *gFile;
54 extern TRandom *gRandom;
55
56 extern AliRun *gAlice;
57
58
59 ClassImp(AliTOFDigitizer)
60
61 //___________________________________________
62   AliTOFDigitizer::AliTOFDigitizer()  :
63     AliDigitizer(),
64     fDigits(0x0),
65     fSDigitsArray(0x0),
66   fhitMap(0x0),
67   fCalib(new AliTOFcalib())
68 {
69   // Default ctor - don't use it
70   InitDecalibration();
71 }
72
73 //___________________________________________
74 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager): 
75   AliDigitizer(manager), 
76   fDigits(0x0),
77   fSDigitsArray(0x0),
78   fhitMap(0x0),
79   fCalib(new AliTOFcalib())
80 {
81   //ctor with RunDigitizer
82   InitDecalibration();
83 }
84
85 //------------------------------------------------------------------------
86 AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
87   AliDigitizer(source),
88   fDigits(source.fDigits),
89   fSDigitsArray(source.fSDigitsArray),
90   fhitMap(source.fhitMap),
91   fCalib(source.fCalib)
92 {
93   // copy constructor
94 }
95
96 //------------------------------------------------------------------------
97   AliTOFDigitizer& AliTOFDigitizer::operator=(const AliTOFDigitizer &source)
98 {
99   // ass. op.
100   this->fDigits=source.fDigits;
101   this->fSDigitsArray=source.fSDigitsArray;
102   this->fhitMap=source.fhitMap;
103   this->fCalib=source.fCalib;
104   return *this;
105
106 }
107
108 //------------------------------------------------------------------------
109 AliTOFDigitizer::~AliTOFDigitizer()
110 {
111   // Destructor
112   delete fCalib;
113 }
114
115 //---------------------------------------------------------------------
116
117 void AliTOFDigitizer::Exec(Option_t* /*option*/)
118 {
119   //
120   // Perform digitization and merging.
121   // The algorithm is the following:
122   // - a hitmap is created to check if a pad is already activated;
123   // - an sdigits container is created to collect all sdigits from
124   //   different files;
125   // - sdigits are summed using the hitmap;
126   // - the sdigits container is used to create the array of AliTOFdigit.
127   //
128
129   AliDebug(1, "");
130
131
132   // get the ptr to TOF detector
133   AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
134
135   //Make branches
136   char branchname[20];
137   sprintf (branchname, "%s", tof->GetName ());
138
139   fDigits=new TClonesArray("AliTOFdigit",4000);
140  
141   AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
142   if (outrl == 0x0)
143    {
144      AliError("Can not find Run Loader in output folder.");
145      return;
146    }
147    
148   /*
149   outrl->CdGAFile();
150   TFile *in=(TFile*)gFile;
151   TDirectory *savedir=gDirectory;
152
153    
154   //when fGeom was needed
155
156   if (!in->IsOpen()) {
157     AliWarning("Geometry file is not open default  TOF geometry will be used");
158     fGeom = new AliTOFGeometry();
159   }
160   else {
161     in->cd();
162     fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
163   }
164   
165   savedir->cd();
166   */
167   AliLoader* outgime = outrl->GetLoader("TOFLoader");
168   if (outgime == 0x0)
169    {
170      AliError("Can not get TOF Loader from Output Run Loader.");
171      return;
172    }
173   
174   TTree* treeD = outgime->TreeD();
175   if (treeD == 0x0)
176    {
177      outgime->MakeTree("D");
178      treeD = outgime->TreeD();
179    }
180   //Make branch for digits (to be created in Init())
181   tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
182
183   // container for all summed sdigits (to be created in Init())
184   fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
185   
186   // create hit map (to be created in Init())
187   fhitMap = new AliTOFHitMap(fSDigitsArray);
188   
189   // Loop over files to digitize
190
191   for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
192        inputFile++) {
193     ReadSDigit(inputFile);
194    }
195
196   // create digits
197   CreateDigits();
198
199   // free used memory for Hit Map in current event
200   delete fhitMap;
201   fSDigitsArray->Delete();
202   delete fSDigitsArray;
203
204   treeD->Fill();
205  
206   outgime->WriteDigits("OVERWRITE");
207   outgime->UnloadDigits();
208   fDigits->Delete();
209   delete fDigits;
210
211 }
212
213 //---------------------------------------------------------------------
214
215 void AliTOFDigitizer::CreateDigits()
216 {
217   // loop on sdigits container to fill the AliTOFdigit TClonesArray
218   // start digitizing all the collected sdigits 
219
220   Int_t ndump=0; // dump the first ndump created digits for each event
221
222   // get the total number of collected sdigits
223   Int_t ndig = fSDigitsArray->GetEntriesFast();
224
225   for (Int_t k = 0; k < ndig; k++) {
226     
227     Int_t  vol[5];  // location for a digit
228     for (Int_t i=0; i<5; i++) vol[i] = -1;
229     
230     // Get the information for this digit
231     AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
232     
233     Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
234     // for current sdigit
235     
236     // TOF sdigit volumes (always the same for all slots)
237     Int_t sector    = tofsdigit->GetSector(); // range [0-17]
238     Int_t plate     = tofsdigit->GetPlate();  // range [0- 4]
239     Int_t strip     = tofsdigit->GetStrip();  // range [0-14/18/19]
240     Int_t padz      = tofsdigit->GetPadz();   // range [0- 1]
241     Int_t padx      = tofsdigit->GetPadx();   // range [0-47]
242     
243     vol[0] = sector;
244     vol[1] = plate;
245     vol[2] = strip;
246     vol[3] = padx;
247     vol[4] = padz;
248     
249     //--------------------- QA section ----------------------
250     // in the while, I perform QA
251     Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
252     
253     if (isSDigitBad) {
254       //AliFatal("strange sdigit found");
255       AliFatal(Form("strange sdigit found   %3i  %2i  %2i  %3i    %3i", sector, plate, padz, padx, strip));
256     }
257     //-------------------------------------------------------
258     
259     //------------------- Dump section ----------------------
260     if(k<ndump){
261       AliInfo(Form("%2i-th | Sector %2i | Plate %1i | Strip %2i | PadZ %1i | PadX %2i ", k, sector, plate, strip, padz, padx));
262       AliInfo(Form("%2i-th", k));
263       AliInfo("----------------------------------------------------");
264     }
265     // ------------------------------------------------------
266     
267     // start loop on number of slots for current sdigit
268     for (Int_t islot = 0; islot < nslot; islot++) {
269       Int_t  digit[4] = {-1,-1,-1,-1};     // TOF digit variables
270       Int_t tracknum[AliTOFSDigit::kMAXDIGITS];     // contributing tracks for the current slot
271       
272       Int_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
273       Int_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
274       
275       tracknum[0]=tofsdigit->GetTrack(islot,0);
276       tracknum[1]=tofsdigit->GetTrack(islot,1);
277       tracknum[2]=tofsdigit->GetTrack(islot,2);
278       
279       // new with placement must be used
280       // adding a TOF digit for each slot
281       TClonesArray &aDigits = *fDigits;
282       Int_t last=fDigits->GetEntriesFast();
283       new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
284
285     }
286     
287   } // end loop on sdigits - end digitizing all collected sdigits
288
289   //Insert Decalibration 
290   AliInfo("in digitizer, create digits");
291   DecalibrateTOFSignal();
292 }
293
294 //---------------------------------------------------------------------
295
296 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
297 {
298   // Read sdigits for current event and inputFile; 
299   // store them into the sdigits container
300   // and update the hit map
301   // SDigits from different files are assumed to
302   // be created with the same simulation parameters.
303   
304   // get the treeS from manager
305   AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
306   if (rl == 0x0)
307    {
308      AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
309      return;
310    }
311
312   AliLoader* gime = rl->GetLoader("TOFLoader");
313   if (gime == 0x0)
314    {
315      AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
316      return;
317    }
318
319   TTree* currentTreeS=gime->TreeS();
320   if (currentTreeS == 0x0)
321    {
322      Int_t retval = gime->LoadSDigits();
323      if (retval) 
324       {
325          AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
326          return;
327       }
328      currentTreeS=gime->TreeS();
329      if (currentTreeS == 0x0)
330       {
331          AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
332          return;
333       }
334    } 
335   // get the branch TOF inside the treeS
336   TClonesArray * sdigitsDummyContainer= new TClonesArray("AliTOFSDigit",  1000); 
337
338   // check if the branch exist
339   TBranch* tofBranch=currentTreeS->GetBranch("TOF");
340
341   if(!tofBranch){
342     AliFatal(Form("TOF branch not found for input %d",inputFile));
343   }
344   
345   tofBranch->SetAddress(&sdigitsDummyContainer);           
346   
347   Int_t nEntries = (Int_t)tofBranch->GetEntries();                                
348
349   // Loop through all entries in the tree
350   Int_t nbytes = 0;
351   
352   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
353     
354     // Import the tree
355     nbytes += tofBranch->GetEvent(iEntry);
356     
357     // Get the number of sdigits
358     Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
359     
360     for (Int_t k=0; k<ndig; k++) {
361       AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
362       
363       Int_t  vol[5]; // location for a sdigit
364       for (Int_t i=0; i<5; i++) vol[i] = -1;
365
366       // check the sdigit volume
367       vol[0] = tofSdigit->GetSector();
368       vol[1] = tofSdigit->GetPlate();
369       vol[2] = tofSdigit->GetStrip();
370       vol[3] = tofSdigit->GetPadx();
371       vol[4] = tofSdigit->GetPadz();
372       
373       if (fhitMap->TestHit(vol) != kEmpty) {
374         AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
375         sdig->Update(tofSdigit);
376
377       } else {
378
379         CollectSDigit(tofSdigit); // collect the current sdigit
380         fhitMap->SetHit(vol);     // update the hitmap for location vol
381
382       } // if (hitMap->TestHit(vol) != kEmpty)
383       
384     } // for (Int_t k=0; k<ndig; k++)
385     sdigitsDummyContainer->Delete();
386
387   } // end loop on entries
388
389   delete sdigitsDummyContainer;
390
391 }
392
393
394 //_____________________________________________________________________________
395 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
396 {
397   //
398   // Add a TOF sdigit in container
399   // new with placement must be used
400   TClonesArray &aSDigitsArray = *fSDigitsArray;
401   Int_t last=fSDigitsArray->GetEntriesFast();
402   // make a copy of the current sdigit and
403   // put it into tmp array
404   new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
405 }
406
407 //_____________________________________________________________________________
408 void AliTOFDigitizer::InitDecalibration() const {
409   //
410   //
411   //
412
413   fCalib->CreateCalArrays();
414   fCalib->ReadSimHistoFromCDB("TOF/Calib", -1); // use AliCDBManager's number
415   fCalib->ReadParOfflineFromCDB("TOF/Calib", -1); // use AliCDBManager's number
416 }
417 //---------------------------------------------------------------------
418 void AliTOFDigitizer::DecalibrateTOFSignal(){
419
420   // Read Calibration parameters from the CDB
421
422   TObjArray * calOffline= fCalib->GetTOFCalArrayOffline();
423
424   AliDebug(2,Form("Size of array for Offline Calibration = %i",calOffline->GetEntries()));
425
426   // Initialize Quantities to Simulate ToT Spectra
427
428   TH1F * hToT= fCalib->GetTOFSimToT();
429   Int_t nbins = hToT->GetNbinsX();
430   Float_t delta = hToT->GetBinWidth(1);
431   Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
432   Float_t minch = hToT->GetBinLowEdge(1);
433   Float_t max=0,min=0; //maximum and minimum value of the distribution
434   Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
435
436   for (Int_t ii=nbins; ii>0; ii--){
437     if (hToT->GetBinContent(ii)!= 0) {
438       max = maxch - (nbins-ii-1)*delta;
439       maxbin = ii; 
440       break;}
441   }
442   for (Int_t j=1; j<nbins; j++){
443     if (hToT->GetBinContent(j)!= 0) {
444       min = minch + (j-1)*delta;
445       minbin = j; 
446       break;}
447   }
448
449   Float_t maxToT=max;
450   Float_t minToT=min;
451  
452   Float_t maxToTDistr=hToT->GetMaximum();
453
454   AliDebug (1, Form(" The minimum ToT = %f", minToT)); 
455   AliDebug (1, Form(" The maximum ToT = %f", maxToT)); 
456   AliDebug (1, Form(" The maximum peak in ToT = %f", maxToTDistr)); 
457   
458   // Loop on TOF Digits
459
460   Bool_t isToTSimulated=kFALSE;
461   Bool_t misCalibPars=kFALSE;
462   if(hToT->GetEntries()>0)isToTSimulated=kTRUE;  
463   Int_t ndigits = fDigits->GetEntriesFast();
464   for (Int_t i=0;i<ndigits;i++){
465     AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
466     Int_t detId[5];
467     detId[0] = dig->GetSector();
468     detId[1] = dig->GetPlate();
469     detId[2] = dig->GetStrip();
470     detId[3] = dig->GetPadz();
471     detId[4] = dig->GetPadx();
472     dig->SetTdcND(dig->GetTdc()); // save the non decalibrated time
473     if(isToTSimulated){  
474
475       //A realistic ToT Spectrum was found in input, 
476       //decalibrated TOF Digits likely to be simulated....
477  
478       Int_t index = AliTOFGeometry::GetIndex(detId); // The channel index    
479       AliTOFChannelOffline *calChannelOffline = (AliTOFChannelOffline *)calOffline->At(index); //retrieve the info for time slewing 
480       Double_t par[6];  // time slewing parameters
481   
482       //check whether we actually ask for miscalibration
483
484       for (Int_t j = 0; j<6; j++){
485         par[j]=(Double_t)calChannelOffline->GetSlewPar(j);
486         if(par[j]!=0)misCalibPars=kTRUE;
487       }
488       AliDebug(2,Form(" Calib Pars = %f (0-th parameter for time slewing + time delay), %f, %f, %f, %f, %f ",par[0],par[1],par[2],par[3],par[4],par[5]));
489
490       // Now generate Realistic ToT distribution from TestBeam Data. 
491       // Tot is in ns, assuming a Matching Window of 10 ns.
492
493       Float_t simToT = 0;
494       Float_t trix = 0;
495       Float_t triy = 0;
496       Double_t timeCorr;
497       Double_t tToT;
498       while (simToT <= triy){
499         trix = gRandom->Rndm(i);
500         triy = gRandom->Rndm(i);
501         trix = (maxToT-minToT)*trix + minToT; 
502         triy = maxToTDistr*triy;
503         Int_t binx=hToT->FindBin(trix);
504         simToT=hToT->GetBinContent(binx);
505       }
506       // the generated ToT (ns)
507       tToT= (Double_t) trix; // to apply slewing we start from ns..
508       // transform TOF signal in ns
509       AliDebug(2,Form(" The Initial Time (counts): %i: ",dig->GetTdc()));
510       AliDebug(2,Form(" Time before miscalibration (ps) %e: ",dig->GetTdc()*(Double_t)AliTOFGeometry::TdcBinWidth()));
511       // add slewing effect
512       timeCorr=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5])))); 
513       AliDebug(2,Form(" The Time slewing + delay (ns): %f: ",timeCorr));
514       // add global time shift
515       //convert to ps
516       timeCorr*=1E3;
517       Double_t timeMis = (Double_t)(dig->GetTdc())*(Double_t)AliTOFGeometry::TdcBinWidth();
518       timeMis = timeMis+timeCorr;
519       AliDebug(2,Form(" The Miscalibrated time (ps): %e: ",timeMis));
520
521       // now update the digit info
522  
523       Int_t tdcCorr= (Int_t)(timeMis/AliTOFGeometry::TdcBinWidth());
524       AliDebug(2,Form(" Final Time (counts): %i: ",tdcCorr));
525       // Setting Decalibrated Time signal (TDC counts)    
526       dig->SetTdc(tdcCorr);   
527       // Setting realistic ToT signal (TDC counts) 
528       tToT*=1E3; //back to ps  
529       Int_t tot=(Int_t)(tToT/AliTOFGeometry::ToTBinWidth());//(factor 1E3 as input ToT is in ns)
530       dig->SetToT(tot); 
531       AliDebug(2,Form(" Final Time and ToT (counts): %i: , %i:",dig->GetTdc(),dig->GetToT()));
532       if(tdcCorr<0){
533         AliWarning (Form(" The bad Slewed Time(TDC counts)= %i ", tdcCorr)); 
534         AliWarning(Form(" The bad ToT (TDC counts)= %i ", tot)); 
535       }
536     }
537     else{
538     // For Data with no Miscalibration, set ToT signal == Adc
539       dig->SetToT((Int_t)(dig->GetAdc()/AliTOFGeometry::ToTBinWidth())); //remove the factor 10^3 just to have a reasonable ToT range for raw data simulation even in the case of non-realistic ToT distribution (n.b. fAdc is practically an arbitrary quantity, and ToT has no impact on the TOF reco for non-miscalibrated digits)
540     }
541   }
542
543   if(!isToTSimulated){
544     AliDebug(1,"Standard Production, no miscalibrated digits");   
545   }else{
546     if(!misCalibPars){
547     AliDebug(1,"Standard Production, no miscalibrated digits");   
548     }
549     else {
550       AliDebug(1,"Simulating miscalibrated digits");   
551     } 
552   }
553   return;
554 }
555