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