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 "TObjArray.h"
37 #include "AliLoader.h"
39 #include "AliRunDigitizer.h"
40 #include "AliRunLoader.h"
43 #include "AliTOFcalib.h"
44 //#include "AliTOFChannelOnlineArray.h"
45 //#include "AliTOFChannelOnlineStatusArray.h"
46 //#include "AliTOFChannelOffline.h"
47 #include "AliTOFDigitizer.h"
48 #include "AliTOFdigit.h"
49 #include "AliTOFHitMap.h"
50 #include "AliTOFGeometry.h"
51 #include "AliTOFSDigit.h"
54 extern TDirectory *gDirectory;
55 //extern TFile *gFile;
56 extern TRandom *gRandom;
58 extern AliRun *gAlice;
61 ClassImp(AliTOFDigitizer)
63 //___________________________________________
64 AliTOFDigitizer::AliTOFDigitizer() :
66 fDigits(new TClonesArray("AliTOFdigit",4000)),
67 fSDigitsArray(new TClonesArray("AliTOFSDigit",1000)),
69 fCalib(new AliTOFcalib())
71 // Default ctor - don't use it
75 //___________________________________________
76 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager):
77 AliDigitizer(manager),
78 fDigits(new TClonesArray("AliTOFdigit",4000)),
79 fSDigitsArray(new TClonesArray("AliTOFSDigit",1000)),
81 fCalib(new AliTOFcalib())
83 //ctor with RunDigitizer
87 //------------------------------------------------------------------------
88 AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
90 fDigits(source.fDigits),
91 fSDigitsArray(source.fSDigitsArray),
92 fhitMap(source.fhitMap),
98 //------------------------------------------------------------------------
99 AliTOFDigitizer& AliTOFDigitizer::operator=(const AliTOFDigitizer &source)
106 AliDigitizer::operator=(source);
107 fDigits=source.fDigits;
108 fSDigitsArray=source.fSDigitsArray;
109 fhitMap=source.fhitMap;
110 fCalib=source.fCalib;
115 //------------------------------------------------------------------------
116 AliTOFDigitizer::~AliTOFDigitizer()
126 fSDigitsArray->Delete();
127 delete fSDigitsArray;
132 //---------------------------------------------------------------------
134 void AliTOFDigitizer::Exec(Option_t* /*option*/)
137 // Perform digitization and merging.
138 // The algorithm is the following:
139 // - a hitmap is created to check if a pad is already activated;
140 // - an sdigits container is created to collect all sdigits from
142 // - sdigits are summed using the hitmap;
143 // - the sdigits container is used to create the array of AliTOFdigit.
149 // get the ptr to TOF detector
150 AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
154 sprintf (branchname, "%s", tof->GetName ());
156 AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
159 AliError("Can not find Run Loader in output folder.");
163 AliLoader* outgime = outrl->GetLoader("TOFLoader");
166 AliError("Can not get TOF Loader from Output Run Loader.");
170 TTree* treeD = outgime->TreeD();
173 outgime->MakeTree("D");
174 treeD = outgime->TreeD();
176 //Make branch for digits (to be created in Init())
177 tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
179 // container for all summed sdigits (to be created in Init())
180 //fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
182 // create hit map (to be created in Init())
183 fhitMap = new AliTOFHitMap(fSDigitsArray);
185 // Loop over files to digitize
187 for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
189 ReadSDigit(inputFile);
195 // free used memory for Hit Map in current event
197 fSDigitsArray->Clear();
201 AliDebug(2,"----------------------------------------");
202 AliDebug(1,Form("%d digits have been created", fDigits->GetEntriesFast()));
203 AliDebug(2,"----------------------------------------");
205 outgime->WriteDigits("OVERWRITE");
206 outgime->UnloadDigits();
211 //---------------------------------------------------------------------
213 void AliTOFDigitizer::CreateDigits()
215 // loop on sdigits container to fill the AliTOFdigit TClonesArray
216 // start digitizing all the collected sdigits
218 Int_t ndump=0; // dump the first ndump created digits for each event
220 // get the total number of collected sdigits
221 Int_t ndig = fSDigitsArray->GetEntriesFast();
223 Int_t vol[5]={-1,-1,-1,-1,-1}; // location for a digit
224 Int_t digit[4] = {0,0,0,0}; // TOF digit variables
225 Int_t tracknum[AliTOFSDigit::kMAXDIGITS]; // contributing tracks for the current slot
226 for (Int_t aa=0; aa<AliTOFSDigit::kMAXDIGITS; aa++) tracknum[aa] = -1;
228 for (Int_t k = 0; k < ndig; k++) {
230 for (Int_t i=0; i<5; i++) vol[i] = -1;
232 // Get the information for this digit
233 AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
235 Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
236 // for current sdigit
238 // TOF sdigit volumes (always the same for all slots)
239 Int_t sector = tofsdigit->GetSector(); // range [0-17]
240 Int_t plate = tofsdigit->GetPlate(); // range [0- 4]
241 Int_t strip = tofsdigit->GetStrip(); // range [0-14/18/19]
242 Int_t padz = tofsdigit->GetPadz(); // range [0- 1]
243 Int_t padx = tofsdigit->GetPadx(); // range [0-47]
251 //--------------------- QA section ----------------------
252 // in the while, I perform QA
253 Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
256 AliFatal(Form("strange sdigit found %2d %1d %2d %1d %2d", sector, plate, strip, padz, padx));
257 //-------------------------------------------------------
259 //------------------- Dump section ----------------------
261 AliInfo(Form("%2d-th digit: Sector %2d | Plate %1d | Strip %2d | PadZ %1d | PadX %2d ", k, sector, plate, strip, padz, padx));
262 AliInfo("----------------------------------------------------");
264 // ------------------------------------------------------
266 // start loop on number of slots for current sdigit
267 for (Int_t islot = 0; islot < nslot; islot++) {
268 for (Int_t aa=0; aa<4; aa++) digit[aa] = 0; // TOF digit variables
269 for (Int_t aa=0; aa<AliTOFSDigit::kMAXDIGITS; aa++) tracknum[aa] = -1;
271 Int_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
272 Int_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
274 //if (tdc>=8192) continue;//AdC
276 tracknum[0]=tofsdigit->GetTrack(islot,0);
277 tracknum[1]=tofsdigit->GetTrack(islot,1);
278 tracknum[2]=tofsdigit->GetTrack(islot,2);
280 // new with placement must be used
281 // adding a TOF digit for each slot
282 TClonesArray &aDigits = *fDigits;
283 Int_t last=fDigits->GetEntriesFast();
284 new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
288 } // end loop on sdigits - end digitizing all collected sdigits
290 //Insert Decalibration
291 AliDebug(2,"in digitizer, create digits");
292 DecalibrateTOFSignal();
295 //---------------------------------------------------------------------
297 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
299 // Read sdigits for current event and inputFile;
300 // store them into the sdigits container
301 // and update the hit map
302 // SDigits from different files are assumed to
303 // be created with the same simulation parameters.
305 // creating the TClonesArray to store the digits
306 static TClonesArray sdigitsClonesArray("AliTOFSDigit", 1000);
307 sdigitsClonesArray.Clear();
309 // get the treeS from manager
310 AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
313 AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
317 AliLoader* gime = rl->GetLoader("TOFLoader");
320 AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
324 TTree* currentTreeS=gime->TreeS();
325 if (currentTreeS == 0x0)
327 Int_t retval = gime->LoadSDigits();
330 AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
333 currentTreeS=gime->TreeS();
334 if (currentTreeS == 0x0)
336 AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
340 // get the branch TOF inside the treeS
341 TClonesArray * sdigitsDummyContainer=&sdigitsClonesArray;
342 // check if the branch exist
343 TBranch* tofBranch=currentTreeS->GetBranch("TOF");
346 AliFatal(Form("TOF branch not found for input %d",inputFile));
349 tofBranch->SetAddress(&sdigitsDummyContainer);
351 Int_t nEntries = (Int_t)tofBranch->GetEntries();
353 // Loop through all entries in the tree
356 Int_t vol[5]; // location for a sdigit
357 for (Int_t i=0; i<5; i++) vol[i] = -1;
359 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
362 nbytes += tofBranch->GetEvent(iEntry);
364 // Get the number of sdigits
365 Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
367 for (Int_t k=0; k<ndig; k++) {
368 AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
370 for (Int_t i=0; i<5; i++) vol[i] = -1;
372 // check the sdigit volume
373 vol[0] = tofSdigit->GetSector();
374 vol[1] = tofSdigit->GetPlate();
375 vol[2] = tofSdigit->GetStrip();
376 vol[3] = tofSdigit->GetPadx();
377 vol[4] = tofSdigit->GetPadz();
379 if (fhitMap->TestHit(vol) != kEmpty) {
380 AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
381 sdig->Update(tofSdigit);
385 CollectSDigit(tofSdigit); // collect the current sdigit
386 fhitMap->SetHit(vol); // update the hitmap for location vol
388 } // if (hitMap->TestHit(vol) != kEmpty)
390 } // for (Int_t k=0; k<ndig; k++)
392 } // end loop on entries
397 //_____________________________________________________________________________
398 void AliTOFDigitizer::CollectSDigit(const AliTOFSDigit * const sdigit)
401 // Add a TOF sdigit in container
402 // new with placement must be used
403 TClonesArray &aSDigitsArray = *fSDigitsArray;
404 Int_t last=fSDigitsArray->GetEntriesFast();
405 // make a copy of the current sdigit and
406 // put it into tmp array
407 new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
410 //_____________________________________________________________________________
411 void AliTOFDigitizer::InitDecalibration() const {
413 // Initialize TOF digits decalibration
418 fCalib->CreateCalArrays();
419 fCalib->ReadSimHistoFromCDB("TOF/Calib", -1); // use AliCDBManager's number
420 fCalib->ReadParOfflineFromCDB("TOF/Calib", -1); // use AliCDBManager's number
423 //---------------------------------------------------------------------
424 void AliTOFDigitizer::DecalibrateTOFSignal() {
426 // Decalibrate TOF signals according to OCDB parameters
429 Double_t time=0., tot=0., corr=0.;
430 Int_t deltaBC=0, l0l1=0, tdcBin=0;
432 Int_t detId[5] ={-1,-1,-1,-1,-1};
435 Int_t ndigits = fDigits->GetEntriesFast();
436 // Loop on TOF Digits
437 for (Int_t i=0;i<ndigits;i++){
438 AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
439 detId[0] = dig->GetSector();
440 detId[1] = dig->GetPlate();
441 detId[2] = dig->GetStrip();
442 detId[3] = dig->GetPadz();
443 detId[4] = dig->GetPadx();
444 dig->SetTdcND(dig->GetTdc()); // save the non decalibrated time
446 index = AliTOFGeometry::GetIndex(detId); // The channel index
448 // Read Calibration parameters from the CDB
450 time = dig->GetTdc() * AliTOFGeometry::TdcBinWidth(); /* ps */
451 tot = dig->GetToT() * AliTOFGeometry::ToTBinWidth() * 1.e-3; /* ns */
452 deltaBC = 0;//dig->GetDeltaBC();
453 l0l1 = 0;//dig->GetL0L1Latency();
456 corr = fCalib->GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp); /* ps */
457 AliDebug(2, Form("calibrate index %d: time=%f (ps) tot=%f (ns) deltaBC=%d l0l1=%d timestamp=%d corr=%f (ps)",
458 index, time, tot, deltaBC, l0l1, timestamp, corr));
460 // apply time correction
463 // convert in TDC bins and set digit
464 //tdcBin = (Int_t)(time / AliTOFGeometry::TdcBinWidth()); //the corrected time (tdc counts)
465 tdcBin = TMath::Nint(time / AliTOFGeometry::TdcBinWidth()); //the corrected time (tdc counts)
470 AliDebug(1,"Simulating miscalibrated digits");
475 //---------------------------------------------------------------------
477 void AliTOFDigitizer::DecalibrateTOFSignal(){ // Old implementation
479 // Read Calibration parameters from the CDB
481 TObjArray * calOffline= fCalib->GetTOFCalArrayOffline();
483 AliDebug(2,Form("Size of array for Offline Calibration = %i",calOffline->GetEntries()));
485 // Initialize Quantities to Simulate ToT Spectra
487 TH1F * hToT= fCalib->GetTOFSimToT();
488 Int_t nbins = hToT->GetNbinsX();
489 Float_t delta = hToT->GetBinWidth(1);
490 Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
491 Float_t minch = hToT->GetBinLowEdge(1);
492 Float_t max=0,min=0; //maximum and minimum value of the distribution
493 Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
495 for (Int_t ii=nbins; ii>0; ii--){
496 if (hToT->GetBinContent(ii)!= 0) {
497 max = maxch - (nbins-ii-1)*delta;
501 for (Int_t j=1; j<nbins; j++){
502 if (hToT->GetBinContent(j)!= 0) {
503 min = minch + (j-1)*delta;
511 Float_t maxToTDistr=hToT->GetMaximum();
513 AliDebug (1, Form(" The minimum ToT = %f", minToT));
514 AliDebug (1, Form(" The maximum ToT = %f", maxToT));
515 AliDebug (1, Form(" The maximum peak in ToT = %f", maxToTDistr));
517 // Loop on TOF Digits
519 Bool_t isToTSimulated=kFALSE;
520 Bool_t misCalibPars=kFALSE;
521 if(hToT->GetEntries()>0)isToTSimulated=kTRUE;
522 Int_t ndigits = fDigits->GetEntriesFast();
523 for (Int_t i=0;i<ndigits;i++){
524 AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
526 detId[0] = dig->GetSector();
527 detId[1] = dig->GetPlate();
528 detId[2] = dig->GetStrip();
529 detId[3] = dig->GetPadz();
530 detId[4] = dig->GetPadx();
531 dig->SetTdcND(dig->GetTdc()); // save the non decalibrated time
534 //A realistic ToT Spectrum was found in input,
535 //decalibrated TOF Digits likely to be simulated....
537 Int_t index = AliTOFGeometry::GetIndex(detId); // The channel index
538 AliTOFChannelOffline *calChannelOffline = (AliTOFChannelOffline *)calOffline->At(index); //retrieve the info for time slewing
539 Double_t par[6]; // time slewing parameters
541 //check whether we actually ask for miscalibration
543 for (Int_t j = 0; j<6; j++){
544 par[j]=(Double_t)calChannelOffline->GetSlewPar(j);
545 if(par[j]!=0)misCalibPars=kTRUE;
547 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]));
549 // Now generate Realistic ToT distribution from TestBeam Data.
550 // Tot is in ns, assuming a Matching Window of 10 ns.
557 while (simToT <= triy){
558 trix = gRandom->Rndm(i);
559 triy = gRandom->Rndm(i);
560 trix = (maxToT-minToT)*trix + minToT;
561 triy = maxToTDistr*triy;
562 Int_t binx=hToT->FindBin(trix);
563 simToT=hToT->GetBinContent(binx);
565 // the generated ToT (ns)
566 tToT= (Double_t) trix; // to apply slewing we start from ns..
567 // transform TOF signal in ns
568 AliDebug(2,Form(" The Initial Time (counts): %i: ",dig->GetTdc()));
569 AliDebug(2,Form(" Time before miscalibration (ps) %e: ",dig->GetTdc()*(Double_t)AliTOFGeometry::TdcBinWidth()));
570 // add slewing effect
571 timeCorr=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5]))));
572 AliDebug(2,Form(" The Time slewing + delay (ns): %f: ",timeCorr));
573 // add global time shift
576 Double_t timeMis = (Double_t)(dig->GetTdc())*(Double_t)AliTOFGeometry::TdcBinWidth();
577 timeMis = timeMis+timeCorr;
578 AliDebug(2,Form(" The Miscalibrated time (ps): %e: ",timeMis));
580 // now update the digit info
582 Int_t tdcCorr= (Int_t)(timeMis/AliTOFGeometry::TdcBinWidth());
583 AliDebug(2,Form(" Final Time (counts): %i: ",tdcCorr));
584 // Setting Decalibrated Time signal (TDC counts)
585 dig->SetTdc(tdcCorr);
586 // Setting realistic ToT signal (TDC counts)
587 tToT*=1E3; //back to ps
588 Int_t tot=(Int_t)(tToT/AliTOFGeometry::ToTBinWidth());//(factor 1E3 as input ToT is in ns)
590 AliDebug(2,Form(" Final Time and ToT (counts): %d: , %d:",dig->GetTdc(),dig->GetToT()));
592 AliWarning (Form(" The bad Slewed Time(TDC counts)= %d ", tdcCorr));
593 AliWarning(Form(" The bad ToT (TDC counts)= %d ", tot));
597 // For Data with no Miscalibration, set ToT signal == Adc
598 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)
603 AliDebug(1,"Standard Production, no miscalibrated digits");
606 AliDebug(1,"Standard Production, no miscalibrated digits");
608 AliDebug(1,"Simulating miscalibrated digits");