]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFDigitizer.cxx
removal of effective c++ warnings (C.Zampolli)
[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
34 #include "AliLoader.h"
35 #include "AliLog.h"
36 #include "AliRunDigitizer.h"
37 #include "AliRunLoader.h"
38 #include "AliRun.h"
39
40 #include "AliTOFCal.h"
41 #include "AliTOFcalib.h"
42 #include "AliTOFChannel.h"
43 #include "AliTOFDigitizer.h"
44 #include "AliTOFdigit.h"
45 #include "AliTOFHitMap.h"
46 #include "AliTOFGeometryV5.h"
47 #include "AliTOFSDigit.h"
48 #include "AliTOF.h"
49
50 extern TDirectory *gDirectory;
51 extern TFile *gFile;
52 extern TRandom *gRandom;
53
54 extern AliRun *gAlice;
55
56
57 ClassImp(AliTOFDigitizer)
58
59 //___________________________________________
60   AliTOFDigitizer::AliTOFDigitizer()  :
61     AliDigitizer(),
62     fGeom(0x0),
63     fDigits(0x0),
64     fSDigitsArray(0x0),
65     fhitMap(0x0)
66 {
67   // Default ctor - don't use it
68 }
69
70 //___________________________________________
71 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager): 
72   AliDigitizer(manager), 
73   fGeom(0x0),
74   fDigits(0x0),
75   fSDigitsArray(0x0),
76   fhitMap(0x0)
77 {
78   //ctor with RunDigitizer
79 }
80
81 //------------------------------------------------------------------------
82 AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
83   AliDigitizer(source),
84   fDigits(0),
85   fSDigitsArray(0),
86   fhitMap(0),
87   fGeom(0x0) 
88 {
89   // copy constructor
90   this->fDigits=source.fDigits;
91   this->fSDigitsArray=source.fSDigitsArray;
92   this->fhitMap=source.fhitMap;
93   this->fGeom=source.fGeom; 
94
95 }
96
97 //------------------------------------------------------------------------
98   AliTOFDigitizer& AliTOFDigitizer::operator=(const AliTOFDigitizer &source)
99 {
100   // ass. op.
101   this->fDigits=source.fDigits;
102   this->fSDigitsArray=source.fSDigitsArray;
103   this->fhitMap=source.fhitMap;
104   this->fGeom=source.fGeom; 
105   return *this;
106
107 }
108
109 //------------------------------------------------------------------------
110 AliTOFDigitizer::~AliTOFDigitizer()
111 {
112   // Destructor
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   outrl->CdGAFile();
149   TFile *in=(TFile*)gFile;
150   TDirectory *savedir=gDirectory;
151
152   if (!in->IsOpen()) {
153     AliWarning("Geometry file is not open default  TOF geometry will be used");
154     fGeom = new AliTOFGeometryV5();
155   }
156   else {
157     in->cd();
158     fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
159   }
160
161   savedir->cd();
162
163   AliLoader* outgime = outrl->GetLoader("TOFLoader");
164   if (outgime == 0x0)
165    {
166      AliError("Can not get TOF Loader from Output Run Loader.");
167      return;
168    }
169   
170   TTree* treeD = outgime->TreeD();
171   if (treeD == 0x0)
172    {
173      outgime->MakeTree("D");
174      treeD = outgime->TreeD();
175    }
176   //Make branch for digits (to be created in Init())
177   tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
178
179   // container for all summed sdigits (to be created in Init())
180   fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
181   
182   // create hit map (to be created in Init())
183   fhitMap = new AliTOFHitMap(fSDigitsArray, fGeom);
184   
185   // Loop over files to digitize
186
187   for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
188        inputFile++) {
189     ReadSDigit(inputFile);
190    }
191
192   // create digits
193   CreateDigits();
194
195   // free used memory for Hit Map in current event
196   delete fhitMap;
197   fSDigitsArray->Delete();
198   delete fSDigitsArray;
199
200   treeD->Fill();
201  
202   outgime->WriteDigits("OVERWRITE");
203   outgime->UnloadDigits();
204   fDigits->Delete();
205   delete fDigits;
206
207 }
208
209 //---------------------------------------------------------------------
210
211 void AliTOFDigitizer::CreateDigits()
212 {
213   // loop on sdigits container to fill the AliTOFdigit TClonesArray
214   // start digitizing all the collected sdigits 
215
216   Int_t ndump=0; // dump the first ndump created digits for each event
217
218   // get the total number of collected sdigits
219   Int_t ndig = fSDigitsArray->GetEntriesFast();
220
221   for (Int_t k = 0; k < ndig; k++) {
222     
223     Int_t  vol[5];  // location for a digit
224     for (Int_t i=0; i<5; i++) vol[i] = -1;
225     
226     // Get the information for this digit
227     AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
228     
229     Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
230     // for current sdigit
231     
232     // TOF sdigit volumes (always the same for all slots)
233     Int_t sector    = tofsdigit->GetSector(); // range [0-17]
234     Int_t plate     = tofsdigit->GetPlate();  // range [0- 4]
235     Int_t strip     = tofsdigit->GetStrip();  // range [0-14/18/19]
236     Int_t padz      = tofsdigit->GetPadz();   // range [0- 1]
237     Int_t padx      = tofsdigit->GetPadx();   // range [0-47]
238     
239     vol[0] = sector;
240     vol[1] = plate;
241     vol[2] = strip;
242     vol[3] = padx;
243     vol[4] = padz;
244     
245     //--------------------- QA section ----------------------
246     // in the while, I perform QA
247     Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
248     
249     if (isSDigitBad) {
250       //AliFatal("strange sdigit found");
251       AliFatal(Form("strange sdigit found   %3i  %2i  %2i  %3i    %3i", sector, plate, padz, padx, strip));
252     }
253     //-------------------------------------------------------
254     
255     //------------------- Dump section ----------------------
256     if(k<ndump){
257       cout << k << "-th | " << "Sector " << sector << " | Plate " << plate << " | Strip " << strip << " | PadZ " << padz << " | PadX " << padx << endl;
258       cout << k << "-th sdigit" << endl;
259       cout << "----------------------------------------------------"<< endl;
260     }
261     // ------------------------------------------------------
262     
263     // start loop on number of slots for current sdigit
264     for (Int_t islot = 0; islot < nslot; islot++) {
265       Float_t  digit[4] = {-1.,-1.,-1.,-1.};     // TOF digit variables
266       Int_t tracknum[AliTOFSDigit::kMAXDIGITS];     // contributing tracks for the current slot
267       
268       Float_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
269       Float_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
270       
271       tracknum[0]=tofsdigit->GetTrack(islot,0);
272       tracknum[1]=tofsdigit->GetTrack(islot,1);
273       tracknum[2]=tofsdigit->GetTrack(islot,2);
274       
275       // new with placement must be used
276       // adding a TOF digit for each slot
277       TClonesArray &aDigits = *fDigits;
278       Int_t last=fDigits->GetEntriesFast();
279       new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
280
281     }
282     
283   } // end loop on sdigits - end digitizing all collected sdigits
284
285   //Insert Decalibration 
286
287   AliTOFcalib * calib = new AliTOFcalib(fGeom);
288   InitDecalibration(calib);
289   DecalibrateTOFSignal(calib);
290   delete calib;
291 }
292
293 //---------------------------------------------------------------------
294
295 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
296 {
297   // Read sdigits for current event and inputFile; 
298   // store them into the sdigits container
299   // and update the hit map
300   // SDigits from different files are assumed to
301   // be created with the same simulation parameters.
302   
303   // get the treeS from manager
304   AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
305   if (rl == 0x0)
306    {
307      AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
308      return;
309    }
310
311   AliLoader* gime = rl->GetLoader("TOFLoader");
312   if (gime == 0x0)
313    {
314      AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
315      return;
316    }
317
318   TTree* currentTreeS=gime->TreeS();
319   if (currentTreeS == 0x0)
320    {
321      Int_t retval = gime->LoadSDigits();
322      if (retval) 
323       {
324          AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
325          return;
326       }
327      currentTreeS=gime->TreeS();
328      if (currentTreeS == 0x0)
329       {
330          AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
331          return;
332       }
333    } 
334   // get the branch TOF inside the treeS
335   TClonesArray * sdigitsDummyContainer= new TClonesArray("AliTOFSDigit",  1000); 
336
337   // check if the branch exist
338   TBranch* tofBranch=currentTreeS->GetBranch("TOF");
339
340   if(!tofBranch){
341     AliFatal(Form("TOF branch not found for input %d",inputFile));
342   }
343   
344   tofBranch->SetAddress(&sdigitsDummyContainer);           
345   
346   Int_t nEntries = (Int_t)tofBranch->GetEntries();                                
347
348   // Loop through all entries in the tree
349   Int_t nbytes = 0;
350   
351   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
352     
353     // Import the tree
354     nbytes += tofBranch->GetEvent(iEntry);
355     
356     // Get the number of sdigits
357     Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
358     
359     for (Int_t k=0; k<ndig; k++) {
360       AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
361       
362       Int_t  vol[5]; // location for a sdigit
363       for (Int_t i=0; i<5; i++) vol[i] = -1;
364
365       // check the sdigit volume
366       vol[0] = tofSdigit->GetSector();
367       vol[1] = tofSdigit->GetPlate();
368       vol[2] = tofSdigit->GetStrip();
369       vol[3] = tofSdigit->GetPadx();
370       vol[4] = tofSdigit->GetPadz();
371       
372       if (fhitMap->TestHit(vol) != kEmpty) {
373         AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
374         sdig->Update(tofSdigit);
375
376       } else {
377
378         CollectSDigit(tofSdigit); // collect the current sdigit
379         fhitMap->SetHit(vol);     // update the hitmap for location vol
380
381       } // if (hitMap->TestHit(vol) != kEmpty)
382       
383     } // for (Int_t k=0; k<ndig; k++)
384     sdigitsDummyContainer->Delete();
385
386   } // end loop on entries
387
388   delete sdigitsDummyContainer;
389
390 }
391
392
393 //_____________________________________________________________________________
394 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
395 {
396   //
397   // Add a TOF sdigit in container
398   // new with placement must be used
399   TClonesArray &aSDigitsArray = *fSDigitsArray;
400   Int_t last=fSDigitsArray->GetEntriesFast();
401   // make a copy of the current sdigit and
402   // put it into tmp array
403   new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
404 }
405
406 //_____________________________________________________________________________
407 void AliTOFDigitizer::InitDecalibration( AliTOFcalib *calib) const {
408   // calib->ReadSimParFromCDB("TOF/Calib", 0); // original
409   calib->ReadSimParFromCDB("TOF/Calib", -1); // use AliCDBManager's number
410 }
411 //---------------------------------------------------------------------
412 void AliTOFDigitizer::DecalibrateTOFSignal( AliTOFcalib *calib){
413
414   // Read Calibration parameters from the CDB
415
416   AliTOFCal * cal= calib->GetTOFCalSimArray();
417
418   AliInfo(Form("Size of AliTOFCal = %i",cal->NPads()));
419   for (Int_t ipad = 0 ; ipad<cal->NPads(); ipad++){
420     AliTOFChannel *calChannel = cal->GetChannel(ipad);
421     Float_t par[6];
422     for (Int_t j = 0; j<6; j++){
423       par[j]=calChannel->GetSlewPar(j);
424     }
425   }
426
427   // Initialize Quantities to Simulate ToT Spectra
428
429
430   TH1F * hToT= calib->GetTOFSimToT();
431   Int_t nbins = hToT->GetNbinsX();
432   Float_t delta = hToT->GetBinWidth(1);
433   Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
434   Float_t minch = hToT->GetBinLowEdge(1);
435   Float_t max=0,min=0; //maximum and minimum value of the distribution
436   Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
437   for (Int_t ii=nbins; ii>0; ii--){
438     if (hToT->GetBinContent(ii)!= 0) {
439       max = maxch - (nbins-ii-1)*delta;
440       maxbin = ii; 
441       break;}
442   }
443   for (Int_t j=1; j<nbins; j++){
444     if (hToT->GetBinContent(j)!= 0) {
445       min = minch + (j-1)*delta;
446       minbin = j; 
447       break;}
448   }
449   Float_t maxToT=max;
450   Float_t minToT=min;
451   Float_t maxToTDistr=hToT->GetMaximum();
452   
453
454   // Loop on TOF Digits
455
456   Bool_t dbEntry=kFALSE;
457   Int_t ndigits = fDigits->GetEntriesFast();    
458   for (Int_t i=0;i<ndigits;i++){
459     AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
460     Int_t detId[5];
461     detId[0] = dig->GetSector();
462     detId[1] = dig->GetPlate();
463     detId[2] = dig->GetStrip();
464     detId[3] = dig->GetPadz();
465     detId[4] = dig->GetPadx();
466     // For Data with no Miscalibration, set ToT signal == Adc
467     dig->SetToT(dig->GetAdc());
468     if(hToT->GetEntries()>0){  
469       Float_t trix = 0;
470       Float_t triy = 0;
471       Float_t simToT = 0;
472       while (simToT <= triy){
473         trix = gRandom->Rndm(i);
474         triy = gRandom->Rndm(i);
475         trix = (maxToT-minToT)*trix + minToT; 
476         triy = maxToTDistr*triy;
477         Int_t binx=hToT->FindBin(trix);
478         simToT=hToT->GetBinContent(binx);
479       }
480     // Setting realistic ToT signal (only for Miscalibrated Data)   
481       dig->SetToT(trix);
482     }
483     Int_t index = calib->GetIndex(detId);     
484     AliTOFChannel *calChannel = cal->GetChannel(index);
485     Float_t par[6];
486     for (Int_t j = 0; j<6; j++){
487       par[j]=calChannel->GetSlewPar(j);
488       if(par[j]!=0)dbEntry=kTRUE;
489     }
490
491     Float_t tToT= dig->GetToT();
492     dig->SetTdcND(dig->GetTdc());
493     Float_t tdc = ((dig->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3; //tof signal in ns
494     Float_t timeoffset=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5])))); 
495     Float_t timeSlewed = tdc + timeoffset;
496     // Setting Decalibrated Time signal    
497     dig->SetTdc((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());   
498   }
499
500   if(hToT->GetEntries()<=0 || !dbEntry){
501     AliInfo("Standard Production, no miscalibrated digits");   
502   }else{
503     AliInfo("Miscalibrated digits");   
504   }
505
506   return;
507 }
508