1 /**************************************************************************
2 * Copyright(c) 1998-2000, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 //_________________________________________________________________________//
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 //
22 // Digits are written to TreeD in branch "TOF". //
24 // -- Author : F. Pierella (Bologna University) pierella@bo.infn.it //
26 //_________________________________________________________________________//
28 #include "Riostream.h"
35 #include "AliLoader.h"
37 #include "AliRunDigitizer.h"
38 #include "AliRunLoader.h"
41 #include "AliTOFCal.h"
42 #include "AliTOFcalib.h"
43 #include "AliTOFChannel.h"
44 #include "AliTOFDigitizer.h"
45 #include "AliTOFdigit.h"
46 #include "AliTOFHitMap.h"
47 #include "AliTOFGeometryV5.h"
48 #include "AliTOFSDigit.h"
51 extern TDirectory *gDirectory;
53 extern TRandom *gRandom;
55 extern AliRun *gAlice;
58 ClassImp(AliTOFDigitizer)
60 //___________________________________________
61 AliTOFDigitizer::AliTOFDigitizer() :
68 // Default ctor - don't use it
71 //___________________________________________
72 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager):
73 AliDigitizer(manager),
79 //ctor with RunDigitizer
82 //------------------------------------------------------------------------
83 AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
91 this->fDigits=source.fDigits;
92 this->fSDigitsArray=source.fSDigitsArray;
93 this->fhitMap=source.fhitMap;
94 this->fGeom=source.fGeom;
98 //------------------------------------------------------------------------
99 AliTOFDigitizer& AliTOFDigitizer::operator=(const AliTOFDigitizer &source)
102 this->fDigits=source.fDigits;
103 this->fSDigitsArray=source.fSDigitsArray;
104 this->fhitMap=source.fhitMap;
105 this->fGeom=source.fGeom;
110 //------------------------------------------------------------------------
111 AliTOFDigitizer::~AliTOFDigitizer()
116 //---------------------------------------------------------------------
118 void AliTOFDigitizer::Exec(Option_t* /*option*/)
121 // Perform digitization and merging.
122 // The algorithm is the following:
123 // - a hitmap is created to check if a pad is already activated;
124 // - an sdigits container is created to collect all sdigits from
126 // - sdigits are summed using the hitmap;
127 // - the sdigits container is used to create the array of AliTOFdigit.
133 // get the ptr to TOF detector
134 AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
138 sprintf (branchname, "%s", tof->GetName ());
140 fDigits=new TClonesArray("AliTOFdigit",4000);
142 AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
145 AliError("Can not find Run Loader in output folder.");
150 TFile *in=(TFile*)gFile;
151 TDirectory *savedir=gDirectory;
154 AliWarning("Geometry file is not open default TOF geometry will be used");
155 fGeom = new AliTOFGeometryV5();
159 fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
164 AliLoader* outgime = outrl->GetLoader("TOFLoader");
167 AliError("Can not get TOF Loader from Output Run Loader.");
171 TTree* treeD = outgime->TreeD();
174 outgime->MakeTree("D");
175 treeD = outgime->TreeD();
177 //Make branch for digits (to be created in Init())
178 tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
180 // container for all summed sdigits (to be created in Init())
181 fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
183 // create hit map (to be created in Init())
184 fhitMap = new AliTOFHitMap(fSDigitsArray, fGeom);
186 // Loop over files to digitize
188 for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
190 ReadSDigit(inputFile);
196 // free used memory for Hit Map in current event
198 fSDigitsArray->Delete();
199 delete fSDigitsArray;
203 outgime->WriteDigits("OVERWRITE");
204 outgime->UnloadDigits();
210 //---------------------------------------------------------------------
212 void AliTOFDigitizer::CreateDigits()
214 // loop on sdigits container to fill the AliTOFdigit TClonesArray
215 // start digitizing all the collected sdigits
217 Int_t ndump=0; // dump the first ndump created digits for each event
219 // get the total number of collected sdigits
220 Int_t ndig = fSDigitsArray->GetEntriesFast();
222 for (Int_t k = 0; k < ndig; k++) {
224 Int_t vol[5]; // location for a digit
225 for (Int_t i=0; i<5; i++) vol[i] = -1;
227 // Get the information for this digit
228 AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
230 Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
231 // for current sdigit
233 // TOF sdigit volumes (always the same for all slots)
234 Int_t sector = tofsdigit->GetSector(); // range [0-17]
235 Int_t plate = tofsdigit->GetPlate(); // range [0- 4]
236 Int_t strip = tofsdigit->GetStrip(); // range [0-14/18/19]
237 Int_t padz = tofsdigit->GetPadz(); // range [0- 1]
238 Int_t padx = tofsdigit->GetPadx(); // range [0-47]
246 //--------------------- QA section ----------------------
247 // in the while, I perform QA
248 Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
251 //AliFatal("strange sdigit found");
252 AliFatal(Form("strange sdigit found %3i %2i %2i %3i %3i", sector, plate, padz, padx, strip));
254 //-------------------------------------------------------
256 //------------------- Dump section ----------------------
258 AliInfo(Form("%2i-th | Sector %2i | Plate %1i | Strip %2i | PadZ %1i | PadX %2i ", k, sector, plate, strip, padz, padx));
259 AliInfo(Form("%2i-th", k));
260 AliInfo("----------------------------------------------------");
262 // ------------------------------------------------------
264 // start loop on number of slots for current sdigit
265 for (Int_t islot = 0; islot < nslot; islot++) {
266 Int_t digit[4] = {-1,-1,-1,-1}; // TOF digit variables
267 Int_t tracknum[AliTOFSDigit::kMAXDIGITS]; // contributing tracks for the current slot
269 Int_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
270 Int_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
272 tracknum[0]=tofsdigit->GetTrack(islot,0);
273 tracknum[1]=tofsdigit->GetTrack(islot,1);
274 tracknum[2]=tofsdigit->GetTrack(islot,2);
276 // new with placement must be used
277 // adding a TOF digit for each slot
278 TClonesArray &aDigits = *fDigits;
279 Int_t last=fDigits->GetEntriesFast();
280 new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
284 } // end loop on sdigits - end digitizing all collected sdigits
286 //Insert Decalibration
288 AliTOFcalib * calib = new AliTOFcalib(fGeom);
289 InitDecalibration(calib);
290 DecalibrateTOFSignal(calib);
294 //---------------------------------------------------------------------
296 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
298 // Read sdigits for current event and inputFile;
299 // store them into the sdigits container
300 // and update the hit map
301 // SDigits from different files are assumed to
302 // be created with the same simulation parameters.
304 // get the treeS from manager
305 AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
308 AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
312 AliLoader* gime = rl->GetLoader("TOFLoader");
315 AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
319 TTree* currentTreeS=gime->TreeS();
320 if (currentTreeS == 0x0)
322 Int_t retval = gime->LoadSDigits();
325 AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
328 currentTreeS=gime->TreeS();
329 if (currentTreeS == 0x0)
331 AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
335 // get the branch TOF inside the treeS
336 TClonesArray * sdigitsDummyContainer= new TClonesArray("AliTOFSDigit", 1000);
338 // check if the branch exist
339 TBranch* tofBranch=currentTreeS->GetBranch("TOF");
342 AliFatal(Form("TOF branch not found for input %d",inputFile));
345 tofBranch->SetAddress(&sdigitsDummyContainer);
347 Int_t nEntries = (Int_t)tofBranch->GetEntries();
349 // Loop through all entries in the tree
352 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
355 nbytes += tofBranch->GetEvent(iEntry);
357 // Get the number of sdigits
358 Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
360 for (Int_t k=0; k<ndig; k++) {
361 AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
363 Int_t vol[5]; // location for a sdigit
364 for (Int_t i=0; i<5; i++) vol[i] = -1;
366 // check the sdigit volume
367 vol[0] = tofSdigit->GetSector();
368 vol[1] = tofSdigit->GetPlate();
369 vol[2] = tofSdigit->GetStrip();
370 vol[3] = tofSdigit->GetPadx();
371 vol[4] = tofSdigit->GetPadz();
373 if (fhitMap->TestHit(vol) != kEmpty) {
374 AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
375 sdig->Update(tofSdigit);
379 CollectSDigit(tofSdigit); // collect the current sdigit
380 fhitMap->SetHit(vol); // update the hitmap for location vol
382 } // if (hitMap->TestHit(vol) != kEmpty)
384 } // for (Int_t k=0; k<ndig; k++)
385 sdigitsDummyContainer->Delete();
387 } // end loop on entries
389 delete sdigitsDummyContainer;
394 //_____________________________________________________________________________
395 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
398 // Add a TOF sdigit in container
399 // new with placement must be used
400 TClonesArray &aSDigitsArray = *fSDigitsArray;
401 Int_t last=fSDigitsArray->GetEntriesFast();
402 // make a copy of the current sdigit and
403 // put it into tmp array
404 new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
407 //_____________________________________________________________________________
408 void AliTOFDigitizer::InitDecalibration( AliTOFcalib *calib) const {
409 calib->ReadSimParFromCDB("TOF/Calib", -1); // use AliCDBManager's number
411 //---------------------------------------------------------------------
412 void AliTOFDigitizer::DecalibrateTOFSignal( AliTOFcalib *calib){
414 // Read Calibration parameters from the CDB
416 AliTOFCal * cal= calib->GetTOFCalSimArray();
418 AliDebug(2,Form("Size of AliTOFCal = %i",cal->NPads()));
420 // Initialize Quantities to Simulate ToT Spectra
422 TH1F * hToT= calib->GetTOFSimToT();
423 Int_t nbins = hToT->GetNbinsX();
424 Float_t delta = hToT->GetBinWidth(1);
425 Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
426 Float_t minch = hToT->GetBinLowEdge(1);
427 Float_t max=0,min=0; //maximum and minimum value of the distribution
428 Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
430 for (Int_t ii=nbins; ii>0; ii--){
431 if (hToT->GetBinContent(ii)!= 0) {
432 max = maxch - (nbins-ii-1)*delta;
436 for (Int_t j=1; j<nbins; j++){
437 if (hToT->GetBinContent(j)!= 0) {
438 min = minch + (j-1)*delta;
446 Float_t maxToTDistr=hToT->GetMaximum();
448 AliDebug (1, Form(" The minimum ToT = %f", minToT));
449 AliDebug (1, Form(" The maximum ToT = %f", maxToT));
450 AliDebug (1, Form(" The maximum peak in ToT = %f", maxToTDistr));
452 // Loop on TOF Digits
454 Bool_t isToTSimulated=kFALSE;
455 Bool_t misCalibPars=kFALSE;
456 if(hToT->GetEntries()>0)isToTSimulated=kTRUE;
457 Int_t ndigits = fDigits->GetEntriesFast();
458 for (Int_t i=0;i<ndigits;i++){
459 AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
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 dig->SetTdcND(dig->GetTdc()); // save the non decalibrated time
469 //A realistic ToT Spectrum was found in input,
470 //decalibrated TOF Digits likely to be simulated....
472 Int_t index = calib->GetIndex(detId); // The channel index
473 AliTOFChannel *calChannel = cal->GetChannel(index); //retrieve the info
474 Float_t timedelay = calChannel->GetDelay(); //The global channel delay
475 Float_t par[6]; // time slewing parameters
477 //check whether we actually ask for miscalibration
479 if(timedelay!=0)misCalibPars=kTRUE;
480 for (Int_t j = 0; j<6; j++){
481 par[j]=calChannel->GetSlewPar(j);
482 if(par[j]!=0)misCalibPars=kTRUE;
485 // Now generate Realistic ToT distribution from TestBeam Data.
486 // Tot is in ns, assuming a Matching Window of 10 ns.
491 while (simToT <= triy){
492 trix = gRandom->Rndm(i);
493 triy = gRandom->Rndm(i);
494 trix = (maxToT-minToT)*trix + minToT;
495 triy = maxToTDistr*triy;
496 Int_t binx=hToT->FindBin(trix);
497 simToT=hToT->GetBinContent(binx);
499 // the generated ToT (ns)
500 Float_t tToT= trix; // to apply slewing we start from ns..
501 // transform TOF signal in ns, factor 1E-3 as bin width is in ps.
502 AliDebug(2,Form(" Initial Time (counts): %i: ",dig->GetTdc()));
503 Float_t time = ((dig->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3;
504 AliDebug(2,Form(" Time before miscalibration (ns) %f: ",time));
505 // add slewing effect
506 Float_t timeoffset=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5]))));
507 Float_t timeSlewed = time+timeoffset;
508 AliDebug(2,Form(" Time after applying slewing (ns): %f: ",timeSlewed));
509 // add global time shift
510 timeSlewed = timeSlewed + timedelay;
511 AliDebug(2,Form(" Time after applying delay (ns): %f: ",timeSlewed));
512 Int_t tdc= (Int_t)((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());
513 // Setting Decalibrated Time signal (TDC counts)
515 AliDebug(2,Form(" Final Time (counts): %i: ",dig->GetTdc()));
516 // Setting realistic ToT signal (TDC counts)
517 // Setting Decalibrated Time signal (TDC counts)
518 Int_t tot=(Int_t)(trix/AliTOFGeometry::ToTBinWidth()*1.E3);//(factor 1E3 as input ToT is in ns)
520 AliDebug(2,Form(" Final Tot (counts): %i: ",dig->GetToT()));
522 AliWarning (Form(" The bad Slewed Time(TDC counts)= %i ", tdc));
523 AliWarning(Form(" The bad ToT (TDC counts)= %i ", tot));
527 // For Data with no Miscalibration, set ToT signal == Adc
528 // dig->SetToT(dig->GetAdc()/AliTOFGeometry::ToTBinWidth()*1.E3);
529 dig->SetToT((Int_t)(dig->GetAdc()/AliTOFGeometry::ToTBinWidth())); //remove the factor 10^3 just to have a reasonable ToT range for raw data simulation even in the case of non-realistic ToT distribution (n.b. fAdc is practically an arbitrary quantity, and ToT has no impact on the TOF reco for non-miscalibrated digits)
534 AliDebug(1,"Standard Production, no miscalibrated digits");
537 AliDebug(1,"Standard Production, no miscalibrated digits");
540 AliDebug(1,"Simulating miscalibrated digits");