]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TOF/AliTOFDigitizer.cxx
Coding conventions
[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
0e46b9ae 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//_________________________________________________________________________//
68861244 27
0e46b9ae 28#include "Riostream.h"
55cb22a1 29
0e46b9ae 30#include "TFile.h"
55cb22a1 31#include "TH1F.h"
0e46b9ae 32#include "TTree.h"
68861244 33
d3c7bfac 34#include "AliLoader.h"
0e46b9ae 35#include "AliLog.h"
d3c7bfac 36#include "AliRunDigitizer.h"
0e46b9ae 37#include "AliRunLoader.h"
38#include "AliRun.h"
68861244 39
0e46b9ae 40#include "AliTOFCal.h"
41#include "AliTOFcalib.h"
42#include "AliTOFChannel.h"
55cb22a1 43#include "AliTOFDigitizer.h"
8e72349e 44#include "AliTOFdigit.h"
8e72349e 45#include "AliTOFHitMap.h"
55cb22a1 46#include "AliTOFGeometryV5.h"
0e46b9ae 47#include "AliTOFSDigit.h"
48#include "AliTOF.h"
49
50extern TDirectory *gDirectory;
51extern TFile *gFile;
52extern TRandom *gRandom;
53
54extern AliRun *gAlice;
55
68861244 56
57ClassImp(AliTOFDigitizer)
58
8e72349e 59//___________________________________________
655e379f 60 AliTOFDigitizer::AliTOFDigitizer() :
61 AliDigitizer(),
62 fGeom(0x0),
63 fDigits(0x0),
64 fSDigitsArray(0x0),
65 fhitMap(0x0)
68861244 66{
8e72349e 67 // Default ctor - don't use it
68861244 68}
69
8e72349e 70//___________________________________________
655e379f 71AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager):
72 AliDigitizer(manager),
73 fGeom(0x0),
74 fDigits(0x0),
75 fSDigitsArray(0x0),
76 fhitMap(0x0)
68861244 77{
340693af 78 //ctor with RunDigitizer
68861244 79}
80
7aeeaf38 81//------------------------------------------------------------------------
655e379f 82AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
83 AliDigitizer(source),
9c80abb0 84 fGeom(0x0),
655e379f 85 fDigits(0),
86 fSDigitsArray(0),
9c80abb0 87 fhitMap(0)
7aeeaf38 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
8e72349e 109//------------------------------------------------------------------------
68861244 110AliTOFDigitizer::~AliTOFDigitizer()
111{
8e72349e 112 // Destructor
68861244 113}
8e72349e 114
115//---------------------------------------------------------------------
116
d076c8d5 117void AliTOFDigitizer::Exec(Option_t* /*option*/)
68861244 118{
8e72349e 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 //
68861244 128
d076c8d5 129 AliDebug(1, "");
68861244 130
68861244 131
8e72349e 132 // get the ptr to TOF detector
133 AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
68861244 134
8e72349e 135 //Make branches
136 char branchname[20];
137 sprintf (branchname, "%s", tof->GetName ());
138
139 fDigits=new TClonesArray("AliTOFdigit",4000);
88cb7938 140
141 AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
142 if (outrl == 0x0)
143 {
d076c8d5 144 AliError("Can not find Run Loader in output folder.");
88cb7938 145 return;
146 }
147
d3c7bfac 148 outrl->CdGAFile();
149 TFile *in=(TFile*)gFile;
150 TDirectory *savedir=gDirectory;
151
d3c7bfac 152 if (!in->IsOpen()) {
153 AliWarning("Geometry file is not open default TOF geometry will be used");
55cb22a1 154 fGeom = new AliTOFGeometryV5();
d3c7bfac 155 }
156 else {
157 in->cd();
55cb22a1 158 fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
d3c7bfac 159 }
160
161 savedir->cd();
162
88cb7938 163 AliLoader* outgime = outrl->GetLoader("TOFLoader");
164 if (outgime == 0x0)
165 {
d076c8d5 166 AliError("Can not get TOF Loader from Output Run Loader.");
88cb7938 167 return;
168 }
169
170 TTree* treeD = outgime->TreeD();
171 if (treeD == 0x0)
172 {
173 outgime->MakeTree("D");
174 treeD = outgime->TreeD();
175 }
8e72349e 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);
68861244 181
8e72349e 182 // create hit map (to be created in Init())
55cb22a1 183 fhitMap = new AliTOFHitMap(fSDigitsArray, fGeom);
68861244 184
8e72349e 185 // Loop over files to digitize
186
187 for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
188 inputFile++) {
189 ReadSDigit(inputFile);
88cb7938 190 }
8e72349e 191
192 // create digits
193 CreateDigits();
194
195 // free used memory for Hit Map in current event
196 delete fhitMap;
197 fSDigitsArray->Delete();
a5c70f8e 198 delete fSDigitsArray;
199
8e72349e 200 treeD->Fill();
201
88cb7938 202 outgime->WriteDigits("OVERWRITE");
203 outgime->UnloadDigits();
8e72349e 204 fDigits->Delete();
205 delete fDigits;
206
207}
208
209//---------------------------------------------------------------------
210
55cb22a1 211void AliTOFDigitizer::CreateDigits()
8e72349e 212{
213 // loop on sdigits container to fill the AliTOFdigit TClonesArray
214 // start digitizing all the collected sdigits
215
c7764ee9 216 Int_t ndump=0; // dump the first ndump created digits for each event
8e72349e 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++) {
68861244 222
da3d3acd 223 Int_t vol[5]; // location for a digit
7e6dce66 224 for (Int_t i=0; i<5; i++) vol[i] = -1;
68861244 225
8e72349e 226 // Get the information for this digit
227 AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
68861244 228
8e72349e 229 Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
230 // for current sdigit
68861244 231
8e72349e 232 // TOF sdigit volumes (always the same for all slots)
da3d3acd 233 Int_t sector = tofsdigit->GetSector(); // range [0-17]
234 Int_t plate = tofsdigit->GetPlate(); // range [0- 4]
d3c7bfac 235 Int_t strip = tofsdigit->GetStrip(); // range [0-14/18/19]
da3d3acd 236 Int_t padz = tofsdigit->GetPadz(); // range [0- 1]
237 Int_t padx = tofsdigit->GetPadx(); // range [0-47]
68861244 238
8e72349e 239 vol[0] = sector;
240 vol[1] = plate;
241 vol[2] = strip;
242 vol[3] = padx;
243 vol[4] = padz;
68861244 244
8e72349e 245 //--------------------- QA section ----------------------
246 // in the while, I perform QA
7e6dce66 247 Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
68861244 248
8e72349e 249 if (isSDigitBad) {
d3c7bfac 250 //AliFatal("strange sdigit found");
251 AliFatal(Form("strange sdigit found %3i %2i %2i %3i %3i", sector, plate, padz, padx, strip));
68861244 252 }
8e72349e 253 //-------------------------------------------------------
68861244 254
8e72349e 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;
68861244 260 }
8e72349e 261 // ------------------------------------------------------
68861244 262
8e72349e 263 // start loop on number of slots for current sdigit
264 for (Int_t islot = 0; islot < nslot; islot++) {
d0eb8f39 265 Float_t digit[4] = {-1.,-1.,-1.,-1.}; // TOF digit variables
7aeeaf38 266 Int_t tracknum[AliTOFSDigit::kMAXDIGITS]; // contributing tracks for the current slot
8e72349e 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);
68861244 274
7f1e928b 275 // new with placement must be used
8e72349e 276 // adding a TOF digit for each slot
7f1e928b 277 TClonesArray &aDigits = *fDigits;
278 Int_t last=fDigits->GetEntriesFast();
279 new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
280
68861244 281 }
68861244 282
8e72349e 283 } // end loop on sdigits - end digitizing all collected sdigits
55cb22a1 284
285 //Insert Decalibration
286
287 AliTOFcalib * calib = new AliTOFcalib(fGeom);
288 InitDecalibration(calib);
289 DecalibrateTOFSignal(calib);
290 delete calib;
68861244 291}
8e72349e 292
293//---------------------------------------------------------------------
294
295void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
68861244 296{
8e72349e 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.
68861244 302
8e72349e 303 // get the treeS from manager
88cb7938 304 AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
305 if (rl == 0x0)
306 {
d076c8d5 307 AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
88cb7938 308 return;
309 }
310
311 AliLoader* gime = rl->GetLoader("TOFLoader");
312 if (gime == 0x0)
313 {
d076c8d5 314 AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
88cb7938 315 return;
316 }
317
318 TTree* currentTreeS=gime->TreeS();
319 if (currentTreeS == 0x0)
320 {
321 Int_t retval = gime->LoadSDigits();
322 if (retval)
323 {
d076c8d5 324 AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
88cb7938 325 return;
326 }
327 currentTreeS=gime->TreeS();
328 if (currentTreeS == 0x0)
329 {
d076c8d5 330 AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
88cb7938 331 return;
332 }
333 }
8e72349e 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){
d076c8d5 341 AliFatal(Form("TOF branch not found for input %d",inputFile));
8e72349e 342 }
68861244 343
8e72349e 344 tofBranch->SetAddress(&sdigitsDummyContainer);
68861244 345
8e72349e 346 Int_t nEntries = (Int_t)tofBranch->GetEntries();
347
348 // Loop through all entries in the tree
b020b05f 349 Int_t nbytes = 0;
8e72349e 350
351 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
68861244 352
8e72349e 353 // Import the tree
354 nbytes += tofBranch->GetEvent(iEntry);
68861244 355
8e72349e 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
da3d3acd 362 Int_t vol[5]; // location for a sdigit
7e6dce66 363 for (Int_t i=0; i<5; i++) vol[i] = -1;
da3d3acd 364
8e72349e 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++)
a5c70f8e 384 sdigitsDummyContainer->Delete();
8e72349e 385
386 } // end loop on entries
387
a5c70f8e 388 delete sdigitsDummyContainer;
8e72349e 389
390}
391
392
393//_____________________________________________________________________________
394void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
395{
396 //
7f1e928b 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);
68861244 404}
55cb22a1 405
406//_____________________________________________________________________________
340693af 407void AliTOFDigitizer::InitDecalibration( AliTOFcalib *calib) const {
3f2bddfa 408 // calib->ReadSimParFromCDB("TOF/Calib", 0); // original
409 calib->ReadSimParFromCDB("TOF/Calib", -1); // use AliCDBManager's number
55cb22a1 410}
411//---------------------------------------------------------------------
412void 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++){
340693af 420 AliTOFChannel *calChannel = cal->GetChannel(ipad);
55cb22a1 421 Float_t par[6];
422 for (Int_t j = 0; j<6; j++){
340693af 423 par[j]=calChannel->GetSlewPar(j);
55cb22a1 424 }
425 }
426
427 // Initialize Quantities to Simulate ToT Spectra
428
429
430 TH1F * hToT= calib->GetTOFSimToT();
431 Int_t nbins = hToT->GetNbinsX();
340693af 432 Float_t delta = hToT->GetBinWidth(1);
433 Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
55cb22a1 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) {
340693af 439 max = maxch - (nbins-ii-1)*delta;
55cb22a1 440 maxbin = ii;
441 break;}
442 }
443 for (Int_t j=1; j<nbins; j++){
444 if (hToT->GetBinContent(j)!= 0) {
340693af 445 min = minch + (j-1)*delta;
55cb22a1 446 minbin = j;
447 break;}
448 }
340693af 449 Float_t maxToT=max;
450 Float_t minToT=min;
451 Float_t maxToTDistr=hToT->GetMaximum();
55cb22a1 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;
340693af 471 Float_t simToT = 0;
472 while (simToT <= triy){
55cb22a1 473 trix = gRandom->Rndm(i);
474 triy = gRandom->Rndm(i);
340693af 475 trix = (maxToT-minToT)*trix + minToT;
476 triy = maxToTDistr*triy;
55cb22a1 477 Int_t binx=hToT->FindBin(trix);
340693af 478 simToT=hToT->GetBinContent(binx);
55cb22a1 479 }
480 // Setting realistic ToT signal (only for Miscalibrated Data)
481 dig->SetToT(trix);
482 }
483 Int_t index = calib->GetIndex(detId);
340693af 484 AliTOFChannel *calChannel = cal->GetChannel(index);
9c80abb0 485 // time slewing parameters
55cb22a1 486 Float_t par[6];
487 for (Int_t j = 0; j<6; j++){
340693af 488 par[j]=calChannel->GetSlewPar(j);
55cb22a1 489 if(par[j]!=0)dbEntry=kTRUE;
490 }
9c80abb0 491 // the global time shift
492 Float_t timedelay = calChannel->GetDelay();
340693af 493 Float_t tToT= dig->GetToT();
55cb22a1 494 dig->SetTdcND(dig->GetTdc());
495 Float_t tdc = ((dig->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3; //tof signal in ns
9c80abb0 496 // add slewing effect
150af438 497 Float_t timeoffset=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5]))));
9c80abb0 498 Float_t timeSlewed = tdc+timeoffset;
499 // add global time shift
500 timeSlewed = timeSlewed + timedelay;
501
55cb22a1 502 // Setting Decalibrated Time signal
503 dig->SetTdc((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());
504 }
505
506 if(hToT->GetEntries()<=0 || !dbEntry){
507 AliInfo("Standard Production, no miscalibrated digits");
508 }else{
509 AliInfo("Miscalibrated digits");
510 }
511
512 return;
513}
514