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 **************************************************************************/
18 #include "AliMUONDigitizerV3.h"
22 #include "AliMUONCalibrationData.h"
23 #include "AliMUONConstants.h"
24 #include "AliMUONData.h"
25 #include "AliMUONDataIterator.h"
26 #include "AliMUONDigit.h"
27 #include "AliMUONSegmentation.h"
28 #include "AliMUONTriggerDecisionV1.h"
29 #include "AliMUONTriggerEfficiencyCells.h"
30 #include "AliMUONTriggerElectronics.h"
31 #include "AliMUONVCalibParam.h"
32 #include "AliMpDEIterator.h"
33 #include "AliMpDEManager.h"
34 #include "AliMpIntPair.h"
36 #include "AliMpStationType.h"
37 #include "AliMpVSegmentation.h"
39 #include "AliRunDigitizer.h"
40 #include "AliRunLoader.h"
41 #include "Riostream.h"
47 /// The digitizer is performing the transformation to go from SDigits (digits
48 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
50 /// The decalibration is performed by doing the reverse operation of the
51 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
53 /// Note also that the digitizer takes care of merging sdigits that belongs
54 /// to the same pad, either because we're merging several input sdigit files
55 /// or with a single file because the sdigitizer does not merge sdigits itself
56 /// (for performance reason mainly, and because anyway we know we have to do it
57 /// here, at the digitization level).
64 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
67 AliMUONSegmentation* Segmentation()
69 static AliMUONSegmentation* segmentation = muon()->GetSegmentation();
74 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
76 ClassImp(AliMUONDigitizerV3)
78 //_____________________________________________________________________________
79 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
80 ETriggerCodeVersion triggerCodeVersion,
81 Bool_t useTriggerEfficiency,
82 Bool_t generateNoisyDigits)
83 : AliDigitizer(manager),
84 fIsInitialized(kFALSE),
86 fCalibrationData(0x0),
87 fTriggerProcessor(0x0),
88 fTriggerCodeVersion(triggerCodeVersion),
89 fUseTriggerEfficiency(useTriggerEfficiency),
90 fTriggerEfficiency(0x0),
92 fGenerateNoisyDigits(generateNoisyDigits)
97 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
98 fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
99 fExecTimer.Start(kTRUE); fExecTimer.Stop();
100 fFindDigitIndexTimer.Start(kTRUE); fFindDigitIndexTimer.Stop();
103 //______________________________________________________________________________
104 AliMUONDigitizerV3::AliMUONDigitizerV3(const AliMUONDigitizerV3& right)
105 : AliDigitizer(right)
107 /// Protected copy constructor (not implemented)
109 AliFatal("Copy constructor not provided.");
112 //_____________________________________________________________________________
113 AliMUONDigitizerV3::~AliMUONDigitizerV3()
116 // Dtor. Note we're the owner of some pointers.
121 delete fCalibrationData;
122 delete fTriggerProcessor;
123 delete fNoiseFunction;
125 AliInfo(Form("Execution time for FindDigitIndex() : R:%.2fs C:%.2fs",
126 fFindDigitIndexTimer.RealTime(),fFindDigitIndexTimer.CpuTime()));
127 if ( fGenerateNoisyDigits )
129 AliInfo(Form("Execution time for GenerateNoisyDigits() : R:%.2fs C:%.2fs",
130 fGenerateNoisyDigitsTimer.RealTime(),
131 fGenerateNoisyDigitsTimer.CpuTime()));
133 AliInfo(Form("Execution time for Exec() : R:%.2fs C:%.2fs",
134 fExecTimer.RealTime(),fExecTimer.CpuTime()));
138 //______________________________________________________________________________
140 AliMUONDigitizerV3::operator=(const AliMUONDigitizerV3& right)
142 /// Protected assignement operator (not implemented)
144 // check assignement to self
145 if (this == &right) return *this;
147 AliFatal("Assignement operator not provided.");
152 //_____________________________________________________________________________
154 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONDigit& digit, Bool_t addNoise)
156 // For tracking digits, starting from an ideal digit's charge, we :
158 // - add some noise (thus leading to a realistic charge), if requested to do so
159 // - divide by a gain (thus decalibrating the digit)
160 // - add a pedestal (thus decalibrating the digit)
161 // - sets the signal to zero if below 3*sigma of the noise
164 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
166 Float_t signal = digit.Signal();
170 digit.SetADC(TMath::Nint(signal));
174 Int_t detElemId = digit.DetElemId();
176 Int_t manuId = digit.ManuId();
177 Int_t manuChannel = digit.ManuChannel();
179 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
182 AliFatal(Form("Could not get pedestal for DE=%d manuId=%d",
185 Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
186 Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
188 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
191 AliFatal(Form("Could not get gain for DE=%d manuId=%d",
194 Float_t gainMean = gain->ValueAsFloat(manuChannel,0);
196 Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
200 if ( gainMean < 1E-6 )
202 AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. "
203 "Setting signal to 0.",
204 gainMean,detElemId,manuId,manuChannel));
209 adc = TMath::Nint( signal / gainMean + pedestalMean + adcNoise);///
211 if ( adc <= pedestalMean + fgkNSigmas*pedestalSigma )
217 // be sure we stick to 12 bits.
223 digit.SetPhysicsSignal(TMath::Nint(signal));
224 digit.SetSignal(adc);
228 //_____________________________________________________________________________
230 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(AliMUONDigit& digit,
233 if ( !fTriggerEfficiency ) return;
235 AliMUONDigit* correspondingDigit = FindCorrespondingDigit(digit,data);
237 Int_t detElemId = digit.DetElemId();
239 const AliMpVSegmentation* segment[2] =
241 Segmentation()->GetMpSegmentation(detElemId,digit.Cathode()),
242 Segmentation()->GetMpSegmentation(detElemId,correspondingDigit->Cathode())
247 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
248 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
254 if (digit.Cathode()==0)
260 Float_t x = pad[ix].Position().X();
261 Float_t y = pad[iy].Position().Y();
262 if ( x==-1 && y==-1 )
266 AliError(Form("Got an unknown position for a digit in DE %d at (ix,iy)=(%d,%d)",
267 detElemId,pad[ix].GetIndices().GetFirst(),pad[iy].GetIndices().GetSecond()));
269 Float_t x0 = segment[0]->Dimensions().X();
270 Float_t y0 = segment[1]->Dimensions().Y();
271 TVector2 newCoord = fTriggerEfficiency->ChangeReferenceFrame(x, y, x0, y0);
274 fTriggerEfficiency->IsTriggered(detElemId, newCoord.Px(), newCoord.Py(),
275 isTrig[0], isTrig[1]);
277 if (!isTrig[digit.Cathode()])
282 if ( &digit != correspondingDigit )
284 if (!isTrig[correspondingDigit->Cathode()])
286 correspondingDigit->SetSignal(0);
291 //_____________________________________________________________________________
293 AliMUONDigitizerV3::ApplyResponse()
296 // Loop over all chamber digits, and apply the response to them
297 // Note that this method may remove digits.
299 const Bool_t kAddNoise = kTRUE;
301 for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich )
303 TClonesArray* digits = fOutputData->Digits(ich);
304 Int_t n = digits->GetEntriesFast();
305 Bool_t trackingChamber = ( ich < AliMUONConstants::NTrackingCh() );
306 for ( Int_t i = 0; i < n; ++i )
308 AliMUONDigit* d = static_cast<AliMUONDigit*>(digits->UncheckedAt(i));
309 if ( trackingChamber )
311 ApplyResponseToTrackerDigit(*d,kAddNoise);
315 ApplyResponseToTriggerDigit(*d,fOutputData);
317 if ( d->Signal() <= 0 )
325 // The version below, using iterator, does not yet work (as the iterator
326 // assumes it is reading digits from the tree, while in this case it's
329 // AliMUONDigit* digit(0x0);
331 // // First loop on tracker digits
332 // AliMUONDataIterator tracker(fOutputData,"D",AliMUONDataIterator::kTrackingChambers);
334 // while ( ( digit = static_cast<AliMUONDigit*>(tracker.Next()) ) )
336 // ApplyResponseToTrackerDigit(*digit);
337 // if ( digit->Signal() <= 0 )
344 // // Then loop on trigger digits
345 // AliMUONDataIterator trigger(fOutputData,"D",AliMUONDataIterator::kTriggerChambers);
347 // while ( ( digit = static_cast<AliMUONDigit*>(trigger.Next()) ) )
349 // ApplyResponseToTriggerDigit(*digit,fOutputData);
350 // if ( digit->Signal() <= 0 )
357 //_____________________________________________________________________________
359 AliMUONDigitizerV3::AddOrUpdateDigit(TClonesArray& array,
360 const AliMUONDigit& digit)
363 // Add or update a digit, depending on whether there's already a digit
364 // for the corresponding channel.
366 Int_t ix = FindDigitIndex(array,digit);
370 AliMUONDigit* d = static_cast<AliMUONDigit*>(array.UncheckedAt(ix));
371 Bool_t ok = MergeDigits(digit,*d);
374 AliError("Digits are not mergeable !");
379 ix = array.GetLast() + 1;
380 new(array[ix]) AliMUONDigit(digit);
385 //_____________________________________________________________________________
387 AliMUONDigitizerV3::Exec(Option_t*)
391 // We first loop over input files, and merge the sdigits we found there.
392 // Second, we digitize all the resulting sdigits
393 // Then we generate noise-only digits (for tracker only)
394 // And we finally generate the trigger outputs.
397 AliDebug(1, "Running digitizer.");
399 if ( fManager->GetNinputs() == 0 )
401 AliWarning("No input set. Nothing to do.");
405 if ( !fIsInitialized )
407 AliError("Not initialized. Cannot perform the work. Sorry");
411 fExecTimer.Start(kFALSE);
413 Int_t nInputFiles = fManager->GetNinputs();
415 if ( fOutputData->TreeD() == 0x0 )
417 AliDebug(1,"Calling MakeDigitsContainer");
418 fOutputData->GetLoader()->MakeDigitsContainer();
420 fOutputData->MakeBranch("D,GLT");
421 fOutputData->SetTreeAddress("D,GLT");
423 // Loop over all the input files, and merge the sdigits found in those
425 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
427 AliMUONData* inputData = GetDataAccess(fManager->GetInputFolderName(iFile));
430 AliFatal(Form("Could not get access to input file #%d",iFile));
433 inputData->GetLoader()->LoadSDigits("READ");
434 inputData->SetTreeAddress("S");
435 inputData->GetSDigits();
437 MergeWithSDigits(*fOutputData,*inputData,fManager->GetMask(iFile));
439 inputData->ResetSDigits();
440 inputData->GetLoader()->UnloadSDigits();
444 // At this point, we do have digit arrays (one per chamber) which contains
445 // the merging of all the sdigits of the input file(s).
446 // We now massage them to apply the detector response, i.e. this
447 // is here that we do the "digitization" work.
451 if ( fGenerateNoisyDigits )
453 // Generate noise-only digits for tracker.
454 GenerateNoisyDigits();
457 // We generate the global and local trigger decisions.
458 fTriggerProcessor->ExecuteTask();
460 // Fill the output treeD
461 fOutputData->Fill("D,GLT");
463 // Write to the output tree(D).
464 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
465 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
467 fOutputData->GetLoader()->WriteDigits("OVERWRITE");
469 // Finally, we clean up after ourselves.
470 fOutputData->ResetDigits();
471 fOutputData->ResetTrigger();
472 fOutputData->GetLoader()->UnloadDigits();
477 //_____________________________________________________________________________
479 AliMUONDigitizerV3::FindCorrespondingDigit(AliMUONDigit& digit,
482 AliMUONDataIterator it(data,"D",AliMUONDataIterator::kAllChambers);
485 while ( ( cd = static_cast<AliMUONDigit*>(it.Next()) ) )
487 if ( cd->DetElemId() == digit.DetElemId() &&
488 cd->Hit() == digit.Hit() &&
489 cd->Cathode() != digit.Cathode() )
498 //_____________________________________________________________________________
500 AliMUONDigitizerV3::FindDigitIndex(TClonesArray& array,
501 const AliMUONDigit& digit) const
504 // Return the index of digit within array, if that digit is there,
505 // otherwise returns -1
507 // FIXME: this is of course not the best implementation you can think of.
508 // Reconsider the use of hit/digit map... ? (but be sure it's needed!)
511 fFindDigitIndexTimer.Start(kFALSE);
513 Int_t n = array.GetEntriesFast();
514 for ( Int_t i = 0; i < n; ++i )
516 AliMUONDigit* d = static_cast<AliMUONDigit*>(array.UncheckedAt(i));
517 if ( d->DetElemId() == digit.DetElemId() &&
518 d->PadX() == digit.PadX() &&
519 d->PadY() == digit.PadY() &&
520 d->Cathode() == digit.Cathode() )
522 fFindDigitIndexTimer.Stop();
526 fFindDigitIndexTimer.Stop();
530 //_____________________________________________________________________________
532 AliMUONDigitizerV3::GenerateNoisyDigits()
535 // According to a given probability, generate digits that
536 // have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
537 // that are "only noise".
540 if ( !fNoiseFunction )
542 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
543 fgkNSigmas,fgkNSigmas*10);
545 fNoiseFunction->SetParameters(1,0,1);
548 fGenerateNoisyDigitsTimer.Start(kFALSE);
550 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
556 while ( !it.IsDone() )
558 for ( Int_t cathode = 0; cathode < 2; ++cathode )
560 GenerateNoisyDigitsForOneCathode(it.CurrentDE(),cathode);
566 fGenerateNoisyDigitsTimer.Stop();
569 //_____________________________________________________________________________
571 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(Int_t detElemId, Int_t cathode)
574 // Generate noise-only digits for one cathode of one detection element.
575 // Called by GenerateNoisyDigits()
578 TClonesArray* digits = fOutputData->Digits(detElemId/100-1);
580 const AliMpVSegmentation* seg = Segmentation()->GetMpSegmentation(detElemId,cathode);
581 Int_t nofPads = seg->NofPads();
583 Int_t maxIx = seg->MaxPadIndexX();
584 Int_t maxIy = seg->MaxPadIndexY();
586 static const Double_t probToBeOutsideNsigmas = 1 - TMath::Erf(fgkNSigmas/TMath::Sqrt(2.0));
588 Int_t nofNoisyPads = TMath::Nint(probToBeOutsideNsigmas*nofPads);
589 if ( !nofNoisyPads ) return;
592 TMath::Nint(gRandom->Gaus(nofNoisyPads,
593 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
595 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
597 for ( Int_t i = 0; i < nofNoisyPads; ++i )
602 ix = gRandom->Integer(maxIx+1);
603 iy = gRandom->Integer(maxIy+1);
604 } while ( !seg->HasPad(AliMpIntPair(ix,iy)) );
606 d.SetDetElemId(detElemId);
607 d.SetCathode(cathode);
610 if ( FindDigitIndex(*digits,d) >= 0 )
612 // this digit is already there, and not noise-only, we simply skip it
615 AliMpPad pad = seg->PadByIndices(AliMpIntPair(ix,iy));
616 Int_t manuId = pad.GetLocation().GetFirst();
617 Int_t manuChannel = pad.GetLocation().GetSecond();
619 d.SetElectronics(manuId,manuChannel);
621 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
623 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
624 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
626 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
628 d.SetSignal(TMath::Nint(ped+pedestalMean+0.5));
629 d.SetPhysicsSignal(0);
631 AliDebug(3,Form("Adding a pure noise digit :"));
632 StdoutToAliDebug(3,cout << "Before Response: " << endl;
634 ApplyResponseToTrackerDigit(d,kFALSE);
635 if ( d.Signal() > 0 )
637 AddOrUpdateDigit(*digits,d);
641 AliError("Pure noise below threshold. This should not happen. Not adding "
644 StdoutToAliDebug(3,cout << "After Response: " << endl;
649 //_____________________________________________________________________________
651 AliMUONDigitizerV3::GetDataAccess(const TString& folderName)
654 // Create an AliMUONData to deal with data found in folderName.
656 AliDebug(1,Form("Getting access to folder %s",folderName.Data()));
657 AliRunLoader* runLoader = AliRunLoader::GetRunLoader(folderName);
660 AliError(Form("Could not get RunLoader from folder %s",folderName.Data()));
663 AliLoader* loader = static_cast<AliLoader*>(runLoader->GetLoader("MUONLoader"));
666 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
669 AliMUONData* data = new AliMUONData(loader,"MUON","MUONDataForDigitOutput");
670 AliDebug(1,Form("AliMUONData=%p loader=%p",data,loader));
674 //_____________________________________________________________________________
676 AliMUONDigitizerV3::Init()
679 // Initialization of the TTask :
680 // a) set the outputData pointer
681 // b) create the calibrationData, according to run number
682 // c) create the trigger processing task
686 if ( fIsInitialized )
688 AliError("Object already initialized.");
694 AliError("fManager is null !");
698 fOutputData = GetDataAccess(fManager->GetOutputFolderName());
701 AliError("Can not perform digitization. I'm sorry");
704 AliDebug(1,Form("fOutputData=%p",fOutputData));
706 AliRunLoader* runLoader = fOutputData->GetLoader()->GetRunLoader();
707 AliRun* galice = runLoader->GetAliRun();
708 Int_t runnumber = galice->GetRunNumber();
710 fCalibrationData = new AliMUONCalibrationData(runnumber);
712 switch (fTriggerCodeVersion)
714 case kTriggerDecision:
715 fTriggerProcessor = new AliMUONTriggerDecisionV1(fOutputData);
717 case kTriggerElectronics:
718 fTriggerProcessor = new AliMUONTriggerElectronics(fOutputData,fCalibrationData);
721 AliFatal("Unknown trigger processor type");
724 AliDebug(1,Form("Using the following trigger code %s - %s",
725 fTriggerProcessor->GetName(),fTriggerProcessor->GetTitle()));
727 if ( fUseTriggerEfficiency )
729 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
730 if ( fTriggerEfficiency )
732 AliInfo("Will apply trigger efficiency");
736 AliError("I was requested to apply trigger efficiency, but I could "
741 if ( fGenerateNoisyDigits )
743 AliInfo("Will generate noise-only digits for tracker");
746 fIsInitialized = kTRUE;
750 //_____________________________________________________________________________
752 AliMUONDigitizerV3::MergeDigits(const AliMUONDigit& src,
753 AliMUONDigit& srcAndDest)
756 // Merge 2 digits (src and srcAndDest) into srcAndDest.
758 AliDebug(1,"Merging the following digits:");
759 StdoutToAliDebug(1,src.Print("tracks"););
760 StdoutToAliDebug(1,srcAndDest.Print("tracks"););
762 Bool_t check = ( src.DetElemId() == srcAndDest.DetElemId() &&
763 src.PadX() == srcAndDest.PadX() &&
764 src.PadY() == srcAndDest.PadY() &&
765 src.Cathode() == srcAndDest.Cathode() );
771 srcAndDest.AddSignal(src.Signal());
772 srcAndDest.AddPhysicsSignal(src.Physics());
773 for ( Int_t i = 0; i < src.Ntracks(); ++i )
775 srcAndDest.AddTrack(src.Track(i),src.TrackCharge(i));
777 StdoutToAliDebug(1,cout << "result:"; srcAndDest.Print("tracks"););
781 //_____________________________________________________________________________
783 AliMUONDigitizerV3::MergeWithSDigits(AliMUONData& outputData,
784 const AliMUONData& inputData, Int_t mask)
787 // Merge the sdigits in inputData with the digits already present in outputData
791 for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich )
793 TClonesArray* iDigits = inputData.SDigits(ich);
794 TClonesArray* oDigits = outputData.Digits(ich);
797 AliError(Form("Could not get sdigits for ich=%d",ich));
800 Int_t nSDigits = iDigits->GetEntriesFast();
801 for ( Int_t k = 0; k < nSDigits; ++k )
803 AliMUONDigit* sdigit = static_cast<AliMUONDigit*>(iDigits->UncheckedAt(k));
806 AliError(Form("Could not get sdigit for ich=%d and k=%d",ich,k));
810 // Update the track references using the mask.
811 // FIXME: this is dirty, for backward compatibility only.
812 // Should re-design all this way of keeping track of MC information...
813 if ( mask ) sdigit->PatchTracks(mask);
814 // Then add or update the digit to the output.
815 AddOrUpdateDigit(*oDigits,*sdigit);