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"
22 #include "AliCodeTimer.h"
25 #include "AliMUONCalibrationData.h"
26 #include "AliMUONConstants.h"
27 #include "AliMUONDigit.h"
28 #include "AliMUONLogger.h"
29 #include "AliMUONTriggerEfficiencyCells.h"
30 #include "AliMUONTriggerElectronics.h"
31 #include "AliMUONTriggerStoreV1.h"
32 #include "AliMUONVCalibParam.h"
33 #include "AliMUONVDigitStore.h"
35 #include "AliMpCathodType.h"
36 #include "AliMpConstants.h"
37 #include "AliMpDEIterator.h"
38 #include "AliMpDEManager.h"
39 #include "AliMpIntPair.h"
41 #include "AliMpSegmentation.h"
42 #include "AliMpStationType.h"
43 #include "AliMpVSegmentation.h"
45 #include "AliRunDigitizer.h"
46 #include "AliRunLoader.h"
47 #include <Riostream.h>
55 #include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
57 //-----------------------------------------------------------------------------
58 /// \class AliMUONDigitizerV3
59 /// The digitizer is performing the transformation to go from SDigits (digits
60 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
62 /// The decalibration is performed by doing the reverse operation of the
63 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
65 /// Note also that the digitizer takes care of merging sdigits that belongs
66 /// to the same pad, either because we're merging several input sdigit files
67 /// or with a single file because the sdigitizer does not merge sdigits itself
68 /// (for performance reason mainly, and because anyway we know we have to do it
69 /// here, at the digitization level).
71 /// \author Laurent Aphecetche
72 //-----------------------------------------------------------------------------
78 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
81 //ADDED for trigger noise
82 const AliMUONGeometryTransformer* GetTransformer()
84 return muon()->GetGeometryTransformer();
88 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
91 ClassImp(AliMUONDigitizerV3)
94 //_____________________________________________________________________________
95 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
96 Int_t generateNoisyDigits)
97 : AliDigitizer(manager),
98 fIsInitialized(kFALSE),
99 fCalibrationData(0x0),
100 fTriggerProcessor(0x0),
101 fTriggerEfficiency(0x0),
103 fNoiseFunctionTrig(0x0),
104 fGenerateNoisyDigits(generateNoisyDigits),
105 fLogger(new AliMUONLogger(1000)),
106 fTriggerStore(new AliMUONTriggerStoreV1),
108 fOutputDigitStore(0x0)
112 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
115 //_____________________________________________________________________________
116 AliMUONDigitizerV3::~AliMUONDigitizerV3()
118 /// Dtor. Note we're the owner of some pointers.
122 delete fCalibrationData;
123 delete fTriggerProcessor;
124 delete fNoiseFunction;
125 delete fNoiseFunctionTrig;
126 delete fTriggerStore;
128 delete fOutputDigitStore;
130 AliInfo("Summary of messages");
136 //_____________________________________________________________________________
138 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
140 /// For tracking digits, starting from an ideal digit's charge, we :
142 /// - "divide" by a gain (thus decalibrating the digit)
143 /// - add a pedestal (thus decalibrating the digit)
144 /// - add some electronics noise (thus leading to a realistic adc), if requested to do so
145 /// - sets the signal to zero if below 3*sigma of the noise
147 Float_t charge = digit.Charge();
149 // We set the charge to 0, as the only relevant piece of information
150 // after Digitization is the ADC value.
153 Int_t detElemId = digit.DetElemId();
154 Int_t manuId = digit.ManuId();
156 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
159 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
166 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
169 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
176 Int_t manuChannel = digit.ManuChannel();
180 Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise);
185 //_____________________________________________________________________________
187 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
188 AliMUONVDigit& digit)
190 /// \todo add comment
192 if ( !fTriggerEfficiency ) return;
194 if (digit.IsEfficiencyApplied()) return;
196 AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
198 if (!correspondingDigit) return; //reject bad correspondences
200 Int_t detElemId = digit.DetElemId();
202 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
203 const AliMpVSegmentation* segment[2] =
205 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())),
206 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
211 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
212 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
216 if (digit.Cathode()==0) p0=0;
218 AliMpIntPair location = pad[p0].GetLocation(0);
219 Int_t nboard = location.GetFirst();
223 fTriggerEfficiency->IsTriggered(detElemId, nboard,
224 isTrig[0], isTrig[1]);
225 digit.EfficiencyApplied(kTRUE);
226 correspondingDigit->EfficiencyApplied(kTRUE);
228 if (!isTrig[digit.Cathode()])
233 if ( &digit != correspondingDigit )
235 if (!isTrig[correspondingDigit->Cathode()])
237 correspondingDigit->SetCharge(0);
242 //_____________________________________________________________________________
244 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
245 AliMUONVDigitStore& filteredStore)
247 /// Loop over all chamber digits, and apply the response to them
248 /// Note that this method may remove digits.
250 filteredStore.Clear();
252 const Bool_t kAddNoise = kTRUE;
254 TIter next(store.CreateIterator());
255 AliMUONVDigit* digit;
257 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
259 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
261 if ( stationType != AliMp::kStationTrigger )
263 ApplyResponseToTrackerDigit(*digit,kAddNoise);
267 ApplyResponseToTriggerDigit(store,*digit);
269 if ( digit->ADC() > 0 || digit->Charge() > 0 )
271 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
276 //_____________________________________________________________________________
278 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
279 const AliMUONVCalibParam& gains,
284 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
285 /// pedestal and gain parameters.
286 /// Must insure before calling that channel is valid (i.e. between 0 and
287 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
288 /// otherwise results are not predictible...)
290 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
292 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
293 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
295 Float_t a0 = gains.ValueAsFloat(channel,0);
296 Float_t a1 = gains.ValueAsFloat(channel,1);
297 Int_t thres = gains.ValueAsInt(channel,2);
298 Int_t qual = gains.ValueAsInt(channel,3);
299 if ( qual <= 0 ) return 0;
301 Float_t chargeThres = a0*thres;
303 Float_t padc(0); // (adc - ped) value
305 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
309 if ( TMath::Abs(a0) > 1E-12 )
316 // linear + parabolic part
317 Double_t qt = chargeThres - charge;
318 Double_t delta = a0*a0-4*a1*qt;
321 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
322 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
323 delta,pedestals.ID0(),pedestals.ID1(),
324 channel, charge, a0, a1, thres, pedestalMean,
329 delta = TMath::Sqrt(delta);
331 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
337 if ( TMath::Abs(padc) > 1E-3)
339 // this is more than a precision problem : let's signal it !
340 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
341 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
342 padc,pedestals.ID0(),pedestals.ID1(),
343 channel, charge, a0, a1, thres, pedestalMean,
344 pedestalSigma,delta));
347 // ok. consider we're just at thres, let it be zero.
360 Float_t adcNoise = 0.0;
362 if ( addNoise ) adcNoise = gRandom->Gaus(0.0,pedestalSigma);
364 adc = TMath::Nint(padc + pedestalMean + adcNoise);
367 // be sure we stick to 12 bits.
376 //_____________________________________________________________________________
378 AliMUONDigitizerV3::Exec(Option_t*)
381 /// We first loop over input files, and merge the sdigits we found there.
382 /// Second, we digitize all the resulting sdigits
383 /// Then we generate noise-only digits (for tracker only)
384 /// And we finally generate the trigger outputs.
388 AliDebug(1, "Running digitizer.");
390 if ( fManager->GetNinputs() == 0 )
392 AliWarning("No input set. Nothing to do.");
396 if ( !fIsInitialized )
398 AliError("Not initialized. Cannot perform the work. Sorry");
402 Int_t nInputFiles = fManager->GetNinputs();
404 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
406 outputLoader->MakeDigitsContainer();
408 TTree* oTreeD = outputLoader->TreeD();
412 AliFatal("Cannot create output TreeD");
415 // Loop over all the input files, and merge the sdigits found in those
418 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
420 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
422 inputLoader->LoadSDigits("READ");
424 TTree* iTreeS = inputLoader->TreeS();
427 AliFatal(Form("Could not get access to input file #%d",iFile));
430 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
431 inputStore->Connect(*iTreeS);
435 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
437 inputLoader->UnloadSDigits();
442 // At this point, we do have digit arrays (one per chamber) which contains
443 // the merging of all the sdigits of the input file(s).
444 // We now massage them to apply the detector response, i.e. this
445 // is here that we do the "digitization" work.
447 if (!fOutputDigitStore)
449 fOutputDigitStore = fDigitStore->Create();
452 if ( fGenerateNoisyDigits>=2 )
454 // Generate noise-only digits for trigger.
455 GenerateNoisyDigitsForTrigger(*fDigitStore);
458 ApplyResponse(*fDigitStore,*fOutputDigitStore);
460 if ( fGenerateNoisyDigits )
462 // Generate noise-only digits for tracker.
463 GenerateNoisyDigits(*fOutputDigitStore);
466 // We generate the global and local trigger decisions.
467 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
469 // Prepare output tree
470 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
471 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
474 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
478 // Fill the output treeD
481 // Write to the output tree(D).
482 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
483 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
485 outputLoader->WriteDigits("OVERWRITE");
487 outputLoader->UnloadDigits();
489 // Finally, we clean up after ourselves.
490 fTriggerStore->Clear();
491 fDigitStore->Clear();
492 fOutputDigitStore->Clear();
495 //_____________________________________________________________________________
497 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
498 AliMUONVDigit& digit) const
500 /// Find, if it exists, the digit corresponding to digit.Hit(), in the
505 TIter next(digitStore.CreateIterator());
508 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
510 if ( d->DetElemId() == digit.DetElemId() &&
511 d->Hit() == digit.Hit() &&
512 d->Cathode() != digit.Cathode() )
521 //_____________________________________________________________________________
523 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
525 /// According to a given probability, generate digits that
526 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
527 /// that are "only noise".
531 if ( !fNoiseFunction )
533 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
534 fgkNSigmas,fgkNSigmas*10);
536 fNoiseFunction->SetParameters(1,0,1);
539 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
545 while ( !it.IsDone() )
547 for ( Int_t cathode = 0; cathode < 2; ++cathode )
549 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
556 //_____________________________________________________________________________
558 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
559 Int_t detElemId, Int_t cathode)
561 /// Generate noise-only digits for one cathode of one detection element.
562 /// Called by GenerateNoisyDigits()
564 const AliMpVSegmentation* seg
565 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
566 Int_t nofPads = seg->NofPads();
568 Int_t maxIx = seg->MaxPadIndexX();
569 Int_t maxIy = seg->MaxPadIndexY();
571 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
573 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
574 if ( !nofNoisyPads ) return;
577 TMath::Nint(gRandom->Gaus(nofNoisyPads,
578 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
580 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
582 for ( Int_t i = 0; i < nofNoisyPads; ++i )
589 ix = gRandom->Integer(maxIx+1);
590 iy = gRandom->Integer(maxIy+1);
591 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
592 } while ( !pad.IsValid() );
594 Int_t manuId = pad.GetLocation().GetFirst();
595 Int_t manuChannel = pad.GetLocation().GetSecond();
597 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
601 // no pedestal available for this channel, simply give up
605 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
609 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
610 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
612 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
614 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
616 ApplyResponseToTrackerDigit(*d,kFALSE);
619 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
620 // this can happen (that we randomly chose a digit that is
621 // already there). We simply ignore this, but log the occurence
622 // to cross-check that it's not too frequent.
625 fLogger->Log("Collision while adding noiseOnly digit");
629 fLogger->Log("Added noiseOnly digit");
634 AliError("Pure noise below threshold. This should not happen. Not adding "
642 //_____________________________________________________________________________
644 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
646 /// Generate noise-only digits for one cathode of one detection element.
647 /// Called by GenerateNoisyDigits()
649 if ( !fNoiseFunctionTrig )
651 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
654 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
658 AliMUONVDigit *d[2]={0x0};
660 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
663 Int_t nofNoisyPads = 50;
665 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
668 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
670 for ( Int_t i = 0; i < nofNoisyPads; ++i )
672 //printf("Generating noise %i\n",i);
675 Bool_t isOk = kFALSE;
676 Int_t detElemId = -1;
678 //r = gRandom->Landau(9.85026, 9.35881e-02);
679 r = fNoiseFunctionTrig->GetRandom();
680 fi = 2. * TMath::Pi() * gRandom->Rndm();
681 //printf("r = %f\tfi = %f\n", r, fi);
682 gx = r * TMath::Cos(fi);
683 gy = r * TMath::Sin(fi);
685 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
686 Int_t currDetElemId = it.CurrentDEId();
687 const AliMpVSegmentation* seg
688 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
690 Float_t deltax = seg->Dimensions().X();
691 Float_t deltay = seg->Dimensions().Y();
692 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
693 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
694 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
703 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
704 detElemId = currDetElemId;
705 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
706 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
707 if(!pad[0].IsValid()) continue;
714 const AliMpVSegmentation* seg1
715 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
716 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
718 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
719 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
720 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
721 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
722 ix = pad[cathode].GetIndices().GetFirst();
723 iy = pad[cathode].GetIndices().GetSecond();
724 d[cathode]->SetPadXY(ix,iy);
725 //d[cathode].SetSignal(1);
726 //d[cathode].SetPhysicsSignal(0);
727 d[cathode]->SetCharge(1);
728 d[cathode]->NoiseOnly(kTRUE);
729 AliDebug(3,Form("Adding a pure noise digit :"));
731 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
734 fLogger->Log("Collision while adding TriggerNoise digit");
738 fLogger->Log("Added triggerNoise digit");
741 } // loop on noisy pads
742 } // loop on chambers
746 //_____________________________________________________________________________
748 AliMUONDigitizerV3::GetLoader(const TString& folderName)
750 /// Get a MUON loader
752 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
753 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
756 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
762 //_____________________________________________________________________________
764 AliMUONDigitizerV3::Init()
766 /// Initialization of the TTask :
767 /// a) create the calibrationData, according to run number
768 /// b) create the trigger processing task
772 if ( fIsInitialized )
774 AliError("Object already initialized.");
780 AliError("fManager is null !");
784 Int_t runnumber = AliCDBManager::Instance()->GetRun();
786 if ( ! AliMpCDB::LoadMpSegmentation() )
788 AliFatal("Could not access mapping from OCDB !");
791 if ( ! AliMpCDB::LoadDDLStore() )
793 AliFatal("Could not access DDL Store from OCDB !");
796 fCalibrationData = new AliMUONCalibrationData(runnumber);
797 if ( !fCalibrationData->Pedestals() )
799 AliFatal("Could not access pedestals from OCDB !");
801 if ( !fCalibrationData->Gains() )
803 AliFatal("Could not access gains from OCDB !");
805 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
807 if ( muon()->GetTriggerEffCells() )
809 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
810 if ( fTriggerEfficiency )
812 AliDebug(1, "Will apply trigger efficiency");
816 AliFatal("I was requested to apply trigger efficiency, but I could "
821 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
822 (fGenerateNoisyDigits ? "":"NOT")));
824 fIsInitialized = kTRUE;
828 //_____________________________________________________________________________
830 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
831 const AliMUONVDigitStore& input,
834 /// Merge the sdigits in inputData with the digits already present in outputData
836 if ( !outputStore ) outputStore = input.Create();
838 TIter next(input.CreateIterator());
839 AliMUONVDigit* sdigit;
841 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
843 // Update the track references using the mask.
844 // FIXME: this is dirty, for backward compatibility only.
845 // Should re-design all this way of keeping track of MC information...
846 if ( mask ) sdigit->PatchTracks(mask);
847 // Then add or update the digit to the output.
848 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
851 AliError("Could not add digit in merge mode");