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