]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFDigitizer.cxx
Protection against wrong trigger bc mask pattern
[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(new TClonesArray("AliTOFdigit",4000)),
65     fSDigitsArray(new TClonesArray("AliTOFSDigit",1000)),
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(new TClonesArray("AliTOFdigit",4000)),
77   fSDigitsArray(new TClonesArray("AliTOFSDigit",1000)),
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   if (fDigits){
114     fDigits->Delete();
115     delete fDigits;
116     fDigits=0x0;
117   }
118   if (fSDigitsArray){
119     fSDigitsArray->Delete();
120     delete fSDigitsArray;
121     fSDigitsArray=0x0;
122   }
123 }
124
125 //---------------------------------------------------------------------
126
127 void AliTOFDigitizer::Exec(Option_t* /*option*/)
128 {
129   //
130   // Perform digitization and merging.
131   // The algorithm is the following:
132   // - a hitmap is created to check if a pad is already activated;
133   // - an sdigits container is created to collect all sdigits from
134   //   different files;
135   // - sdigits are summed using the hitmap;
136   // - the sdigits container is used to create the array of AliTOFdigit.
137   //
138
139   AliDebug(1, "");
140
141
142   // get the ptr to TOF detector
143   AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
144
145   //Make branches
146   char branchname[20];
147   sprintf (branchname, "%s", tof->GetName ());
148  
149   AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
150   if (outrl == 0x0)
151    {
152      AliError("Can not find Run Loader in output folder.");
153      return;
154    }
155    
156   /*
157   outrl->CdGAFile();
158   TFile *in=(TFile*)gFile;
159   TDirectory *savedir=gDirectory;
160
161    
162   //when fGeom was needed
163
164   if (!in->IsOpen()) {
165     AliWarning("Geometry file is not open default  TOF geometry will be used");
166     fGeom = new AliTOFGeometry();
167   }
168   else {
169     in->cd();
170     fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
171   }
172   
173   savedir->cd();
174   */
175   AliLoader* outgime = outrl->GetLoader("TOFLoader");
176   if (outgime == 0x0)
177    {
178      AliError("Can not get TOF Loader from Output Run Loader.");
179      return;
180    }
181   
182   TTree* treeD = outgime->TreeD();
183   if (treeD == 0x0)
184    {
185      outgime->MakeTree("D");
186      treeD = outgime->TreeD();
187    }
188   //Make branch for digits (to be created in Init())
189   tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
190
191   // container for all summed sdigits (to be created in Init())
192   //fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
193   
194   // create hit map (to be created in Init())
195   fhitMap = new AliTOFHitMap(fSDigitsArray);
196   
197   // Loop over files to digitize
198
199   for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
200        inputFile++) {
201     ReadSDigit(inputFile);
202    }
203
204   // create digits
205   CreateDigits();
206
207   // free used memory for Hit Map in current event
208   delete fhitMap;
209   fSDigitsArray->Clear();
210
211   treeD->Fill();
212  
213   outgime->WriteDigits("OVERWRITE");
214   outgime->UnloadDigits();
215   fDigits->Clear();
216
217 }
218
219 //---------------------------------------------------------------------
220
221 void AliTOFDigitizer::CreateDigits()
222 {
223   // loop on sdigits container to fill the AliTOFdigit TClonesArray
224   // start digitizing all the collected sdigits 
225
226   Int_t ndump=0; // dump the first ndump created digits for each event
227
228   // get the total number of collected sdigits
229   Int_t ndig = fSDigitsArray->GetEntriesFast();
230
231   for (Int_t k = 0; k < ndig; k++) {
232     
233     Int_t  vol[5];  // location for a digit
234     for (Int_t i=0; i<5; i++) vol[i] = -1;
235     
236     // Get the information for this digit
237     AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
238     
239     Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
240     // for current sdigit
241     
242     // TOF sdigit volumes (always the same for all slots)
243     Int_t sector    = tofsdigit->GetSector(); // range [0-17]
244     Int_t plate     = tofsdigit->GetPlate();  // range [0- 4]
245     Int_t strip     = tofsdigit->GetStrip();  // range [0-14/18/19]
246     Int_t padz      = tofsdigit->GetPadz();   // range [0- 1]
247     Int_t padx      = tofsdigit->GetPadx();   // range [0-47]
248     
249     vol[0] = sector;
250     vol[1] = plate;
251     vol[2] = strip;
252     vol[3] = padx;
253     vol[4] = padz;
254     
255     //--------------------- QA section ----------------------
256     // in the while, I perform QA
257     Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
258     
259     if (isSDigitBad) {
260       //AliFatal("strange sdigit found");
261       AliFatal(Form("strange sdigit found   %3i  %2i  %2i  %3i    %3i", sector, plate, padz, padx, strip));
262     }
263     //-------------------------------------------------------
264     
265     //------------------- Dump section ----------------------
266     if(k<ndump){
267       AliInfo(Form("%2i-th | Sector %2i | Plate %1i | Strip %2i | PadZ %1i | PadX %2i ", k, sector, plate, strip, padz, padx));
268       AliInfo(Form("%2i-th", k));
269       AliInfo("----------------------------------------------------");
270     }
271     // ------------------------------------------------------
272     
273     // start loop on number of slots for current sdigit
274     for (Int_t islot = 0; islot < nslot; islot++) {
275       Int_t  digit[4] = {-1,-1,-1,-1};     // TOF digit variables
276       Int_t tracknum[AliTOFSDigit::kMAXDIGITS];     // contributing tracks for the current slot
277       
278       Int_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
279       Int_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
280
281       //if (tdc>=8192) continue;//AdC
282
283       tracknum[0]=tofsdigit->GetTrack(islot,0);
284       tracknum[1]=tofsdigit->GetTrack(islot,1);
285       tracknum[2]=tofsdigit->GetTrack(islot,2);
286       
287       // new with placement must be used
288       // adding a TOF digit for each slot
289       TClonesArray &aDigits = *fDigits;
290       Int_t last=fDigits->GetEntriesFast();
291       new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
292
293     }
294     
295   } // end loop on sdigits - end digitizing all collected sdigits
296
297   //Insert Decalibration 
298   AliInfo("in digitizer, create digits");
299   DecalibrateTOFSignal();
300 }
301
302 //---------------------------------------------------------------------
303
304 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
305 {
306   // Read sdigits for current event and inputFile; 
307   // store them into the sdigits container
308   // and update the hit map
309   // SDigits from different files are assumed to
310   // be created with the same simulation parameters.
311   
312   // creating the TClonesArray to store the digits
313   static TClonesArray sdigitsClonesArray("AliTOFSDigit",  1000); 
314   sdigitsClonesArray.Clear();
315
316   // get the treeS from manager
317   AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
318   if (rl == 0x0)
319    {
320      AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
321      return;
322    }
323
324   AliLoader* gime = rl->GetLoader("TOFLoader");
325   if (gime == 0x0)
326    {
327      AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
328      return;
329    }
330
331   TTree* currentTreeS=gime->TreeS();
332   if (currentTreeS == 0x0)
333    {
334      Int_t retval = gime->LoadSDigits();
335      if (retval) 
336       {
337          AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
338          return;
339       }
340      currentTreeS=gime->TreeS();
341      if (currentTreeS == 0x0)
342       {
343          AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
344          return;
345       }
346    } 
347   // get the branch TOF inside the treeS
348   TClonesArray * sdigitsDummyContainer=&sdigitsClonesArray;
349   // check if the branch exist
350   TBranch* tofBranch=currentTreeS->GetBranch("TOF");
351
352   if(!tofBranch){
353     AliFatal(Form("TOF branch not found for input %d",inputFile));
354   }
355   
356   tofBranch->SetAddress(&sdigitsDummyContainer);           
357   
358   Int_t nEntries = (Int_t)tofBranch->GetEntries();                                
359
360   // Loop through all entries in the tree
361   Int_t nbytes = 0;
362   
363   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
364     
365     // Import the tree
366     nbytes += tofBranch->GetEvent(iEntry);
367     
368     // Get the number of sdigits
369     Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
370     
371     for (Int_t k=0; k<ndig; k++) {
372       AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
373       
374       Int_t  vol[5]; // location for a sdigit
375       for (Int_t i=0; i<5; i++) vol[i] = -1;
376
377       // check the sdigit volume
378       vol[0] = tofSdigit->GetSector();
379       vol[1] = tofSdigit->GetPlate();
380       vol[2] = tofSdigit->GetStrip();
381       vol[3] = tofSdigit->GetPadx();
382       vol[4] = tofSdigit->GetPadz();
383       
384       if (fhitMap->TestHit(vol) != kEmpty) {
385         AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
386         sdig->Update(tofSdigit);
387
388       } else {
389
390         CollectSDigit(tofSdigit); // collect the current sdigit
391         fhitMap->SetHit(vol);     // update the hitmap for location vol
392
393       } // if (hitMap->TestHit(vol) != kEmpty)
394       
395     } // for (Int_t k=0; k<ndig; k++)
396
397   } // end loop on entries
398
399 }
400
401
402 //_____________________________________________________________________________
403 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
404 {
405   //
406   // Add a TOF sdigit in container
407   // new with placement must be used
408   TClonesArray &aSDigitsArray = *fSDigitsArray;
409   Int_t last=fSDigitsArray->GetEntriesFast();
410   // make a copy of the current sdigit and
411   // put it into tmp array
412   new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
413 }
414
415 //_____________________________________________________________________________
416 void AliTOFDigitizer::InitDecalibration() const {
417   //
418   //
419   //
420
421   fCalib->CreateCalArrays();
422   fCalib->ReadSimHistoFromCDB("TOF/Calib", -1); // use AliCDBManager's number
423   fCalib->ReadParOfflineFromCDB("TOF/Calib", -1); // use AliCDBManager's number
424 }
425 //---------------------------------------------------------------------
426 void AliTOFDigitizer::DecalibrateTOFSignal(){
427
428   // Read Calibration parameters from the CDB
429
430   TObjArray * calOffline= fCalib->GetTOFCalArrayOffline();
431
432   AliDebug(2,Form("Size of array for Offline Calibration = %i",calOffline->GetEntries()));
433
434   // Initialize Quantities to Simulate ToT Spectra
435
436   TH1F * hToT= fCalib->GetTOFSimToT();
437   Int_t nbins = hToT->GetNbinsX();
438   Float_t delta = hToT->GetBinWidth(1);
439   Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
440   Float_t minch = hToT->GetBinLowEdge(1);
441   Float_t max=0,min=0; //maximum and minimum value of the distribution
442   Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
443
444   for (Int_t ii=nbins; ii>0; ii--){
445     if (hToT->GetBinContent(ii)!= 0) {
446       max = maxch - (nbins-ii-1)*delta;
447       maxbin = ii; 
448       break;}
449   }
450   for (Int_t j=1; j<nbins; j++){
451     if (hToT->GetBinContent(j)!= 0) {
452       min = minch + (j-1)*delta;
453       minbin = j; 
454       break;}
455   }
456
457   Float_t maxToT=max;
458   Float_t minToT=min;
459  
460   Float_t maxToTDistr=hToT->GetMaximum();
461
462   AliDebug (1, Form(" The minimum ToT = %f", minToT)); 
463   AliDebug (1, Form(" The maximum ToT = %f", maxToT)); 
464   AliDebug (1, Form(" The maximum peak in ToT = %f", maxToTDistr)); 
465   
466   // Loop on TOF Digits
467
468   Bool_t isToTSimulated=kFALSE;
469   Bool_t misCalibPars=kFALSE;
470   if(hToT->GetEntries()>0)isToTSimulated=kTRUE;  
471   Int_t ndigits = fDigits->GetEntriesFast();
472   for (Int_t i=0;i<ndigits;i++){
473     AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
474     Int_t detId[5];
475     detId[0] = dig->GetSector();
476     detId[1] = dig->GetPlate();
477     detId[2] = dig->GetStrip();
478     detId[3] = dig->GetPadz();
479     detId[4] = dig->GetPadx();
480     dig->SetTdcND(dig->GetTdc()); // save the non decalibrated time
481     if(isToTSimulated){  
482
483       //A realistic ToT Spectrum was found in input, 
484       //decalibrated TOF Digits likely to be simulated....
485  
486       Int_t index = AliTOFGeometry::GetIndex(detId); // The channel index    
487       AliTOFChannelOffline *calChannelOffline = (AliTOFChannelOffline *)calOffline->At(index); //retrieve the info for time slewing 
488       Double_t par[6];  // time slewing parameters
489   
490       //check whether we actually ask for miscalibration
491
492       for (Int_t j = 0; j<6; j++){
493         par[j]=(Double_t)calChannelOffline->GetSlewPar(j);
494         if(par[j]!=0)misCalibPars=kTRUE;
495       }
496       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]));
497
498       // Now generate Realistic ToT distribution from TestBeam Data. 
499       // Tot is in ns, assuming a Matching Window of 10 ns.
500
501       Float_t simToT = 0;
502       Float_t trix = 0;
503       Float_t triy = 0;
504       Double_t timeCorr;
505       Double_t tToT;
506       while (simToT <= triy){
507         trix = gRandom->Rndm(i);
508         triy = gRandom->Rndm(i);
509         trix = (maxToT-minToT)*trix + minToT; 
510         triy = maxToTDistr*triy;
511         Int_t binx=hToT->FindBin(trix);
512         simToT=hToT->GetBinContent(binx);
513       }
514       // the generated ToT (ns)
515       tToT= (Double_t) trix; // to apply slewing we start from ns..
516       // transform TOF signal in ns
517       AliDebug(2,Form(" The Initial Time (counts): %i: ",dig->GetTdc()));
518       AliDebug(2,Form(" Time before miscalibration (ps) %e: ",dig->GetTdc()*(Double_t)AliTOFGeometry::TdcBinWidth()));
519       // add slewing effect
520       timeCorr=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5])))); 
521       AliDebug(2,Form(" The Time slewing + delay (ns): %f: ",timeCorr));
522       // add global time shift
523       //convert to ps
524       timeCorr*=1E3;
525       Double_t timeMis = (Double_t)(dig->GetTdc())*(Double_t)AliTOFGeometry::TdcBinWidth();
526       timeMis = timeMis+timeCorr;
527       AliDebug(2,Form(" The Miscalibrated time (ps): %e: ",timeMis));
528
529       // now update the digit info
530  
531       Int_t tdcCorr= (Int_t)(timeMis/AliTOFGeometry::TdcBinWidth());
532       AliDebug(2,Form(" Final Time (counts): %i: ",tdcCorr));
533       // Setting Decalibrated Time signal (TDC counts)    
534       dig->SetTdc(tdcCorr);   
535       // Setting realistic ToT signal (TDC counts) 
536       tToT*=1E3; //back to ps  
537       Int_t tot=(Int_t)(tToT/AliTOFGeometry::ToTBinWidth());//(factor 1E3 as input ToT is in ns)
538       dig->SetToT(tot); 
539       AliDebug(2,Form(" Final Time and ToT (counts): %i: , %i:",dig->GetTdc(),dig->GetToT()));
540       if(tdcCorr<0){
541         AliWarning (Form(" The bad Slewed Time(TDC counts)= %i ", tdcCorr)); 
542         AliWarning(Form(" The bad ToT (TDC counts)= %i ", tot)); 
543       }
544     }
545     else{
546     // For Data with no Miscalibration, set ToT signal == Adc
547       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)
548     }
549   }
550
551   if(!isToTSimulated){
552     AliDebug(1,"Standard Production, no miscalibrated digits");   
553   }else{
554     if(!misCalibPars){
555     AliDebug(1,"Standard Production, no miscalibrated digits");   
556     }
557     else {
558       AliDebug(1,"Simulating miscalibrated digits");   
559     } 
560   }
561   return;
562 }
563