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