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