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"
22 #include "AliMUONCalibrationData.h"
23 #include "AliCDBManager.h"
24 #include "AliMUONConstants.h"
25 #include "AliMUONData.h"
26 #include "AliMUONDataIterator.h"
27 #include "AliMUONDigit.h"
28 #include "AliMUONLogger.h"
29 #include "AliMUONSegmentation.h"
30 #include "AliMUONTriggerEfficiencyCells.h"
31 #include "AliMUONTriggerElectronics.h"
32 #include "AliMUONVCalibParam.h"
34 #include "AliMpDEIterator.h"
35 #include "AliMpDEManager.h"
36 #include "AliMpIntPair.h"
38 #include "AliMpStationType.h"
39 #include "AliMpSegmentation.h"
40 #include "AliMpVSegmentation.h"
41 #include "AliMpDEManager.h"
42 #include "AliMpCathodType.h"
45 #include "AliRunDigitizer.h"
46 #include "AliRunLoader.h"
49 #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),
90 fCalibrationData(0x0),
91 fTriggerProcessor(0x0),
92 fTriggerEfficiency(0x0),
93 fFindDigitIndexTimer(),
94 fGenerateNoisyDigitsTimer(),
97 fGenerateNoisyDigits(generateNoisyDigits),
98 fLogger(new AliMUONLogger(1000))
102 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
103 fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
104 fExecTimer.Start(kTRUE); fExecTimer.Stop();
105 fFindDigitIndexTimer.Start(kTRUE); fFindDigitIndexTimer.Stop();
108 //_____________________________________________________________________________
109 AliMUONDigitizerV3::~AliMUONDigitizerV3()
111 /// Dtor. Note we're the owner of some pointers.
116 delete fCalibrationData;
117 delete fTriggerProcessor;
118 delete fNoiseFunction;
120 AliDebug(1, Form("Execution time for FindDigitIndex() : R:%.2fs C:%.2fs",
121 fFindDigitIndexTimer.RealTime(),fFindDigitIndexTimer.CpuTime()));
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(AliMUONDigit& 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.Signal();
154 digit.SetADC(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.",
169 digit.SetPhysicsSignal(0);
174 Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
175 Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
177 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
180 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
183 digit.SetPhysicsSignal(0);
188 Float_t gainMean = gain->ValueAsFloat(manuChannel,0);
190 Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
194 if ( gainMean < 1E-6 )
196 AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. "
197 "Setting signal to 0.",
198 gainMean,detElemId,manuId,manuChannel));
203 adc = TMath::Nint( signal / gainMean + pedestalMean + adcNoise);///
205 if ( adc <= pedestalMean + fgkNSigmas*pedestalSigma )
211 // be sure we stick to 12 bits.
217 digit.SetPhysicsSignal(TMath::Nint(signal));
218 digit.SetSignal(adc);
222 //_____________________________________________________________________________
224 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(AliMUONDigit& digit)
226 /// \todo add comment
228 if ( !fTriggerEfficiency ) return;
230 if (digit.IsEfficiencyApplied()) return;
232 AliMUONDigit* correspondingDigit = FindCorrespondingDigit(digit);
234 if (!correspondingDigit) return; //reject bad correspondences
236 Int_t detElemId = digit.DetElemId();
238 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
239 const AliMpVSegmentation* segment[2] =
241 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())),
242 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
247 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
248 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
252 if (digit.Cathode()==0) p0=0;
254 AliMpIntPair location = pad[p0].GetLocation(0);
255 Int_t nboard = location.GetFirst();
259 fTriggerEfficiency->IsTriggered(detElemId, nboard-1,
260 isTrig[0], isTrig[1]);
261 digit.EfficiencyApplied(kTRUE);
262 correspondingDigit->EfficiencyApplied(kTRUE);
264 if (!isTrig[digit.Cathode()])
269 if ( &digit != correspondingDigit )
271 if (!isTrig[correspondingDigit->Cathode()])
273 correspondingDigit->SetSignal(0);
278 //_____________________________________________________________________________
280 AliMUONDigitizerV3::ApplyResponse()
282 /// Loop over all chamber digits, and apply the response to them
283 /// Note that this method may remove digits.
285 const Bool_t kAddNoise = kTRUE;
287 for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich )
289 TClonesArray* digits = fOutputData->Digits(ich);
290 Int_t n = digits->GetEntriesFast();
291 Bool_t trackingChamber = ( ich < AliMUONConstants::NTrackingCh() );
292 for ( Int_t i = 0; i < n; ++i )
294 AliMUONDigit* d = static_cast<AliMUONDigit*>(digits->UncheckedAt(i));
295 if ( !d ) continue; // that digit might have been removed
296 if ( trackingChamber )
298 ApplyResponseToTrackerDigit(*d,kAddNoise);
302 ApplyResponseToTriggerDigit(*d);
304 if ( d->Signal() <= 0 )
309 digits->Compress(); // only do the compress at the end in order not to
310 // change the n = digits->GetEntriesFast()
313 // The version below, using iterator, does not yet work (as the iterator
314 // assumes it is reading digits from the tree, while in this case it's
317 // AliMUONDigit* digit(0x0);
319 // // First loop on tracker digits
320 // AliMUONDataIterator tracker(fOutputData,"D",AliMUONDataIterator::kTrackingChambers);
322 // while ( ( digit = static_cast<AliMUONDigit*>(tracker.Next()) ) )
324 // ApplyResponseToTrackerDigit(*digit);
325 // if ( digit->Signal() <= 0 )
332 // // Then loop on trigger digits
333 // AliMUONDataIterator trigger(fOutputData,"D",AliMUONDataIterator::kTriggerChambers);
335 // while ( ( digit = static_cast<AliMUONDigit*>(trigger.Next()) ) )
337 // ApplyResponseToTriggerDigit(*digit,fOutputData);
338 // if ( digit->Signal() <= 0 )
345 //_____________________________________________________________________________
347 AliMUONDigitizerV3::AddOrUpdateDigit(TClonesArray& array,
348 const AliMUONDigit& digit)
350 /// Add or update a digit, depending on whether there's already a digit
351 /// for the corresponding channel.
353 Int_t ix = FindDigitIndex(array,digit);
357 AliMUONDigit* d = static_cast<AliMUONDigit*>(array.UncheckedAt(ix));
358 Bool_t ok = MergeDigits(digit,*d);
361 AliError("Digits are not mergeable !");
366 ix = array.GetLast() + 1;
367 new(array[ix]) AliMUONDigit(digit);
372 //_____________________________________________________________________________
374 AliMUONDigitizerV3::Exec(Option_t*)
377 /// We first loop over input files, and merge the sdigits we found there.
378 /// Second, we digitize all the resulting sdigits
379 /// Then we generate noise-only digits (for tracker only)
380 /// And we finally generate the trigger outputs.
382 AliDebug(1, "Running digitizer.");
384 if ( fManager->GetNinputs() == 0 )
386 AliWarning("No input set. Nothing to do.");
390 if ( !fIsInitialized )
392 AliError("Not initialized. Cannot perform the work. Sorry");
396 fExecTimer.Start(kFALSE);
398 Int_t nInputFiles = fManager->GetNinputs();
400 if ( fOutputData->TreeD() == 0x0 )
402 AliDebug(2,"Calling MakeDigitsContainer");
403 fOutputData->GetLoader()->MakeDigitsContainer();
405 fOutputData->MakeBranch("D,GLT");
406 fOutputData->SetTreeAddress("D,GLT");
408 // Loop over all the input files, and merge the sdigits found in those
410 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
412 AliMUONData* inputData = GetDataAccess(fManager->GetInputFolderName(iFile));
415 AliFatal(Form("Could not get access to input file #%d",iFile));
418 inputData->GetLoader()->LoadSDigits("READ");
419 inputData->SetTreeAddress("S");
420 inputData->GetSDigits();
422 MergeWithSDigits(*fOutputData,*inputData,fManager->GetMask(iFile));
424 inputData->ResetSDigits();
425 inputData->GetLoader()->UnloadSDigits();
429 // At this point, we do have digit arrays (one per chamber) which contains
430 // the merging of all the sdigits of the input file(s).
431 // We now massage them to apply the detector response, i.e. this
432 // is here that we do the "digitization" work.
436 if ( fGenerateNoisyDigits )
438 // Generate noise-only digits for tracker.
439 GenerateNoisyDigits();
442 // We generate the global and local trigger decisions.
443 fTriggerProcessor->ExecuteTask();
445 // Fill the output treeD
446 fOutputData->Fill("D,GLT");
448 // Write to the output tree(D).
449 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
450 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
452 fOutputData->GetLoader()->WriteDigits("OVERWRITE");
454 // Finally, we clean up after ourselves.
455 fOutputData->ResetDigits();
456 fOutputData->ResetTrigger();
457 fOutputData->GetLoader()->UnloadDigits();
462 //_____________________________________________________________________________
464 AliMUONDigitizerV3::FindCorrespondingDigit(AliMUONDigit& digit) const
466 /// Find, if it exists, the digit corresponding to digit.Hit(), in the
469 // Iterator does not yet work when writing digits (only works when reading,
470 // which is not the case here)
472 // AliMUONDataIterator it(data,"D",AliMUONDataIterator::kTriggerChambers);
475 // while ( ( cd = static_cast<AliMUONDigit*>(it.Next()) ) )
477 // if ( cd->DetElemId() == digit.DetElemId() &&
478 // cd->Hit() == digit.Hit() &&
479 // cd->Cathode() != digit.Cathode() )
485 Int_t ich = AliMpDEManager::GetChamberId(digit.DetElemId());
486 TClonesArray* digits = fOutputData->Digits(ich);
487 Int_t n = digits->GetEntriesFast();
488 for ( Int_t i = 0; i < n; ++i )
490 AliMUONDigit* d = static_cast<AliMUONDigit*>(digits->UncheckedAt(i));
492 d->DetElemId() == digit.DetElemId() &&
493 d->Hit() == digit.Hit() &&
494 d->Cathode() != digit.Cathode() )
504 //_____________________________________________________________________________
506 AliMUONDigitizerV3::FindDigitIndex(TClonesArray& array,
507 const AliMUONDigit& digit) const
509 /// Return the index of digit within array, if that digit is there,
510 /// otherwise returns -1
512 /// \todo FIXME: this is of course not the best implementation you can think of.
513 /// Reconsider the use of hit/digit map... ? (but be sure it's needed!)
515 fFindDigitIndexTimer.Start(kFALSE);
517 Int_t n = array.GetEntriesFast();
518 for ( Int_t i = 0; i < n; ++i )
520 AliMUONDigit* d = static_cast<AliMUONDigit*>(array.UncheckedAt(i));
521 if ( d->DetElemId() == digit.DetElemId() &&
522 d->PadX() == digit.PadX() &&
523 d->PadY() == digit.PadY() &&
524 d->Cathode() == digit.Cathode() )
526 fFindDigitIndexTimer.Stop();
530 fFindDigitIndexTimer.Stop();
534 //_____________________________________________________________________________
536 AliMUONDigitizerV3::GenerateNoisyDigits()
538 /// According to a given probability, generate digits that
539 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
540 /// that are "only noise".
542 if ( !fNoiseFunction )
544 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
545 fgkNSigmas,fgkNSigmas*10);
547 fNoiseFunction->SetParameters(1,0,1);
550 fGenerateNoisyDigitsTimer.Start(kFALSE);
552 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
558 while ( !it.IsDone() )
560 for ( Int_t cathode = 0; cathode < 2; ++cathode )
562 GenerateNoisyDigitsForOneCathode(it.CurrentDEId(),cathode);
568 fGenerateNoisyDigitsTimer.Stop();
571 //_____________________________________________________________________________
573 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(Int_t detElemId, Int_t cathode)
575 /// Generate noise-only digits for one cathode of one detection element.
576 /// Called by GenerateNoisyDigits()
578 Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
579 TClonesArray* digits = fOutputData->Digits(chamberId);
581 const AliMpVSegmentation* seg
582 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
583 Int_t nofPads = seg->NofPads();
585 Int_t maxIx = seg->MaxPadIndexX();
586 Int_t maxIy = seg->MaxPadIndexY();
588 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
590 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
591 if ( !nofNoisyPads ) return;
594 TMath::Nint(gRandom->Gaus(nofNoisyPads,
595 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
597 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
599 for ( Int_t i = 0; i < nofNoisyPads; ++i )
604 ix = gRandom->Integer(maxIx+1);
605 iy = gRandom->Integer(maxIy+1);
606 } while ( !seg->HasPad(AliMpIntPair(ix,iy)) );
608 d.SetDetElemId(detElemId);
609 d.SetCathode(cathode);
612 if ( FindDigitIndex(*digits,d) >= 0 )
614 // this digit is already there, and not noise-only, we simply skip it
617 AliMpPad pad = seg->PadByIndices(AliMpIntPair(ix,iy));
618 Int_t manuId = pad.GetLocation().GetFirst();
619 Int_t manuChannel = pad.GetLocation().GetSecond();
621 d.SetElectronics(manuId,manuChannel);
623 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
627 // no pedestal available for this channel, simply give up
631 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
632 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
634 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
636 d.SetSignal(TMath::Nint(ped+pedestalMean+0.5));
637 d.SetPhysicsSignal(0);
639 AliDebug(3,Form("Adding a pure noise digit :"));
640 // StdoutToAliDebug(3,cout << "Before Response: " << endl;
642 ApplyResponseToTrackerDigit(d,kFALSE);
643 if ( d.Signal() > 0 )
645 AddOrUpdateDigit(*digits,d);
649 AliError("Pure noise below threshold. This should not happen. Not adding "
652 // StdoutToAliDebug(3,cout << "After Response: " << endl;
657 //_____________________________________________________________________________
659 AliMUONDigitizerV3::GetDataAccess(const TString& folderName)
661 /// Create an AliMUONData to deal with data found in folderName.
663 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
664 AliRunLoader* runLoader = AliRunLoader::GetRunLoader(folderName);
667 AliError(Form("Could not get RunLoader from folder %s",folderName.Data()));
670 AliLoader* loader = static_cast<AliLoader*>(runLoader->GetLoader("MUONLoader"));
673 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
676 AliMUONData* data = new AliMUONData(loader,"MUON","MUONDataForDigitOutput");
677 AliDebug(2,Form("AliMUONData=%p loader=%p",data,loader));
681 //_____________________________________________________________________________
683 AliMUONDigitizerV3::Init()
685 /// Initialization of the TTask :
686 /// a) set the outputData pointer
687 /// b) create the calibrationData, according to run number
688 /// c) create the trigger processing task
692 if ( fIsInitialized )
694 AliError("Object already initialized.");
700 AliError("fManager is null !");
704 fOutputData = GetDataAccess(fManager->GetOutputFolderName());
707 AliError("Can not perform digitization. I'm sorry");
710 AliDebug(2,Form("fOutputData=%p",fOutputData));
712 Int_t runnumber = AliCDBManager::Instance()->GetRun();
714 fCalibrationData = new AliMUONCalibrationData(runnumber);
715 if ( !fCalibrationData->Pedestals() )
717 AliFatal("Could not access pedestals from OCDB !");
719 if ( !fCalibrationData->Gains() )
721 AliFatal("Could not access gains from OCDB !");
723 fTriggerProcessor = new AliMUONTriggerElectronics(fOutputData,fCalibrationData);
725 if ( muon()->GetTriggerEffCells() )
727 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
728 if ( fTriggerEfficiency )
730 AliDebug(1, "Will apply trigger efficiency");
734 AliFatal("I was requested to apply trigger efficiency, but I could "
739 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
740 (fGenerateNoisyDigits ? "":"NOT")));
742 fIsInitialized = kTRUE;
746 //_____________________________________________________________________________
748 AliMUONDigitizerV3::MergeDigits(const AliMUONDigit& src,
749 AliMUONDigit& srcAndDest)
751 /// Merge 2 digits (src and srcAndDest) into srcAndDest.
753 AliDebug(2,"Merging the following digits:");
754 // StdoutToAliDebug(2,src.Print("tracks"););
755 // StdoutToAliDebug(2,srcAndDest.Print("tracks"););
757 Bool_t check = ( src.DetElemId() == srcAndDest.DetElemId() &&
758 src.PadX() == srcAndDest.PadX() &&
759 src.PadY() == srcAndDest.PadY() &&
760 src.Cathode() == srcAndDest.Cathode() );
766 srcAndDest.AddSignal(src.Signal());
767 srcAndDest.AddPhysicsSignal(src.Physics());
768 for ( Int_t i = 0; i < src.Ntracks(); ++i )
770 srcAndDest.AddTrack(src.Track(i),src.TrackCharge(i));
772 // StdoutToAliDebug(2,cout << "result:"; srcAndDest.Print("tracks"););
776 //_____________________________________________________________________________
778 AliMUONDigitizerV3::MergeWithSDigits(AliMUONData& outputData,
779 const AliMUONData& inputData, Int_t mask)
781 /// Merge the sdigits in inputData with the digits already present in outputData
785 for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich )
787 TClonesArray* iDigits = inputData.SDigits(ich);
788 TClonesArray* oDigits = outputData.Digits(ich);
791 AliError(Form("Could not get sdigits for ich=%d",ich));
794 Int_t nSDigits = iDigits->GetEntriesFast();
795 for ( Int_t k = 0; k < nSDigits; ++k )
797 AliMUONDigit* sdigit = static_cast<AliMUONDigit*>(iDigits->UncheckedAt(k));
800 AliError(Form("Could not get sdigit for ich=%d and k=%d",ich,k));
804 // Update the track references using the mask.
805 // FIXME: this is dirty, for backward compatibility only.
806 // Should re-design all this way of keeping track of MC information...
807 if ( mask ) sdigit->PatchTracks(mask);
808 // Then add or update the digit to the output.
809 AddOrUpdateDigit(*oDigits,*sdigit);