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 /// \class AliMUONDigitizerV3
58 /// The digitizer is performing the transformation to go from SDigits (digits
59 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
61 /// The decalibration is performed by doing the reverse operation of the
62 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
64 /// Note also that the digitizer takes care of merging sdigits that belongs
65 /// to the same pad, either because we're merging several input sdigit files
66 /// or with a single file because the sdigitizer does not merge sdigits itself
67 /// (for performance reason mainly, and because anyway we know we have to do it
68 /// here, at the digitization level).
70 /// \author Laurent Aphecetche
76 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
79 //ADDED for trigger noise
80 const AliMUONGeometryTransformer* GetTransformer()
82 return muon()->GetGeometryTransformer();
86 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
89 ClassImp(AliMUONDigitizerV3)
92 //_____________________________________________________________________________
93 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
94 Int_t generateNoisyDigits)
95 : AliDigitizer(manager),
96 fIsInitialized(kFALSE),
97 fCalibrationData(0x0),
98 fTriggerProcessor(0x0),
99 fTriggerEfficiency(0x0),
101 fNoiseFunctionTrig(0x0),
102 fGenerateNoisyDigits(generateNoisyDigits),
103 fLogger(new AliMUONLogger(1000)),
104 fTriggerStore(new AliMUONTriggerStoreV1),
106 fOutputDigitStore(0x0)
110 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
113 //_____________________________________________________________________________
114 AliMUONDigitizerV3::~AliMUONDigitizerV3()
116 /// Dtor. Note we're the owner of some pointers.
120 delete fCalibrationData;
121 delete fTriggerProcessor;
122 delete fNoiseFunction;
123 delete fNoiseFunctionTrig;
124 delete fTriggerStore;
126 delete fOutputDigitStore;
128 AliInfo("Summary of messages");
134 //_____________________________________________________________________________
136 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
138 /// For tracking digits, starting from an ideal digit's charge, we :
140 /// - "divide" by a gain (thus decalibrating the digit)
141 /// - add a pedestal (thus decalibrating the digit)
142 /// - add some electronics noise (thus leading to a realistic adc), if requested to do so
143 /// - sets the signal to zero if below 3*sigma of the noise
145 Float_t charge = digit.Charge();
147 // We set the charge to 0, as the only relevant piece of information
148 // after Digitization is the ADC value.
151 Int_t detElemId = digit.DetElemId();
152 Int_t manuId = digit.ManuId();
154 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
157 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
164 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
167 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
174 Int_t manuChannel = digit.ManuChannel();
178 Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise);
183 //_____________________________________________________________________________
185 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
186 AliMUONVDigit& digit)
188 /// \todo add comment
190 if ( !fTriggerEfficiency ) return;
192 if (digit.IsEfficiencyApplied()) return;
194 AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
196 if (!correspondingDigit) return; //reject bad correspondences
198 Int_t detElemId = digit.DetElemId();
200 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
201 const AliMpVSegmentation* segment[2] =
203 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())),
204 segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
209 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
210 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
214 if (digit.Cathode()==0) p0=0;
216 AliMpIntPair location = pad[p0].GetLocation(0);
217 Int_t nboard = location.GetFirst();
221 fTriggerEfficiency->IsTriggered(detElemId, nboard,
222 isTrig[0], isTrig[1]);
223 digit.EfficiencyApplied(kTRUE);
224 correspondingDigit->EfficiencyApplied(kTRUE);
226 if (!isTrig[digit.Cathode()])
231 if ( &digit != correspondingDigit )
233 if (!isTrig[correspondingDigit->Cathode()])
235 correspondingDigit->SetCharge(0);
240 //_____________________________________________________________________________
242 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
243 AliMUONVDigitStore& filteredStore)
245 /// Loop over all chamber digits, and apply the response to them
246 /// Note that this method may remove digits.
248 filteredStore.Clear();
250 const Bool_t kAddNoise = kTRUE;
252 TIter next(store.CreateIterator());
253 AliMUONVDigit* digit;
255 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
257 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
259 if ( stationType != AliMp::kStationTrigger )
261 ApplyResponseToTrackerDigit(*digit,kAddNoise);
265 ApplyResponseToTriggerDigit(store,*digit);
267 if ( digit->ADC() > 0 || digit->Charge() > 0 )
269 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
274 //_____________________________________________________________________________
276 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
277 const AliMUONVCalibParam& gains,
282 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
283 /// pedestal and gain parameters.
284 /// Must insure before calling that channel is valid (i.e. between 0 and
285 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
286 /// otherwise results are not predictible...)
288 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
290 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
291 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
293 Float_t a0 = gains.ValueAsFloat(channel,0);
294 Float_t a1 = gains.ValueAsFloat(channel,1);
295 Int_t thres = gains.ValueAsInt(channel,2);
296 Int_t qual = gains.ValueAsInt(channel,3);
297 if ( qual <= 0 ) return 0;
299 Float_t chargeThres = a0*thres;
301 Float_t padc(0); // (adc - ped) value
303 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
307 if ( TMath::Abs(a0) > 1E-12 )
314 // linear + parabolic part
315 Double_t qt = chargeThres - charge;
316 Double_t delta = a0*a0-4*a1*qt;
319 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
320 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
321 delta,pedestals.ID0(),pedestals.ID1(),
322 channel, charge, a0, a1, thres, pedestalMean,
327 delta = TMath::Sqrt(delta);
329 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
335 if ( TMath::Abs(padc) > 1E-3)
337 // this is more than a precision problem : let's signal it !
338 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
339 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
340 padc,pedestals.ID0(),pedestals.ID1(),
341 channel, charge, a0, a1, thres, pedestalMean,
342 pedestalSigma,delta));
345 // ok. consider we're just at thres, let it be zero.
358 Float_t adcNoise = 0.0;
360 if ( addNoise ) adcNoise = gRandom->Gaus(0.0,pedestalSigma);
362 adc = TMath::Nint(padc + pedestalMean + adcNoise);
365 // be sure we stick to 12 bits.
374 //_____________________________________________________________________________
376 AliMUONDigitizerV3::Exec(Option_t*)
379 /// We first loop over input files, and merge the sdigits we found there.
380 /// Second, we digitize all the resulting sdigits
381 /// Then we generate noise-only digits (for tracker only)
382 /// And we finally generate the trigger outputs.
386 AliDebug(1, "Running digitizer.");
388 if ( fManager->GetNinputs() == 0 )
390 AliWarning("No input set. Nothing to do.");
394 if ( !fIsInitialized )
396 AliError("Not initialized. Cannot perform the work. Sorry");
400 Int_t nInputFiles = fManager->GetNinputs();
402 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
404 outputLoader->MakeDigitsContainer();
406 TTree* oTreeD = outputLoader->TreeD();
410 AliFatal("Cannot create output TreeD");
413 // Loop over all the input files, and merge the sdigits found in those
416 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
418 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
420 inputLoader->LoadSDigits("READ");
422 TTree* iTreeS = inputLoader->TreeS();
425 AliFatal(Form("Could not get access to input file #%d",iFile));
428 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
429 inputStore->Connect(*iTreeS);
433 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
435 inputLoader->UnloadSDigits();
440 // At this point, we do have digit arrays (one per chamber) which contains
441 // the merging of all the sdigits of the input file(s).
442 // We now massage them to apply the detector response, i.e. this
443 // is here that we do the "digitization" work.
445 if (!fOutputDigitStore)
447 fOutputDigitStore = fDigitStore->Create();
450 if ( fGenerateNoisyDigits>=2 )
452 // Generate noise-only digits for trigger.
453 GenerateNoisyDigitsForTrigger(*fDigitStore);
456 ApplyResponse(*fDigitStore,*fOutputDigitStore);
458 if ( fGenerateNoisyDigits )
460 // Generate noise-only digits for tracker.
461 GenerateNoisyDigits(*fOutputDigitStore);
464 // We generate the global and local trigger decisions.
465 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
467 // Prepare output tree
468 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
469 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
472 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
476 // Fill the output treeD
479 // Write to the output tree(D).
480 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
481 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
483 outputLoader->WriteDigits("OVERWRITE");
485 outputLoader->UnloadDigits();
487 // Finally, we clean up after ourselves.
488 fTriggerStore->Clear();
489 fDigitStore->Clear();
490 fOutputDigitStore->Clear();
493 //_____________________________________________________________________________
495 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
496 AliMUONVDigit& digit) const
498 /// Find, if it exists, the digit corresponding to digit.Hit(), in the
503 TIter next(digitStore.CreateIterator());
506 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
508 if ( d->DetElemId() == digit.DetElemId() &&
509 d->Hit() == digit.Hit() &&
510 d->Cathode() != digit.Cathode() )
519 //_____________________________________________________________________________
521 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
523 /// According to a given probability, generate digits that
524 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
525 /// that are "only noise".
529 if ( !fNoiseFunction )
531 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
532 fgkNSigmas,fgkNSigmas*10);
534 fNoiseFunction->SetParameters(1,0,1);
537 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
543 while ( !it.IsDone() )
545 for ( Int_t cathode = 0; cathode < 2; ++cathode )
547 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
554 //_____________________________________________________________________________
556 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
557 Int_t detElemId, Int_t cathode)
559 /// Generate noise-only digits for one cathode of one detection element.
560 /// Called by GenerateNoisyDigits()
562 const AliMpVSegmentation* seg
563 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
564 Int_t nofPads = seg->NofPads();
566 Int_t maxIx = seg->MaxPadIndexX();
567 Int_t maxIy = seg->MaxPadIndexY();
569 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
571 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
572 if ( !nofNoisyPads ) return;
575 TMath::Nint(gRandom->Gaus(nofNoisyPads,
576 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
578 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
580 for ( Int_t i = 0; i < nofNoisyPads; ++i )
587 ix = gRandom->Integer(maxIx+1);
588 iy = gRandom->Integer(maxIy+1);
589 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
590 } while ( !pad.IsValid() );
592 Int_t manuId = pad.GetLocation().GetFirst();
593 Int_t manuChannel = pad.GetLocation().GetSecond();
595 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
599 // no pedestal available for this channel, simply give up
603 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
607 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
608 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
610 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
612 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
614 ApplyResponseToTrackerDigit(*d,kFALSE);
617 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
618 // this can happen (that we randomly chose a digit that is
619 // already there). We simply ignore this, but log the occurence
620 // to cross-check that it's not too frequent.
623 fLogger->Log("Collision while adding noiseOnly digit");
627 fLogger->Log("Added noiseOnly digit");
632 AliError("Pure noise below threshold. This should not happen. Not adding "
640 //_____________________________________________________________________________
642 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
644 /// Generate noise-only digits for one cathode of one detection element.
645 /// Called by GenerateNoisyDigits()
647 if ( !fNoiseFunctionTrig )
649 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
652 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
656 AliMUONVDigit *d[2]={0x0};
658 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
661 Int_t nofNoisyPads = 50;
663 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
666 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
668 for ( Int_t i = 0; i < nofNoisyPads; ++i )
670 //printf("Generating noise %i\n",i);
673 Bool_t isOk = kFALSE;
674 Int_t detElemId = -1;
676 //r = gRandom->Landau(9.85026, 9.35881e-02);
677 r = fNoiseFunctionTrig->GetRandom();
678 fi = 2. * TMath::Pi() * gRandom->Rndm();
679 //printf("r = %f\tfi = %f\n", r, fi);
680 gx = r * TMath::Cos(fi);
681 gy = r * TMath::Sin(fi);
683 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
684 Int_t currDetElemId = it.CurrentDEId();
685 const AliMpVSegmentation* seg
686 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
688 Float_t deltax = seg->Dimensions().X();
689 Float_t deltay = seg->Dimensions().Y();
690 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
691 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
692 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
701 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
702 detElemId = currDetElemId;
703 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
704 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
705 if(!pad[0].IsValid()) continue;
712 const AliMpVSegmentation* seg1
713 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
714 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
716 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
717 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
718 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
719 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
720 ix = pad[cathode].GetIndices().GetFirst();
721 iy = pad[cathode].GetIndices().GetSecond();
722 d[cathode]->SetPadXY(ix,iy);
723 //d[cathode].SetSignal(1);
724 //d[cathode].SetPhysicsSignal(0);
725 d[cathode]->SetCharge(1);
726 d[cathode]->NoiseOnly(kTRUE);
727 AliDebug(3,Form("Adding a pure noise digit :"));
729 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
732 fLogger->Log("Collision while adding TriggerNoise digit");
736 fLogger->Log("Added triggerNoise digit");
739 } // loop on noisy pads
740 } // loop on chambers
744 //_____________________________________________________________________________
746 AliMUONDigitizerV3::GetLoader(const TString& folderName)
748 /// Get a MUON loader
750 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
751 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
754 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
760 //_____________________________________________________________________________
762 AliMUONDigitizerV3::Init()
764 /// Initialization of the TTask :
765 /// a) create the calibrationData, according to run number
766 /// b) create the trigger processing task
770 if ( fIsInitialized )
772 AliError("Object already initialized.");
778 AliError("fManager is null !");
782 Int_t runnumber = AliCDBManager::Instance()->GetRun();
784 if ( ! AliMpCDB::LoadMpSegmentation() )
786 AliFatal("Could not access mapping from OCDB !");
789 if ( ! AliMpCDB::LoadDDLStore() )
791 AliFatal("Could not access DDL Store from OCDB !");
794 fCalibrationData = new AliMUONCalibrationData(runnumber);
795 if ( !fCalibrationData->Pedestals() )
797 AliFatal("Could not access pedestals from OCDB !");
799 if ( !fCalibrationData->Gains() )
801 AliFatal("Could not access gains from OCDB !");
803 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
805 if ( muon()->GetTriggerEffCells() )
807 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
808 if ( fTriggerEfficiency )
810 AliDebug(1, "Will apply trigger efficiency");
814 AliFatal("I was requested to apply trigger efficiency, but I could "
819 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
820 (fGenerateNoisyDigits ? "":"NOT")));
822 fIsInitialized = kTRUE;
826 //_____________________________________________________________________________
828 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
829 const AliMUONVDigitStore& input,
832 /// Merge the sdigits in inputData with the digits already present in outputData
834 if ( !outputStore ) outputStore = input.Create();
836 TIter next(input.CreateIterator());
837 AliMUONVDigit* sdigit;
839 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
841 // Update the track references using the mask.
842 // FIXME: this is dirty, for backward compatibility only.
843 // Should re-design all this way of keeping track of MC information...
844 if ( mask ) sdigit->PatchTracks(mask);
845 // Then add or update the digit to the output.
846 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
849 AliError("Could not add digit in merge mode");