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