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 charge = digit.Charge();
161 // We set the charge to 0, as the only relevant piece of information
162 // after Digitization is the ADC value.
167 digit.SetADC(TMath::Min(kMaxADC,TMath::Nint(charge)));
171 Int_t detElemId = digit.DetElemId();
173 Int_t manuId = digit.ManuId();
174 Int_t manuChannel = digit.ManuChannel();
176 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
179 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
185 Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
186 Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
188 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
191 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
198 Float_t a0 = gain->ValueAsFloat(manuChannel,0);
199 Float_t a1 = gain->ValueAsFloat(manuChannel,1);
201 Int_t thres = gain->ValueAsInt(manuChannel,2);
202 Float_t chargeThres = a0*thres;
204 Float_t padc(0); // (adc - ped) value
206 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
210 if ( TMath::Abs(a0) > 1E-12 )
215 else // charge > chargeThres && a1 not zero
218 padc = TMath::Sqrt((chargeThres-charge)/a1) + thres;
221 Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
226 adc = TMath::Nint(padc + pedestalMean + adcNoise);
229 // be sure we stick to 12 bits.
235 AliDebug(3,Form("DE %4d Manu %4d Ch %2d Charge %e A0 %e A1 %e Thres %d padc %e ADC %4d",
236 detElemId,manuId,manuChannel,charge,
237 gain->ValueAsFloat(manuChannel,0),
238 gain->ValueAsFloat(manuChannel,1),
239 gain->ValueAsInt(manuChannel,2),
246 //_____________________________________________________________________________
248 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
249 AliMUONVDigit& digit)
251 /// \todo add comment
253 if ( !fTriggerEfficiency ) return;
255 if (digit.IsEfficiencyApplied()) return;
257 AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
259 if (!correspondingDigit) return; //reject bad correspondences
261 Int_t detElemId = digit.DetElemId();
263 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
264 const AliMpVSegmentation* segment[2] =
266 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())),
267 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
272 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
273 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
277 if (digit.Cathode()==0) p0=0;
279 AliMpIntPair location = pad[p0].GetLocation(0);
280 Int_t nboard = location.GetFirst();
284 fTriggerEfficiency->IsTriggered(detElemId, nboard,
285 isTrig[0], isTrig[1]);
286 digit.EfficiencyApplied(kTRUE);
287 correspondingDigit->EfficiencyApplied(kTRUE);
289 if (!isTrig[digit.Cathode()])
294 if ( &digit != correspondingDigit )
296 if (!isTrig[correspondingDigit->Cathode()])
298 correspondingDigit->SetCharge(0);
303 //_____________________________________________________________________________
305 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
306 AliMUONVDigitStore& filteredStore)
308 /// Loop over all chamber digits, and apply the response to them
309 /// Note that this method may remove digits.
311 filteredStore.Clear();
313 const Bool_t kAddNoise = kTRUE;
315 TIter next(store.CreateIterator());
316 AliMUONVDigit* digit;
318 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
320 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
322 if ( stationType != AliMp::kStationTrigger )
324 ApplyResponseToTrackerDigit(*digit,kAddNoise);
328 ApplyResponseToTriggerDigit(store,*digit);
330 if ( digit->ADC() > 0 || digit->Charge() > 0 )
332 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
337 //_____________________________________________________________________________
339 AliMUONDigitizerV3::Exec(Option_t*)
342 /// We first loop over input files, and merge the sdigits we found there.
343 /// Second, we digitize all the resulting sdigits
344 /// Then we generate noise-only digits (for tracker only)
345 /// And we finally generate the trigger outputs.
347 AliDebug(1, "Running digitizer.");
349 if ( fManager->GetNinputs() == 0 )
351 AliWarning("No input set. Nothing to do.");
355 if ( !fIsInitialized )
357 AliError("Not initialized. Cannot perform the work. Sorry");
361 fExecTimer.Start(kFALSE);
363 Int_t nInputFiles = fManager->GetNinputs();
365 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
367 outputLoader->MakeDigitsContainer();
369 TTree* oTreeD = outputLoader->TreeD();
373 AliFatal("Cannot create output TreeD");
376 // Loop over all the input files, and merge the sdigits found in those
379 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
381 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
383 inputLoader->LoadSDigits("READ");
385 TTree* iTreeS = inputLoader->TreeS();
388 AliFatal(Form("Could not get access to input file #%d",iFile));
391 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
392 inputStore->Connect(*iTreeS);
396 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
398 inputLoader->UnloadSDigits();
403 // At this point, we do have digit arrays (one per chamber) which contains
404 // the merging of all the sdigits of the input file(s).
405 // We now massage them to apply the detector response, i.e. this
406 // is here that we do the "digitization" work.
408 if (!fOutputDigitStore)
410 fOutputDigitStore = fDigitStore->Create();
413 if ( fGenerateNoisyDigits>=2 )
415 // Generate noise-only digits for trigger.
416 GenerateNoisyDigitsForTrigger(*fDigitStore);
419 ApplyResponse(*fDigitStore,*fOutputDigitStore);
421 if ( fGenerateNoisyDigits )
423 // Generate noise-only digits for tracker.
424 GenerateNoisyDigits(*fOutputDigitStore);
427 // We generate the global and local trigger decisions.
428 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
430 // Prepare output tree
431 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
432 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
435 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
439 // Fill the output treeD
442 // Write to the output tree(D).
443 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
444 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
446 outputLoader->WriteDigits("OVERWRITE");
448 outputLoader->UnloadDigits();
450 // Finally, we clean up after ourselves.
451 fTriggerStore->Clear();
452 fDigitStore->Clear();
453 fOutputDigitStore->Clear();
457 //_____________________________________________________________________________
459 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
460 AliMUONVDigit& digit) const
462 /// Find, if it exists, the digit corresponding to digit.Hit(), in the
465 TIter next(digitStore.CreateIterator());
468 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
470 if ( d->DetElemId() == digit.DetElemId() &&
471 d->Hit() == digit.Hit() &&
472 d->Cathode() != digit.Cathode() )
481 //_____________________________________________________________________________
483 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
485 /// According to a given probability, generate digits that
486 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
487 /// that are "only noise".
489 if ( !fNoiseFunction )
491 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
492 fgkNSigmas,fgkNSigmas*10);
494 fNoiseFunction->SetParameters(1,0,1);
497 fGenerateNoisyDigitsTimer.Start(kFALSE);
499 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
505 while ( !it.IsDone() )
507 for ( Int_t cathode = 0; cathode < 2; ++cathode )
509 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
515 fGenerateNoisyDigitsTimer.Stop();
518 //_____________________________________________________________________________
520 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
521 Int_t detElemId, Int_t cathode)
523 /// Generate noise-only digits for one cathode of one detection element.
524 /// Called by GenerateNoisyDigits()
526 const AliMpVSegmentation* seg
527 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
528 Int_t nofPads = seg->NofPads();
530 Int_t maxIx = seg->MaxPadIndexX();
531 Int_t maxIy = seg->MaxPadIndexY();
533 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
535 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
536 if ( !nofNoisyPads ) return;
539 TMath::Nint(gRandom->Gaus(nofNoisyPads,
540 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
542 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
544 for ( Int_t i = 0; i < nofNoisyPads; ++i )
551 ix = gRandom->Integer(maxIx+1);
552 iy = gRandom->Integer(maxIy+1);
553 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
554 } while ( !pad.IsValid() );
556 Int_t manuId = pad.GetLocation().GetFirst();
557 Int_t manuChannel = pad.GetLocation().GetSecond();
559 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
563 // no pedestal available for this channel, simply give up
567 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
571 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
572 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
574 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
576 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
578 ApplyResponseToTrackerDigit(*d,kFALSE);
581 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
582 // this can happen (that we randomly chose a digit that is
583 // already there). We simply ignore this, but log the occurence
584 // to cross-check that it's not too frequent.
587 fLogger->Log("Collision while adding noiseOnly digit");
591 fLogger->Log("Added noiseOnly digit");
596 AliError("Pure noise below threshold. This should not happen. Not adding "
604 //_____________________________________________________________________________
606 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
608 /// Generate noise-only digits for one cathode of one detection element.
609 /// Called by GenerateNoisyDigits()
611 if ( !fNoiseFunctionTrig )
613 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
616 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
620 AliMUONVDigit *d[2]={0x0};
622 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
625 Int_t nofNoisyPads = 50;
627 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
630 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
632 for ( Int_t i = 0; i < nofNoisyPads; ++i )
634 //printf("Generating noise %i\n",i);
637 Bool_t isOk = kFALSE;
638 Int_t detElemId = -1;
640 //r = gRandom->Landau(9.85026, 9.35881e-02);
641 r = fNoiseFunctionTrig->GetRandom();
642 fi = 2. * TMath::Pi() * gRandom->Rndm();
643 //printf("r = %f\tfi = %f\n", r, fi);
644 gx = r * TMath::Cos(fi);
645 gy = r * TMath::Sin(fi);
647 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
648 Int_t currDetElemId = it.CurrentDEId();
649 const AliMpVSegmentation* seg
650 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
652 Float_t deltax = seg->Dimensions().X();
653 Float_t deltay = seg->Dimensions().Y();
654 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
655 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
656 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
665 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
666 detElemId = currDetElemId;
667 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
668 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
669 if(!pad[0].IsValid()) continue;
676 const AliMpVSegmentation* seg1
677 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
678 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
680 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
681 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
682 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
683 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
684 ix = pad[cathode].GetIndices().GetFirst();
685 iy = pad[cathode].GetIndices().GetSecond();
686 d[cathode]->SetPadXY(ix,iy);
687 //d[cathode].SetSignal(1);
688 //d[cathode].SetPhysicsSignal(0);
689 d[cathode]->SetCharge(1);
690 d[cathode]->NoiseOnly(kTRUE);
691 AliDebug(3,Form("Adding a pure noise digit :"));
693 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
696 fLogger->Log("Collision while adding TriggerNoise digit");
700 fLogger->Log("Added triggerNoise digit");
703 } // loop on noisy pads
704 } // loop on chambers
708 //_____________________________________________________________________________
710 AliMUONDigitizerV3::GetLoader(const TString& folderName)
712 /// Get a MUON loader
714 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
715 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
718 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
724 //_____________________________________________________________________________
726 AliMUONDigitizerV3::Init()
728 /// Initialization of the TTask :
729 /// a) create the calibrationData, according to run number
730 /// b) create the trigger processing task
734 if ( fIsInitialized )
736 AliError("Object already initialized.");
742 AliError("fManager is null !");
746 Int_t runnumber = AliCDBManager::Instance()->GetRun();
748 fCalibrationData = new AliMUONCalibrationData(runnumber);
749 if ( !fCalibrationData->Pedestals() )
751 AliFatal("Could not access pedestals from OCDB !");
753 if ( !fCalibrationData->Gains() )
755 AliFatal("Could not access gains from OCDB !");
757 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
759 if ( muon()->GetTriggerEffCells() )
761 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
762 if ( fTriggerEfficiency )
764 AliDebug(1, "Will apply trigger efficiency");
768 AliFatal("I was requested to apply trigger efficiency, but I could "
773 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
774 (fGenerateNoisyDigits ? "":"NOT")));
776 fIsInitialized = kTRUE;
780 //_____________________________________________________________________________
782 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
783 const AliMUONVDigitStore& input,
786 /// Merge the sdigits in inputData with the digits already present in outputData
788 if ( !outputStore ) outputStore = input.Create();
790 TIter next(input.CreateIterator());
791 AliMUONVDigit* sdigit;
793 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
795 // Update the track references using the mask.
796 // FIXME: this is dirty, for backward compatibility only.
797 // Should re-design all this way of keeping track of MC information...
798 if ( mask ) sdigit->PatchTracks(mask);
799 // Then add or update the digit to the output.
800 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
803 AliError("Could not add digit in merge mode");