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"
34 #include "TObjArray.h"
36 #include "AliLoader.h"
38 #include "AliRunDigitizer.h"
39 #include "AliRunLoader.h"
42 #include "AliTOFcalib.h"
43 //#include "AliTOFChannelOnline.h"
44 #include "AliTOFChannelOffline.h"
45 #include "AliTOFDigitizer.h"
46 #include "AliTOFdigit.h"
47 #include "AliTOFHitMap.h"
48 #include "AliTOFGeometry.h"
49 #include "AliTOFSDigit.h"
52 extern TDirectory *gDirectory;
53 //extern TFile *gFile;
54 extern TRandom *gRandom;
56 extern AliRun *gAlice;
59 ClassImp(AliTOFDigitizer)
61 //___________________________________________
62 AliTOFDigitizer::AliTOFDigitizer() :
64 fDigits(new TClonesArray("AliTOFdigit",4000)),
65 fSDigitsArray(new TClonesArray("AliTOFSDigit",1000)),
67 fCalib(new AliTOFcalib())
69 // Default ctor - don't use it
73 //___________________________________________
74 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager):
75 AliDigitizer(manager),
76 fDigits(new TClonesArray("AliTOFdigit",4000)),
77 fSDigitsArray(new TClonesArray("AliTOFSDigit",1000)),
79 fCalib(new AliTOFcalib())
81 //ctor with RunDigitizer
85 //------------------------------------------------------------------------
86 AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
88 fDigits(source.fDigits),
89 fSDigitsArray(source.fSDigitsArray),
90 fhitMap(source.fhitMap),
96 //------------------------------------------------------------------------
97 AliTOFDigitizer& AliTOFDigitizer::operator=(const AliTOFDigitizer &source)
100 this->fDigits=source.fDigits;
101 this->fSDigitsArray=source.fSDigitsArray;
102 this->fhitMap=source.fhitMap;
103 this->fCalib=source.fCalib;
108 //------------------------------------------------------------------------
109 AliTOFDigitizer::~AliTOFDigitizer()
119 fSDigitsArray->Delete();
120 delete fSDigitsArray;
125 //---------------------------------------------------------------------
127 void AliTOFDigitizer::Exec(Option_t* /*option*/)
130 // Perform digitization and merging.
131 // The algorithm is the following:
132 // - a hitmap is created to check if a pad is already activated;
133 // - an sdigits container is created to collect all sdigits from
135 // - sdigits are summed using the hitmap;
136 // - the sdigits container is used to create the array of AliTOFdigit.
142 // get the ptr to TOF detector
143 AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
147 sprintf (branchname, "%s", tof->GetName ());
149 AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
152 AliError("Can not find Run Loader in output folder.");
158 TFile *in=(TFile*)gFile;
159 TDirectory *savedir=gDirectory;
162 //when fGeom was needed
165 AliWarning("Geometry file is not open default TOF geometry will be used");
166 fGeom = new AliTOFGeometry();
170 fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
175 AliLoader* outgime = outrl->GetLoader("TOFLoader");
178 AliError("Can not get TOF Loader from Output Run Loader.");
182 TTree* treeD = outgime->TreeD();
185 outgime->MakeTree("D");
186 treeD = outgime->TreeD();
188 //Make branch for digits (to be created in Init())
189 tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
191 // container for all summed sdigits (to be created in Init())
192 //fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
194 // create hit map (to be created in Init())
195 fhitMap = new AliTOFHitMap(fSDigitsArray);
197 // Loop over files to digitize
199 for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
201 ReadSDigit(inputFile);
207 // free used memory for Hit Map in current event
209 fSDigitsArray->Clear();
213 outgime->WriteDigits("OVERWRITE");
214 outgime->UnloadDigits();
219 //---------------------------------------------------------------------
221 void AliTOFDigitizer::CreateDigits()
223 // loop on sdigits container to fill the AliTOFdigit TClonesArray
224 // start digitizing all the collected sdigits
226 Int_t ndump=0; // dump the first ndump created digits for each event
228 // get the total number of collected sdigits
229 Int_t ndig = fSDigitsArray->GetEntriesFast();
231 for (Int_t k = 0; k < ndig; k++) {
233 Int_t vol[5]; // location for a digit
234 for (Int_t i=0; i<5; i++) vol[i] = -1;
236 // Get the information for this digit
237 AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
239 Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
240 // for current sdigit
242 // TOF sdigit volumes (always the same for all slots)
243 Int_t sector = tofsdigit->GetSector(); // range [0-17]
244 Int_t plate = tofsdigit->GetPlate(); // range [0- 4]
245 Int_t strip = tofsdigit->GetStrip(); // range [0-14/18/19]
246 Int_t padz = tofsdigit->GetPadz(); // range [0- 1]
247 Int_t padx = tofsdigit->GetPadx(); // range [0-47]
255 //--------------------- QA section ----------------------
256 // in the while, I perform QA
257 Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
260 //AliFatal("strange sdigit found");
261 AliFatal(Form("strange sdigit found %3i %2i %2i %3i %3i", sector, plate, padz, padx, strip));
263 //-------------------------------------------------------
265 //------------------- Dump section ----------------------
267 AliInfo(Form("%2i-th | Sector %2i | Plate %1i | Strip %2i | PadZ %1i | PadX %2i ", k, sector, plate, strip, padz, padx));
268 AliInfo(Form("%2i-th", k));
269 AliInfo("----------------------------------------------------");
271 // ------------------------------------------------------
273 // start loop on number of slots for current sdigit
274 for (Int_t islot = 0; islot < nslot; islot++) {
275 Int_t digit[4] = {-1,-1,-1,-1}; // TOF digit variables
276 Int_t tracknum[AliTOFSDigit::kMAXDIGITS]; // contributing tracks for the current slot
278 Int_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
279 Int_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
281 tracknum[0]=tofsdigit->GetTrack(islot,0);
282 tracknum[1]=tofsdigit->GetTrack(islot,1);
283 tracknum[2]=tofsdigit->GetTrack(islot,2);
285 // new with placement must be used
286 // adding a TOF digit for each slot
287 TClonesArray &aDigits = *fDigits;
288 Int_t last=fDigits->GetEntriesFast();
289 new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
293 } // end loop on sdigits - end digitizing all collected sdigits
295 //Insert Decalibration
296 AliInfo("in digitizer, create digits");
297 DecalibrateTOFSignal();
300 //---------------------------------------------------------------------
302 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
304 // Read sdigits for current event and inputFile;
305 // store them into the sdigits container
306 // and update the hit map
307 // SDigits from different files are assumed to
308 // be created with the same simulation parameters.
310 // creating the TClonesArray to store the digits
311 static TClonesArray sdigitsClonesArray("AliTOFSDigit", 1000);
312 sdigitsClonesArray.Clear();
314 // get the treeS from manager
315 AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
318 AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
322 AliLoader* gime = rl->GetLoader("TOFLoader");
325 AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
329 TTree* currentTreeS=gime->TreeS();
330 if (currentTreeS == 0x0)
332 Int_t retval = gime->LoadSDigits();
335 AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
338 currentTreeS=gime->TreeS();
339 if (currentTreeS == 0x0)
341 AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
345 // get the branch TOF inside the treeS
346 TClonesArray * sdigitsDummyContainer=&sdigitsClonesArray;
347 // check if the branch exist
348 TBranch* tofBranch=currentTreeS->GetBranch("TOF");
351 AliFatal(Form("TOF branch not found for input %d",inputFile));
354 tofBranch->SetAddress(&sdigitsDummyContainer);
356 Int_t nEntries = (Int_t)tofBranch->GetEntries();
358 // Loop through all entries in the tree
361 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
364 nbytes += tofBranch->GetEvent(iEntry);
366 // Get the number of sdigits
367 Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
369 for (Int_t k=0; k<ndig; k++) {
370 AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
372 Int_t vol[5]; // location for a sdigit
373 for (Int_t i=0; i<5; i++) vol[i] = -1;
375 // check the sdigit volume
376 vol[0] = tofSdigit->GetSector();
377 vol[1] = tofSdigit->GetPlate();
378 vol[2] = tofSdigit->GetStrip();
379 vol[3] = tofSdigit->GetPadx();
380 vol[4] = tofSdigit->GetPadz();
382 if (fhitMap->TestHit(vol) != kEmpty) {
383 AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
384 sdig->Update(tofSdigit);
388 CollectSDigit(tofSdigit); // collect the current sdigit
389 fhitMap->SetHit(vol); // update the hitmap for location vol
391 } // if (hitMap->TestHit(vol) != kEmpty)
393 } // for (Int_t k=0; k<ndig; k++)
395 } // end loop on entries
400 //_____________________________________________________________________________
401 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
404 // Add a TOF sdigit in container
405 // new with placement must be used
406 TClonesArray &aSDigitsArray = *fSDigitsArray;
407 Int_t last=fSDigitsArray->GetEntriesFast();
408 // make a copy of the current sdigit and
409 // put it into tmp array
410 new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
413 //_____________________________________________________________________________
414 void AliTOFDigitizer::InitDecalibration() const {
419 fCalib->CreateCalArrays();
420 fCalib->ReadSimHistoFromCDB("TOF/Calib", -1); // use AliCDBManager's number
421 fCalib->ReadParOfflineFromCDB("TOF/Calib", -1); // use AliCDBManager's number
423 //---------------------------------------------------------------------
424 void AliTOFDigitizer::DecalibrateTOFSignal(){
426 // Read Calibration parameters from the CDB
428 TObjArray * calOffline= fCalib->GetTOFCalArrayOffline();
430 AliDebug(2,Form("Size of array for Offline Calibration = %i",calOffline->GetEntries()));
432 // Initialize Quantities to Simulate ToT Spectra
434 TH1F * hToT= fCalib->GetTOFSimToT();
435 Int_t nbins = hToT->GetNbinsX();
436 Float_t delta = hToT->GetBinWidth(1);
437 Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
438 Float_t minch = hToT->GetBinLowEdge(1);
439 Float_t max=0,min=0; //maximum and minimum value of the distribution
440 Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
442 for (Int_t ii=nbins; ii>0; ii--){
443 if (hToT->GetBinContent(ii)!= 0) {
444 max = maxch - (nbins-ii-1)*delta;
448 for (Int_t j=1; j<nbins; j++){
449 if (hToT->GetBinContent(j)!= 0) {
450 min = minch + (j-1)*delta;
458 Float_t maxToTDistr=hToT->GetMaximum();
460 AliDebug (1, Form(" The minimum ToT = %f", minToT));
461 AliDebug (1, Form(" The maximum ToT = %f", maxToT));
462 AliDebug (1, Form(" The maximum peak in ToT = %f", maxToTDistr));
464 // Loop on TOF Digits
466 Bool_t isToTSimulated=kFALSE;
467 Bool_t misCalibPars=kFALSE;
468 if(hToT->GetEntries()>0)isToTSimulated=kTRUE;
469 Int_t ndigits = fDigits->GetEntriesFast();
470 for (Int_t i=0;i<ndigits;i++){
471 AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
473 detId[0] = dig->GetSector();
474 detId[1] = dig->GetPlate();
475 detId[2] = dig->GetStrip();
476 detId[3] = dig->GetPadz();
477 detId[4] = dig->GetPadx();
478 dig->SetTdcND(dig->GetTdc()); // save the non decalibrated time
481 //A realistic ToT Spectrum was found in input,
482 //decalibrated TOF Digits likely to be simulated....
484 Int_t index = AliTOFGeometry::GetIndex(detId); // The channel index
485 AliTOFChannelOffline *calChannelOffline = (AliTOFChannelOffline *)calOffline->At(index); //retrieve the info for time slewing
486 Double_t par[6]; // time slewing parameters
488 //check whether we actually ask for miscalibration
490 for (Int_t j = 0; j<6; j++){
491 par[j]=(Double_t)calChannelOffline->GetSlewPar(j);
492 if(par[j]!=0)misCalibPars=kTRUE;
494 AliDebug(2,Form(" Calib Pars = %f (0-th parameter for time slewing + time delay), %f, %f, %f, %f, %f ",par[0],par[1],par[2],par[3],par[4],par[5]));
496 // Now generate Realistic ToT distribution from TestBeam Data.
497 // Tot is in ns, assuming a Matching Window of 10 ns.
504 while (simToT <= triy){
505 trix = gRandom->Rndm(i);
506 triy = gRandom->Rndm(i);
507 trix = (maxToT-minToT)*trix + minToT;
508 triy = maxToTDistr*triy;
509 Int_t binx=hToT->FindBin(trix);
510 simToT=hToT->GetBinContent(binx);
512 // the generated ToT (ns)
513 tToT= (Double_t) trix; // to apply slewing we start from ns..
514 // transform TOF signal in ns
515 AliDebug(2,Form(" The Initial Time (counts): %i: ",dig->GetTdc()));
516 AliDebug(2,Form(" Time before miscalibration (ps) %e: ",dig->GetTdc()*(Double_t)AliTOFGeometry::TdcBinWidth()));
517 // add slewing effect
518 timeCorr=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5]))));
519 AliDebug(2,Form(" The Time slewing + delay (ns): %f: ",timeCorr));
520 // add global time shift
523 Double_t timeMis = (Double_t)(dig->GetTdc())*(Double_t)AliTOFGeometry::TdcBinWidth();
524 timeMis = timeMis+timeCorr;
525 AliDebug(2,Form(" The Miscalibrated time (ps): %e: ",timeMis));
527 // now update the digit info
529 Int_t tdcCorr= (Int_t)(timeMis/AliTOFGeometry::TdcBinWidth());
530 AliDebug(2,Form(" Final Time (counts): %i: ",tdcCorr));
531 // Setting Decalibrated Time signal (TDC counts)
532 dig->SetTdc(tdcCorr);
533 // Setting realistic ToT signal (TDC counts)
534 tToT*=1E3; //back to ps
535 Int_t tot=(Int_t)(tToT/AliTOFGeometry::ToTBinWidth());//(factor 1E3 as input ToT is in ns)
537 AliDebug(2,Form(" Final Time and ToT (counts): %i: , %i:",dig->GetTdc(),dig->GetToT()));
539 AliWarning (Form(" The bad Slewed Time(TDC counts)= %i ", tdcCorr));
540 AliWarning(Form(" The bad ToT (TDC counts)= %i ", tot));
544 // For Data with no Miscalibration, set ToT signal == Adc
545 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)
550 AliDebug(1,"Standard Production, no miscalibrated digits");
553 AliDebug(1,"Standard Production, no miscalibrated digits");
556 AliDebug(1,"Simulating miscalibrated digits");