1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
19 #include "AliMUONDigitizerV3.h"
21 #include "AliCDBManager.h"
24 #include "AliMUONCalibrationData.h"
25 #include "AliMUONConstants.h"
26 #include "AliMUONDigit.h"
27 #include "AliMUONLogger.h"
28 #include "AliMUONTriggerEfficiencyCells.h"
29 #include "AliMUONTriggerElectronics.h"
30 #include "AliMUONTriggerStoreV1.h"
31 #include "AliMUONVCalibParam.h"
32 #include "AliMUONVDigitStore.h"
33 #include "AliMpCathodType.h"
34 #include "AliMpConstants.h"
35 #include "AliMpDEIterator.h"
36 #include "AliMpDEManager.h"
37 #include "AliMpDEManager.h"
38 #include "AliMpIntPair.h"
40 #include "AliMpSegmentation.h"
41 #include "AliMpStationType.h"
42 #include "AliMpVSegmentation.h"
44 #include "AliRunDigitizer.h"
45 #include "AliRunLoader.h"
46 #include <Riostream.h>
55 /// \class AliMUONDigitizerV3
56 /// The digitizer is performing the transformation to go from SDigits (digits
57 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
59 /// The decalibration is performed by doing the reverse operation of the
60 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
62 /// Note also that the digitizer takes care of merging sdigits that belongs
63 /// to the same pad, either because we're merging several input sdigit files
64 /// or with a single file because the sdigitizer does not merge sdigits itself
65 /// (for performance reason mainly, and because anyway we know we have to do it
66 /// here, at the digitization level).
68 /// \author Laurent Aphecetche
74 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
78 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
81 ClassImp(AliMUONDigitizerV3)
84 //_____________________________________________________________________________
85 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
86 Bool_t generateNoisyDigits)
87 : AliDigitizer(manager),
88 fIsInitialized(kFALSE),
89 fCalibrationData(0x0),
90 fTriggerProcessor(0x0),
91 fTriggerEfficiency(0x0),
92 fGenerateNoisyDigitsTimer(),
95 fGenerateNoisyDigits(generateNoisyDigits),
96 fLogger(new AliMUONLogger(1000)),
97 fTriggerStore(new AliMUONTriggerStoreV1),
99 fOutputDigitStore(0x0)
103 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
104 fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
105 fExecTimer.Start(kTRUE); fExecTimer.Stop();
108 //_____________________________________________________________________________
109 AliMUONDigitizerV3::~AliMUONDigitizerV3()
111 /// Dtor. Note we're the owner of some pointers.
115 delete fCalibrationData;
116 delete fTriggerProcessor;
117 delete fNoiseFunction;
118 delete fTriggerStore;
120 delete fOutputDigitStore;
122 if ( fGenerateNoisyDigits )
124 AliDebug(1, Form("Execution time for GenerateNoisyDigits() : R:%.2fs C:%.2fs",
125 fGenerateNoisyDigitsTimer.RealTime(),
126 fGenerateNoisyDigitsTimer.CpuTime()));
128 AliDebug(1, Form("Execution time for Exec() : R:%.2fs C:%.2fs",
129 fExecTimer.RealTime(),fExecTimer.CpuTime()));
131 AliInfo("Summary of messages");
137 //_____________________________________________________________________________
139 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
141 /// For tracking digits, starting from an ideal digit's charge, we :
143 /// - add some noise (thus leading to a realistic charge), if requested to do so
144 /// - divide by a gain (thus decalibrating the digit)
145 /// - add a pedestal (thus decalibrating the digit)
146 /// - sets the signal to zero if below 3*sigma of the noise
148 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
150 Float_t signal = digit.Charge();
154 digit.SetADC(TMath::Min(kMaxADC,TMath::Nint(signal)));
158 Int_t detElemId = digit.DetElemId();
160 Int_t manuId = digit.ManuId();
161 Int_t manuChannel = digit.ManuChannel();
163 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
166 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
173 Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
174 Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
176 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
179 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
186 Float_t gainMean = gain->ValueAsFloat(manuChannel,0);
188 Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
192 if ( gainMean < 1E-6 )
194 AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. "
195 "Setting signal to 0.",
196 gainMean,detElemId,manuId,manuChannel));
201 adc = TMath::Nint( signal / gainMean + pedestalMean + adcNoise);///
203 if ( adc <= pedestalMean + fgkNSigmas*pedestalSigma )
209 // be sure we stick to 12 bits.
215 digit.SetCharge(adc);
219 //_____________________________________________________________________________
221 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
222 AliMUONVDigit& digit)
224 /// \todo add comment
226 if ( !fTriggerEfficiency ) return;
228 if (digit.IsEfficiencyApplied()) return;
230 AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
232 if (!correspondingDigit) return; //reject bad correspondences
234 Int_t detElemId = digit.DetElemId();
236 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
237 const AliMpVSegmentation* segment[2] =
239 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())),
240 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
245 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
246 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
250 if (digit.Cathode()==0) p0=0;
252 AliMpIntPair location = pad[p0].GetLocation(0);
253 Int_t nboard = location.GetFirst();
257 fTriggerEfficiency->IsTriggered(detElemId, nboard-1,
258 isTrig[0], isTrig[1]);
259 digit.EfficiencyApplied(kTRUE);
260 correspondingDigit->EfficiencyApplied(kTRUE);
262 if (!isTrig[digit.Cathode()])
267 if ( &digit != correspondingDigit )
269 if (!isTrig[correspondingDigit->Cathode()])
271 correspondingDigit->SetCharge(0);
276 //_____________________________________________________________________________
278 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
279 AliMUONVDigitStore& filteredStore)
281 /// Loop over all chamber digits, and apply the response to them
282 /// Note that this method may remove digits.
284 filteredStore.Clear();
286 const Bool_t kAddNoise = kTRUE;
288 TIter next(store.CreateIterator());
289 AliMUONVDigit* digit;
291 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
293 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
295 if ( stationType != AliMp::kStationTrigger )
297 ApplyResponseToTrackerDigit(*digit,kAddNoise);
301 ApplyResponseToTriggerDigit(store,*digit);
303 if ( digit->Charge() > 0 )
305 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
310 //_____________________________________________________________________________
312 AliMUONDigitizerV3::Exec(Option_t*)
315 /// We first loop over input files, and merge the sdigits we found there.
316 /// Second, we digitize all the resulting sdigits
317 /// Then we generate noise-only digits (for tracker only)
318 /// And we finally generate the trigger outputs.
320 AliDebug(1, "Running digitizer.");
322 if ( fManager->GetNinputs() == 0 )
324 AliWarning("No input set. Nothing to do.");
328 if ( !fIsInitialized )
330 AliError("Not initialized. Cannot perform the work. Sorry");
334 fExecTimer.Start(kFALSE);
336 Int_t nInputFiles = fManager->GetNinputs();
338 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
340 outputLoader->MakeDigitsContainer();
342 TTree* oTreeD = outputLoader->TreeD();
346 AliFatal("Cannot create output TreeD");
349 // Loop over all the input files, and merge the sdigits found in those
352 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
354 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
356 inputLoader->LoadSDigits("READ");
358 TTree* iTreeS = inputLoader->TreeS();
361 AliFatal(Form("Could not get access to input file #%d",iFile));
364 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
365 inputStore->Connect(*iTreeS);
369 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
371 inputLoader->UnloadSDigits();
376 // At this point, we do have digit arrays (one per chamber) which contains
377 // the merging of all the sdigits of the input file(s).
378 // We now massage them to apply the detector response, i.e. this
379 // is here that we do the "digitization" work.
381 if (!fOutputDigitStore)
383 fOutputDigitStore = fDigitStore->Create();
386 ApplyResponse(*fDigitStore,*fOutputDigitStore);
388 if ( fGenerateNoisyDigits )
390 // Generate noise-only digits for tracker.
391 GenerateNoisyDigits(*fOutputDigitStore);
394 // We generate the global and local trigger decisions.
395 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
397 // Prepare output tree
398 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
399 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
402 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
406 // Fill the output treeD
409 // Write to the output tree(D).
410 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
411 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
413 outputLoader->WriteDigits("OVERWRITE");
415 outputLoader->UnloadDigits();
417 // Finally, we clean up after ourselves.
418 fTriggerStore->Clear();
419 fDigitStore->Clear();
420 fOutputDigitStore->Clear();
424 //_____________________________________________________________________________
426 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
427 AliMUONVDigit& digit) const
429 /// Find, if it exists, the digit corresponding to digit.Hit(), in the
432 TIter next(digitStore.CreateIterator());
435 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
437 if ( d->DetElemId() == digit.DetElemId() &&
438 d->Hit() == digit.Hit() &&
439 d->Cathode() != digit.Cathode() )
448 //_____________________________________________________________________________
450 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
452 /// According to a given probability, generate digits that
453 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
454 /// that are "only noise".
456 if ( !fNoiseFunction )
458 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
459 fgkNSigmas,fgkNSigmas*10);
461 fNoiseFunction->SetParameters(1,0,1);
464 fGenerateNoisyDigitsTimer.Start(kFALSE);
466 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
472 while ( !it.IsDone() )
474 for ( Int_t cathode = 0; cathode < 2; ++cathode )
476 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
482 fGenerateNoisyDigitsTimer.Stop();
485 //_____________________________________________________________________________
487 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
488 Int_t detElemId, Int_t cathode)
490 /// Generate noise-only digits for one cathode of one detection element.
491 /// Called by GenerateNoisyDigits()
493 const AliMpVSegmentation* seg
494 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
495 Int_t nofPads = seg->NofPads();
497 Int_t maxIx = seg->MaxPadIndexX();
498 Int_t maxIy = seg->MaxPadIndexY();
500 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
502 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
503 if ( !nofNoisyPads ) return;
506 TMath::Nint(gRandom->Gaus(nofNoisyPads,
507 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
509 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
511 for ( Int_t i = 0; i < nofNoisyPads; ++i )
518 ix = gRandom->Integer(maxIx+1);
519 iy = gRandom->Integer(maxIy+1);
520 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
521 } while ( !pad.IsValid() );
523 Int_t manuId = pad.GetLocation().GetFirst();
524 Int_t manuChannel = pad.GetLocation().GetSecond();
526 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
530 // no pedestal available for this channel, simply give up
534 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
538 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
539 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
541 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
543 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
545 ApplyResponseToTrackerDigit(*d,kFALSE);
546 if ( d->Charge() > 0 )
548 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
549 // this can happen (that we randomly chose a digit that is
550 // already there). We simply ignore this, but log the occurence
551 // to cross-check that it's not too frequent.
554 fLogger->Log("Collision while adding noiseOnly digit");
558 fLogger->Log("Added noiseOnly digit");
563 AliError("Pure noise below threshold. This should not happen. Not adding "
570 //_____________________________________________________________________________
572 AliMUONDigitizerV3::GetLoader(const TString& folderName)
574 /// Get a MUON loader
576 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
577 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
580 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
586 //_____________________________________________________________________________
588 AliMUONDigitizerV3::Init()
590 /// Initialization of the TTask :
591 /// a) create the calibrationData, according to run number
592 /// b) create the trigger processing task
596 if ( fIsInitialized )
598 AliError("Object already initialized.");
604 AliError("fManager is null !");
608 Int_t runnumber = AliCDBManager::Instance()->GetRun();
610 fCalibrationData = new AliMUONCalibrationData(runnumber);
611 if ( !fCalibrationData->Pedestals() )
613 AliFatal("Could not access pedestals from OCDB !");
615 if ( !fCalibrationData->Gains() )
617 AliFatal("Could not access gains from OCDB !");
619 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
621 if ( muon()->GetTriggerEffCells() )
623 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
624 if ( fTriggerEfficiency )
626 AliDebug(1, "Will apply trigger efficiency");
630 AliFatal("I was requested to apply trigger efficiency, but I could "
635 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
636 (fGenerateNoisyDigits ? "":"NOT")));
638 fIsInitialized = kTRUE;
642 //_____________________________________________________________________________
644 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
645 const AliMUONVDigitStore& input,
648 /// Merge the sdigits in inputData with the digits already present in outputData
650 if ( !outputStore ) outputStore = input.Create();
652 TIter next(input.CreateIterator());
653 AliMUONVDigit* sdigit;
655 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
657 // Update the track references using the mask.
658 // FIXME: this is dirty, for backward compatibility only.
659 // Should re-design all this way of keeping track of MC information...
660 if ( mask ) sdigit->PatchTracks(mask);
661 // Then add or update the digit to the output.
662 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
665 AliError("Could not add digit in merge mode");