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>
54 #include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
56 /// \class AliMUONDigitizerV3
57 /// The digitizer is performing the transformation to go from SDigits (digits
58 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
60 /// The decalibration is performed by doing the reverse operation of the
61 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
63 /// Note also that the digitizer takes care of merging sdigits that belongs
64 /// to the same pad, either because we're merging several input sdigit files
65 /// or with a single file because the sdigitizer does not merge sdigits itself
66 /// (for performance reason mainly, and because anyway we know we have to do it
67 /// here, at the digitization level).
69 /// \author Laurent Aphecetche
75 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
78 //ADDED for trigger noise
79 const AliMUONGeometryTransformer* GetTransformer()
81 return muon()->GetGeometryTransformer();
85 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
88 ClassImp(AliMUONDigitizerV3)
91 //_____________________________________________________________________________
92 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
93 Int_t generateNoisyDigits)
94 : AliDigitizer(manager),
95 fIsInitialized(kFALSE),
96 fCalibrationData(0x0),
97 fTriggerProcessor(0x0),
98 fTriggerEfficiency(0x0),
99 fGenerateNoisyDigitsTimer(),
102 fNoiseFunctionTrig(0x0),
103 fGenerateNoisyDigits(generateNoisyDigits),
104 fLogger(new AliMUONLogger(1000)),
105 fTriggerStore(new AliMUONTriggerStoreV1),
107 fOutputDigitStore(0x0)
111 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
112 fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
113 fExecTimer.Start(kTRUE); fExecTimer.Stop();
116 //_____________________________________________________________________________
117 AliMUONDigitizerV3::~AliMUONDigitizerV3()
119 /// Dtor. Note we're the owner of some pointers.
123 delete fCalibrationData;
124 delete fTriggerProcessor;
125 delete fNoiseFunction;
126 delete fNoiseFunctionTrig;
127 delete fTriggerStore;
129 delete fOutputDigitStore;
131 if ( fGenerateNoisyDigits )
133 AliDebug(1, Form("Execution time for GenerateNoisyDigits() : R:%.2fs C:%.2fs",
134 fGenerateNoisyDigitsTimer.RealTime(),
135 fGenerateNoisyDigitsTimer.CpuTime()));
137 AliDebug(1, Form("Execution time for Exec() : R:%.2fs C:%.2fs",
138 fExecTimer.RealTime(),fExecTimer.CpuTime()));
140 AliInfo("Summary of messages");
146 //_____________________________________________________________________________
148 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
150 /// For tracking digits, starting from an ideal digit's charge, we :
152 /// - add some noise (thus leading to a realistic charge), if requested to do so
153 /// - divide by a gain (thus decalibrating the digit)
154 /// - add a pedestal (thus decalibrating the digit)
155 /// - sets the signal to zero if below 3*sigma of the noise
157 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
159 Float_t signal = digit.Charge();
163 digit.SetADC(TMath::Min(kMaxADC,TMath::Nint(signal)));
167 Int_t detElemId = digit.DetElemId();
169 Int_t manuId = digit.ManuId();
170 Int_t manuChannel = digit.ManuChannel();
172 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
175 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
182 Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
183 Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
185 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
188 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
195 Float_t gainMean = gain->ValueAsFloat(manuChannel,0);
197 Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
201 if ( gainMean < 1E-6 )
203 AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. "
204 "Setting signal to 0.",
205 gainMean,detElemId,manuId,manuChannel));
210 adc = TMath::Nint( signal / gainMean + pedestalMean + adcNoise);///
212 if ( adc <= pedestalMean + fgkNSigmas*pedestalSigma )
218 // be sure we stick to 12 bits.
224 digit.SetCharge(adc);
228 //_____________________________________________________________________________
230 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
231 AliMUONVDigit& digit)
233 /// \todo add comment
235 if ( !fTriggerEfficiency ) return;
237 if (digit.IsEfficiencyApplied()) return;
239 AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
241 if (!correspondingDigit) return; //reject bad correspondences
243 Int_t detElemId = digit.DetElemId();
245 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
246 const AliMpVSegmentation* segment[2] =
248 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())),
249 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
254 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
255 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
259 if (digit.Cathode()==0) p0=0;
261 AliMpIntPair location = pad[p0].GetLocation(0);
262 Int_t nboard = location.GetFirst();
266 fTriggerEfficiency->IsTriggered(detElemId, nboard,
267 isTrig[0], isTrig[1]);
268 digit.EfficiencyApplied(kTRUE);
269 correspondingDigit->EfficiencyApplied(kTRUE);
271 if (!isTrig[digit.Cathode()])
276 if ( &digit != correspondingDigit )
278 if (!isTrig[correspondingDigit->Cathode()])
280 correspondingDigit->SetCharge(0);
285 //_____________________________________________________________________________
287 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
288 AliMUONVDigitStore& filteredStore)
290 /// Loop over all chamber digits, and apply the response to them
291 /// Note that this method may remove digits.
293 filteredStore.Clear();
295 const Bool_t kAddNoise = kTRUE;
297 TIter next(store.CreateIterator());
298 AliMUONVDigit* digit;
300 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
302 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
304 if ( stationType != AliMp::kStationTrigger )
306 ApplyResponseToTrackerDigit(*digit,kAddNoise);
310 ApplyResponseToTriggerDigit(store,*digit);
312 if ( digit->Charge() > 0 )
314 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
319 //_____________________________________________________________________________
321 AliMUONDigitizerV3::Exec(Option_t*)
324 /// We first loop over input files, and merge the sdigits we found there.
325 /// Second, we digitize all the resulting sdigits
326 /// Then we generate noise-only digits (for tracker only)
327 /// And we finally generate the trigger outputs.
329 AliDebug(1, "Running digitizer.");
331 if ( fManager->GetNinputs() == 0 )
333 AliWarning("No input set. Nothing to do.");
337 if ( !fIsInitialized )
339 AliError("Not initialized. Cannot perform the work. Sorry");
343 fExecTimer.Start(kFALSE);
345 Int_t nInputFiles = fManager->GetNinputs();
347 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
349 outputLoader->MakeDigitsContainer();
351 TTree* oTreeD = outputLoader->TreeD();
355 AliFatal("Cannot create output TreeD");
358 // Loop over all the input files, and merge the sdigits found in those
361 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
363 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
365 inputLoader->LoadSDigits("READ");
367 TTree* iTreeS = inputLoader->TreeS();
370 AliFatal(Form("Could not get access to input file #%d",iFile));
373 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
374 inputStore->Connect(*iTreeS);
378 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
380 inputLoader->UnloadSDigits();
385 // At this point, we do have digit arrays (one per chamber) which contains
386 // the merging of all the sdigits of the input file(s).
387 // We now massage them to apply the detector response, i.e. this
388 // is here that we do the "digitization" work.
390 if (!fOutputDigitStore)
392 fOutputDigitStore = fDigitStore->Create();
395 if ( fGenerateNoisyDigits>=2 )
397 // Generate noise-only digits for trigger.
398 GenerateNoisyDigitsForTrigger(*fDigitStore);
401 ApplyResponse(*fDigitStore,*fOutputDigitStore);
403 if ( fGenerateNoisyDigits )
405 // Generate noise-only digits for tracker.
406 GenerateNoisyDigits(*fOutputDigitStore);
409 // We generate the global and local trigger decisions.
410 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
412 // Prepare output tree
413 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
414 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
417 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
421 // Fill the output treeD
424 // Write to the output tree(D).
425 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
426 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
428 outputLoader->WriteDigits("OVERWRITE");
430 outputLoader->UnloadDigits();
432 // Finally, we clean up after ourselves.
433 fTriggerStore->Clear();
434 fDigitStore->Clear();
435 fOutputDigitStore->Clear();
439 //_____________________________________________________________________________
441 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
442 AliMUONVDigit& digit) const
444 /// Find, if it exists, the digit corresponding to digit.Hit(), in the
447 TIter next(digitStore.CreateIterator());
450 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
452 if ( d->DetElemId() == digit.DetElemId() &&
453 d->Hit() == digit.Hit() &&
454 d->Cathode() != digit.Cathode() )
463 //_____________________________________________________________________________
465 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
467 /// According to a given probability, generate digits that
468 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
469 /// that are "only noise".
471 if ( !fNoiseFunction )
473 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
474 fgkNSigmas,fgkNSigmas*10);
476 fNoiseFunction->SetParameters(1,0,1);
479 fGenerateNoisyDigitsTimer.Start(kFALSE);
481 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
487 while ( !it.IsDone() )
489 for ( Int_t cathode = 0; cathode < 2; ++cathode )
491 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
497 fGenerateNoisyDigitsTimer.Stop();
500 //_____________________________________________________________________________
502 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
503 Int_t detElemId, Int_t cathode)
505 /// Generate noise-only digits for one cathode of one detection element.
506 /// Called by GenerateNoisyDigits()
508 const AliMpVSegmentation* seg
509 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
510 Int_t nofPads = seg->NofPads();
512 Int_t maxIx = seg->MaxPadIndexX();
513 Int_t maxIy = seg->MaxPadIndexY();
515 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
517 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
518 if ( !nofNoisyPads ) return;
521 TMath::Nint(gRandom->Gaus(nofNoisyPads,
522 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
524 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
526 for ( Int_t i = 0; i < nofNoisyPads; ++i )
533 ix = gRandom->Integer(maxIx+1);
534 iy = gRandom->Integer(maxIy+1);
535 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
536 } while ( !pad.IsValid() );
538 Int_t manuId = pad.GetLocation().GetFirst();
539 Int_t manuChannel = pad.GetLocation().GetSecond();
541 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
545 // no pedestal available for this channel, simply give up
549 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
553 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
554 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
556 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
558 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
560 ApplyResponseToTrackerDigit(*d,kFALSE);
561 if ( d->Charge() > 0 )
563 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
564 // this can happen (that we randomly chose a digit that is
565 // already there). We simply ignore this, but log the occurence
566 // to cross-check that it's not too frequent.
569 fLogger->Log("Collision while adding noiseOnly digit");
573 fLogger->Log("Added noiseOnly digit");
578 AliError("Pure noise below threshold. This should not happen. Not adding "
586 //_____________________________________________________________________________
588 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
590 /// Generate noise-only digits for one cathode of one detection element.
591 /// Called by GenerateNoisyDigits()
593 if ( !fNoiseFunctionTrig )
595 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
598 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
602 AliMUONVDigit *d[2]={0x0};
604 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
607 Int_t nofNoisyPads = 50;
609 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
612 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
614 for ( Int_t i = 0; i < nofNoisyPads; ++i )
616 //printf("Generating noise %i\n",i);
619 Bool_t isOk = kFALSE;
620 Int_t detElemId = -1;
622 //r = gRandom->Landau(9.85026, 9.35881e-02);
623 r = fNoiseFunctionTrig->GetRandom();
624 fi = 2. * TMath::Pi() * gRandom->Rndm();
625 //printf("r = %f\tfi = %f\n", r, fi);
626 gx = r * TMath::Cos(fi);
627 gy = r * TMath::Sin(fi);
629 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
630 Int_t currDetElemId = it.CurrentDEId();
631 const AliMpVSegmentation* seg
632 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
634 Float_t deltax = seg->Dimensions().X();
635 Float_t deltay = seg->Dimensions().Y();
636 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
637 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
638 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
647 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
648 detElemId = currDetElemId;
649 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
650 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
651 if(!pad[0].IsValid()) continue;
658 const AliMpVSegmentation* seg1
659 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
660 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
662 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
663 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
664 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
665 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
666 ix = pad[cathode].GetIndices().GetFirst();
667 iy = pad[cathode].GetIndices().GetSecond();
668 d[cathode]->SetPadXY(ix,iy);
669 //d[cathode].SetSignal(1);
670 //d[cathode].SetPhysicsSignal(0);
671 d[cathode]->SetCharge(1);
672 d[cathode]->NoiseOnly(kTRUE);
673 AliDebug(3,Form("Adding a pure noise digit :"));
675 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
678 fLogger->Log("Collision while adding TriggerNoise digit");
682 fLogger->Log("Added triggerNoise digit");
685 } // loop on noisy pads
686 } // loop on chambers
690 //_____________________________________________________________________________
692 AliMUONDigitizerV3::GetLoader(const TString& folderName)
694 /// Get a MUON loader
696 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
697 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
700 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
706 //_____________________________________________________________________________
708 AliMUONDigitizerV3::Init()
710 /// Initialization of the TTask :
711 /// a) create the calibrationData, according to run number
712 /// b) create the trigger processing task
716 if ( fIsInitialized )
718 AliError("Object already initialized.");
724 AliError("fManager is null !");
728 Int_t runnumber = AliCDBManager::Instance()->GetRun();
730 fCalibrationData = new AliMUONCalibrationData(runnumber);
731 if ( !fCalibrationData->Pedestals() )
733 AliFatal("Could not access pedestals from OCDB !");
735 if ( !fCalibrationData->Gains() )
737 AliFatal("Could not access gains from OCDB !");
739 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
741 if ( muon()->GetTriggerEffCells() )
743 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
744 if ( fTriggerEfficiency )
746 AliDebug(1, "Will apply trigger efficiency");
750 AliFatal("I was requested to apply trigger efficiency, but I could "
755 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
756 (fGenerateNoisyDigits ? "":"NOT")));
758 fIsInitialized = kTRUE;
762 //_____________________________________________________________________________
764 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
765 const AliMUONVDigitStore& input,
768 /// Merge the sdigits in inputData with the digits already present in outputData
770 if ( !outputStore ) outputStore = input.Create();
772 TIter next(input.CreateIterator());
773 AliMUONVDigit* sdigit;
775 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
777 // Update the track references using the mask.
778 // FIXME: this is dirty, for backward compatibility only.
779 // Should re-design all this way of keeping track of MC information...
780 if ( mask ) sdigit->PatchTracks(mask);
781 // Then add or update the digit to the output.
782 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
785 AliError("Could not add digit in merge mode");