]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TOF/AliTOFDigitizer.cxx
Coding conventions (C.Oppedisano)
[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{
8e72349e 75 fDigits=0;
76 fSDigitsArray=0;
77 fhitMap=0;
55cb22a1 78 fGeom=0x0;
68861244 79}
80
8e72349e 81//------------------------------------------------------------------------
68861244 82AliTOFDigitizer::~AliTOFDigitizer()
83{
8e72349e 84 // Destructor
68861244 85}
8e72349e 86
87//---------------------------------------------------------------------
88
d076c8d5 89void AliTOFDigitizer::Exec(Option_t* /*option*/)
68861244 90{
8e72349e 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 //
68861244 100
d076c8d5 101 AliDebug(1, "");
68861244 102
68861244 103
8e72349e 104 // get the ptr to TOF detector
105 AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
68861244 106
8e72349e 107 //Make branches
108 char branchname[20];
109 sprintf (branchname, "%s", tof->GetName ());
110
111 fDigits=new TClonesArray("AliTOFdigit",4000);
88cb7938 112
113 AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
114 if (outrl == 0x0)
115 {
d076c8d5 116 AliError("Can not find Run Loader in output folder.");
88cb7938 117 return;
118 }
119
d3c7bfac 120 outrl->CdGAFile();
121 TFile *in=(TFile*)gFile;
122 TDirectory *savedir=gDirectory;
123
d3c7bfac 124 if (!in->IsOpen()) {
125 AliWarning("Geometry file is not open default TOF geometry will be used");
55cb22a1 126 fGeom = new AliTOFGeometryV5();
d3c7bfac 127 }
128 else {
129 in->cd();
55cb22a1 130 fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
d3c7bfac 131 }
132
133 savedir->cd();
134
88cb7938 135 AliLoader* outgime = outrl->GetLoader("TOFLoader");
136 if (outgime == 0x0)
137 {
d076c8d5 138 AliError("Can not get TOF Loader from Output Run Loader.");
88cb7938 139 return;
140 }
141
142 TTree* treeD = outgime->TreeD();
143 if (treeD == 0x0)
144 {
145 outgime->MakeTree("D");
146 treeD = outgime->TreeD();
147 }
8e72349e 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);
68861244 153
8e72349e 154 // create hit map (to be created in Init())
55cb22a1 155 fhitMap = new AliTOFHitMap(fSDigitsArray, fGeom);
68861244 156
8e72349e 157 // Loop over files to digitize
158
159 for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
160 inputFile++) {
161 ReadSDigit(inputFile);
88cb7938 162 }
8e72349e 163
164 // create digits
165 CreateDigits();
166
167 // free used memory for Hit Map in current event
168 delete fhitMap;
169 fSDigitsArray->Delete();
a5c70f8e 170 delete fSDigitsArray;
171
8e72349e 172 treeD->Fill();
173
88cb7938 174 outgime->WriteDigits("OVERWRITE");
175 outgime->UnloadDigits();
8e72349e 176 fDigits->Delete();
177 delete fDigits;
178
179}
180
181//---------------------------------------------------------------------
182
55cb22a1 183void AliTOFDigitizer::CreateDigits()
8e72349e 184{
185 // loop on sdigits container to fill the AliTOFdigit TClonesArray
186 // start digitizing all the collected sdigits
187
c7764ee9 188 Int_t ndump=0; // dump the first ndump created digits for each event
8e72349e 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++) {
68861244 194
da3d3acd 195 Int_t vol[5]; // location for a digit
7e6dce66 196 for (Int_t i=0; i<5; i++) vol[i] = -1;
68861244 197
8e72349e 198 // Get the information for this digit
199 AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
68861244 200
8e72349e 201 Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
202 // for current sdigit
68861244 203
8e72349e 204 // TOF sdigit volumes (always the same for all slots)
da3d3acd 205 Int_t sector = tofsdigit->GetSector(); // range [0-17]
206 Int_t plate = tofsdigit->GetPlate(); // range [0- 4]
d3c7bfac 207 Int_t strip = tofsdigit->GetStrip(); // range [0-14/18/19]
da3d3acd 208 Int_t padz = tofsdigit->GetPadz(); // range [0- 1]
209 Int_t padx = tofsdigit->GetPadx(); // range [0-47]
68861244 210
8e72349e 211 vol[0] = sector;
212 vol[1] = plate;
213 vol[2] = strip;
214 vol[3] = padx;
215 vol[4] = padz;
68861244 216
8e72349e 217 //--------------------- QA section ----------------------
218 // in the while, I perform QA
7e6dce66 219 Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
68861244 220
8e72349e 221 if (isSDigitBad) {
d3c7bfac 222 //AliFatal("strange sdigit found");
223 AliFatal(Form("strange sdigit found %3i %2i %2i %3i %3i", sector, plate, padz, padx, strip));
68861244 224 }
8e72349e 225 //-------------------------------------------------------
68861244 226
8e72349e 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;
68861244 232 }
8e72349e 233 // ------------------------------------------------------
68861244 234
8e72349e 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);
68861244 246
7f1e928b 247 // new with placement must be used
8e72349e 248 // adding a TOF digit for each slot
7f1e928b 249 TClonesArray &aDigits = *fDigits;
250 Int_t last=fDigits->GetEntriesFast();
251 new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
252
68861244 253 }
68861244 254
8e72349e 255 } // end loop on sdigits - end digitizing all collected sdigits
55cb22a1 256
257 //Insert Decalibration
258
259 AliTOFcalib * calib = new AliTOFcalib(fGeom);
260 InitDecalibration(calib);
261 DecalibrateTOFSignal(calib);
262 delete calib;
68861244 263}
8e72349e 264
265//---------------------------------------------------------------------
266
267void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
68861244 268{
8e72349e 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.
68861244 274
8e72349e 275 // get the treeS from manager
88cb7938 276 AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
277 if (rl == 0x0)
278 {
d076c8d5 279 AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
88cb7938 280 return;
281 }
282
283 AliLoader* gime = rl->GetLoader("TOFLoader");
284 if (gime == 0x0)
285 {
d076c8d5 286 AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
88cb7938 287 return;
288 }
289
290 TTree* currentTreeS=gime->TreeS();
291 if (currentTreeS == 0x0)
292 {
293 Int_t retval = gime->LoadSDigits();
294 if (retval)
295 {
d076c8d5 296 AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
88cb7938 297 return;
298 }
299 currentTreeS=gime->TreeS();
300 if (currentTreeS == 0x0)
301 {
d076c8d5 302 AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
88cb7938 303 return;
304 }
305 }
8e72349e 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){
d076c8d5 313 AliFatal(Form("TOF branch not found for input %d",inputFile));
8e72349e 314 }
68861244 315
8e72349e 316 tofBranch->SetAddress(&sdigitsDummyContainer);
68861244 317
8e72349e 318 Int_t nEntries = (Int_t)tofBranch->GetEntries();
319
320 // Loop through all entries in the tree
b020b05f 321 Int_t nbytes = 0;
8e72349e 322
323 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
68861244 324
8e72349e 325 // Import the tree
326 nbytes += tofBranch->GetEvent(iEntry);
68861244 327
8e72349e 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
da3d3acd 334 Int_t vol[5]; // location for a sdigit
7e6dce66 335 for (Int_t i=0; i<5; i++) vol[i] = -1;
da3d3acd 336
8e72349e 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++)
a5c70f8e 356 sdigitsDummyContainer->Delete();
8e72349e 357
358 } // end loop on entries
359
a5c70f8e 360 delete sdigitsDummyContainer;
8e72349e 361
362}
363
364
365//_____________________________________________________________________________
366void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
367{
368 //
7f1e928b 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);
68861244 376}
55cb22a1 377
378//_____________________________________________________________________________
379void AliTOFDigitizer::InitDecalibration( AliTOFcalib *calib){
28dd10b6 380 calib->ReadSimParFromCDB("TOF/Calib", 0);
55cb22a1 381}
382//---------------------------------------------------------------------
383void 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