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"
22 #include "AliMUONCalibrationData.h"
23 #include "AliMUONConstants.h"
24 #include "AliMUONDigit.h"
25 #include "AliMUONLogger.h"
26 #include "AliMUONTriggerElectronics.h"
27 #include "AliMUONTriggerStoreV1.h"
28 #include "AliMUONVCalibParam.h"
29 #include "AliMUONVDigitStore.h"
30 #include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
33 #include "AliMpSegmentation.h"
34 #include "AliMpCathodType.h"
35 #include "AliMpConstants.h"
36 #include "AliMpDEIterator.h"
37 #include "AliMpDEManager.h"
39 #include "AliMpStationType.h"
40 #include "AliMpVSegmentation.h"
42 #include "AliCDBManager.h"
43 #include "AliCodeTimer.h"
46 #include "AliRunDigitizer.h"
47 #include "AliLoader.h"
48 #include "AliRunLoader.h"
50 #include <Riostream.h>
59 //-----------------------------------------------------------------------------
60 /// \class AliMUONDigitizerV3
61 /// The digitizer is performing the transformation to go from SDigits (digits
62 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
64 /// The decalibration is performed by doing the reverse operation of the
65 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
67 /// Note also that the digitizer takes care of merging sdigits that belongs
68 /// to the same pad, either because we're merging several input sdigit files
69 /// or with a single file because the sdigitizer does not merge sdigits itself
70 /// (for performance reason mainly, and because anyway we know we have to do it
71 /// here, at the digitization level).
73 /// \author Laurent Aphecetche
74 //-----------------------------------------------------------------------------
80 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
83 //ADDED for trigger noise
84 const AliMUONGeometryTransformer* GetTransformer()
86 return muon()->GetGeometryTransformer();
90 Double_t AliMUONDigitizerV3::fgNSigmas = 4.0;
93 ClassImp(AliMUONDigitizerV3)
96 //_____________________________________________________________________________
97 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
98 Int_t generateNoisyDigits)
99 : AliDigitizer(manager),
100 fIsInitialized(kFALSE),
101 fCalibrationData(0x0),
102 fTriggerProcessor(0x0),
103 fNoiseFunctionTrig(0x0),
104 fGenerateNoisyDigits(generateNoisyDigits),
105 fLogger(new AliMUONLogger(1000)),
106 fTriggerStore(new AliMUONTriggerStoreV1),
108 fOutputDigitStore(0x0),
109 fInputDigitStores(0x0)
113 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
117 //_____________________________________________________________________________
118 AliMUONDigitizerV3::~AliMUONDigitizerV3()
120 /// Dtor. Note we're the owner of some pointers.
124 // delete fCalibrationData;
125 delete fTriggerProcessor;
126 delete fNoiseFunctionTrig;
127 delete fTriggerStore;
129 delete fOutputDigitStore;
130 delete fInputDigitStores;
132 AliInfo("Summary of messages");
138 //_____________________________________________________________________________
140 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
142 /// For tracking digits, starting from an ideal digit's charge, we :
144 /// - "divide" by a gain (thus decalibrating the digit)
145 /// - add a pedestal (thus decalibrating the digit)
146 /// - add some electronics noise (thus leading to a realistic adc), if requested to do so
147 /// - sets the signal to zero if below 3*sigma of the noise
149 Float_t charge = digit.IsChargeInFC() ? digit.Charge()*AliMUONConstants::FC2ADC() : digit.Charge();
151 // We set the charge to 0, as the only relevant piece of information
152 // after Digitization is the ADC value.
155 Int_t detElemId = digit.DetElemId();
156 Int_t manuId = digit.ManuId();
158 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
161 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
168 Int_t manuChannel = digit.ManuChannel();
170 if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() ||
171 pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() )
173 // protection against invalid pedestal value
178 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
181 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
188 Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise,
189 digit.IsNoiseOnly());
194 //_____________________________________________________________________________
196 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
197 AliMUONVDigitStore& filteredStore)
199 /// Loop over all chamber digits, and apply the response to them
200 /// Note that this method may remove digits.
202 filteredStore.Clear();
204 const Bool_t kAddNoise = kTRUE;
206 TIter next(store.CreateIterator());
207 AliMUONVDigit* digit;
209 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
211 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
213 if ( stationType != AliMp::kStationTrigger )
215 Bool_t addNoise = kAddNoise;
216 if (digit->IsConverted()) addNoise = kFALSE; // No need to add extra noise to a converted real digit
217 ApplyResponseToTrackerDigit(*digit,addNoise);
220 if ( digit->ADC() > 0 || digit->Charge() > 0 )
222 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
227 //_____________________________________________________________________________
229 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
230 const AliMUONVCalibParam& gains,
236 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
237 /// pedestal and gain parameters.
238 /// Must insure before calling that channel is valid (i.e. between 0 and
239 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
240 /// otherwise results are not predictible...)
242 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
244 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
245 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
247 AliDebugClass(1,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f",
248 pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma));
250 Float_t a0 = gains.ValueAsFloat(channel,0);
251 Float_t a1 = gains.ValueAsFloat(channel,1);
252 Int_t thres = gains.ValueAsInt(channel,2);
253 Int_t qual = gains.ValueAsInt(channel,3);
255 if ( qual <= 0 ) return 0;
257 Float_t chargeThres = a0*thres;
259 Float_t padc(0); // (adc - ped) value
261 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
265 if ( TMath::Abs(a0) > 1E-12 )
272 // linear + parabolic part
273 Double_t qt = chargeThres - charge;
274 Double_t delta = a0*a0-4*a1*qt;
277 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
278 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
279 delta,pedestals.ID0(),pedestals.ID1(),
280 channel, charge, a0, a1, thres, pedestalMean,
285 delta = TMath::Sqrt(delta);
287 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
293 if ( TMath::Abs(padc) > 1E-3)
295 // this is more than a precision problem : let's signal it !
296 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
297 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
298 padc,pedestals.ID0(),pedestals.ID1(),
299 channel, charge, a0, a1, thres, pedestalMean,
300 pedestalSigma,delta));
303 // ok. consider we're just at thres, let it be zero.
314 Float_t adcNoise = 0.0;
320 adcNoise = NoiseFunction()->GetRandom()*pedestalSigma;
324 adcNoise = gRandom->Gaus(0.0,pedestalSigma);
328 adc = TMath::Nint(padc + pedestalMean + adcNoise + 0.5);
330 if ( adc < TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5) )
332 // this is an error only in specific cases
333 if ( !addNoise || (addNoise && noiseOnly) )
335 AliErrorClass(Form(" DE %04d Manu %04d Channel %02d "
336 " a0 %7.2f a1 %7.2f thres %04d ped %7.2f pedsig %7.2f adcNoise %7.2f "
337 " charge=%7.2f padc=%7.2f adc=%04d ZS=%04d fgNSigmas=%e addNoise %d noiseOnly %d ",
338 pedestals.ID0(),pedestals.ID1(),channel,
339 a0, a1, thres, pedestalMean, pedestalSigma, adcNoise,
341 TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5),
342 fgNSigmas,addNoise,noiseOnly));
348 // be sure we stick to 12 bits.
357 //_____________________________________________________________________________
359 AliMUONDigitizerV3::CreateInputDigitStores()
361 /// Create input digit stores
364 if (fInputDigitStores)
366 AliFatal("Should be called only once !");
369 fInputDigitStores = new TObjArray;
371 fInputDigitStores->SetOwner(kTRUE);
373 for ( Int_t iFile = 0; iFile < fManager->GetNinputs(); ++iFile )
375 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
377 inputLoader->LoadSDigits("READ");
379 TTree* iTreeS = inputLoader->TreeS();
382 AliFatal(Form("Could not get access to input file #%d",iFile));
385 fInputDigitStores->AddAt(AliMUONVDigitStore::Create(*iTreeS),iFile);
389 //_____________________________________________________________________________
391 AliMUONDigitizerV3::Exec(Option_t*)
394 /// We first loop over input files, and merge the sdigits we found there.
395 /// Second, we digitize all the resulting sdigits
396 /// Then we generate noise-only digits (for tracker only)
397 /// And we finally generate the trigger outputs.
399 AliCodeTimerAuto("",0)
401 if ( fManager->GetNinputs() == 0 )
403 AliWarning("No input set. Nothing to do.");
407 if ( !fIsInitialized )
409 AliError("Not initialized. Cannot perform the work. Sorry");
413 Int_t nInputFiles = fManager->GetNinputs();
415 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
417 outputLoader->MakeDigitsContainer();
419 TTree* oTreeD = outputLoader->TreeD();
423 AliFatal("Cannot create output TreeD");
426 // Loop over all the input files, and merge the sdigits found in those
429 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
431 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
433 inputLoader->LoadSDigits("READ");
435 TTree* iTreeS = inputLoader->TreeS();
438 AliFatal(Form("Could not get access to input file #%d",iFile));
441 if (!fInputDigitStores)
443 CreateInputDigitStores();
446 AliMUONVDigitStore* dstore = static_cast<AliMUONVDigitStore*>(fInputDigitStores->At(iFile));
448 dstore->Connect(*iTreeS);
452 MergeWithSDigits(fDigitStore,*dstore,fManager->GetMask(iFile));
454 inputLoader->UnloadSDigits();
460 // At this point, we do have digit arrays (one per chamber) which contains
461 // the merging of all the sdigits of the input file(s).
462 // We now massage them to apply the detector response, i.e. this
463 // is here that we do the "digitization" work.
465 if (!fOutputDigitStore)
467 fOutputDigitStore = fDigitStore->Create();
470 if ( fGenerateNoisyDigits>=2 )
472 // Generate noise-only digits for trigger.
473 GenerateNoisyDigitsForTrigger(*fDigitStore);
475 ApplyResponse(*fDigitStore,*fOutputDigitStore);
477 if ( fGenerateNoisyDigits )
479 // Generate noise-only digits for tracker.
480 GenerateNoisyDigits(*fOutputDigitStore);
483 // We generate the global and local trigger decisions.
484 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
486 // Prepare output tree
487 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
488 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
491 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
495 // Fill the output treeD
498 // Write to the output tree(D).
499 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
500 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
502 outputLoader->WriteDigits("OVERWRITE");
504 outputLoader->UnloadDigits();
506 // Finally, we clean up after ourselves.
507 fTriggerStore->Clear();
508 fDigitStore->Clear();
509 fOutputDigitStore->Clear();
513 //_____________________________________________________________________________
515 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
517 /// According to a given probability, generate digits that
518 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
519 /// that are "only noise".
521 AliCodeTimerAuto("",0)
523 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
529 while ( !it.IsDone() )
531 for ( Int_t cathode = 0; cathode < 2; ++cathode )
533 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
540 //_____________________________________________________________________________
542 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
543 Int_t detElemId, Int_t cathode)
545 /// Generate noise-only digits for one cathode of one detection element.
546 /// Called by GenerateNoisyDigits()
548 const AliMpVSegmentation* seg
549 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
550 Int_t nofPads = seg->NofPads();
552 Int_t maxIx = seg->MaxPadIndexX();
553 Int_t maxIy = seg->MaxPadIndexY();
555 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgNSigmas/TMath::Sqrt(2.0)) / 2. ;
557 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
558 if ( !nofNoisyPads ) return;
561 TMath::Nint(gRandom->Gaus(nofNoisyPads,
562 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
564 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
566 for ( Int_t i = 0; i < nofNoisyPads; ++i )
573 ix = gRandom->Integer(maxIx+1);
574 iy = gRandom->Integer(maxIy+1);
575 pad = seg->PadByIndices(ix,iy,kFALSE);
576 } while ( !pad.IsValid() );
578 Int_t manuId = pad.GetManuId();
579 Int_t manuChannel = pad.GetManuChannel();
581 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
585 // no pedestal available for this channel, simply give up
589 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
593 d->SetCharge(0.0); // charge is zero, the ApplyResponseToTrackerDigit will add the noise
595 ApplyResponseToTrackerDigit(*d,kTRUE);
598 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
599 // this can happen (that we randomly chose a digit that is
600 // already there). We simply ignore this, but log the occurence
601 // to cross-check that it's not too frequent.
604 fLogger->Log("Collision while adding noiseOnly digit");
608 fLogger->Log("Added noiseOnly digit");
616 //_____________________________________________________________________________
618 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
620 /// Generate noise-only digits for one cathode of one detection element.
621 /// Called by GenerateNoisyDigits()
623 if ( !fNoiseFunctionTrig )
625 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
628 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
632 AliMUONVDigit *d[2]={0x0};
634 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
637 Int_t nofNoisyPads = 50;
639 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
642 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
644 for ( Int_t i = 0; i < nofNoisyPads; ++i )
646 //printf("Generating noise %i\n",i);
649 Bool_t isOk = kFALSE;
650 Int_t detElemId = -1;
652 //r = gRandom->Landau(9.85026, 9.35881e-02);
653 r = fNoiseFunctionTrig->GetRandom();
654 fi = 2. * TMath::Pi() * gRandom->Rndm();
655 //printf("r = %f\tfi = %f\n", r, fi);
656 gx = r * TMath::Cos(fi);
657 gy = r * TMath::Sin(fi);
659 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
660 Int_t currDetElemId = it.CurrentDEId();
661 const AliMpVSegmentation* seg
662 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
664 Float_t deltax = seg->GetDimensionX();
665 Float_t deltay = seg->GetDimensionY();
666 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
667 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
668 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
677 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
678 detElemId = currDetElemId;
679 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
680 pad[0] = seg->PadByPosition(x,y,kFALSE);
681 if(!pad[0].IsValid()) continue;
688 const AliMpVSegmentation* seg1
689 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
690 pad[1] = seg1->PadByPosition(x,y,kFALSE);
692 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
693 Int_t manuId = pad[cathode].GetLocalBoardId(0);
694 Int_t manuChannel = pad[cathode].GetLocalBoardChannel(0);
695 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
696 ix = pad[cathode].GetIx();
697 iy = pad[cathode].GetIy();
698 d[cathode]->SetPadXY(ix,iy);
699 //d[cathode].SetSignal(1);
700 //d[cathode].SetPhysicsSignal(0);
701 d[cathode]->SetCharge(1);
702 d[cathode]->NoiseOnly(kTRUE);
703 AliDebug(3,Form("Adding a pure noise digit :"));
705 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
708 fLogger->Log("Collision while adding TriggerNoise digit");
712 fLogger->Log("Added triggerNoise digit");
715 } // loop on noisy pads
716 } // loop on chambers
720 //_____________________________________________________________________________
722 AliMUONDigitizerV3::GetLoader(const TString& folderName)
724 /// Get a MUON loader
726 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
727 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
730 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
736 //_____________________________________________________________________________
738 AliMUONDigitizerV3::Init()
740 /// Initialization of the TTask :
741 /// a) create the calibrationData, according to run number
742 /// b) create the trigger processing task
746 if ( fIsInitialized )
748 AliError("Object already initialized.");
754 AliError("fManager is null !");
759 if ( ! AliMpCDB::LoadDDLStore() ) {
760 AliFatal("Could not access mapping from OCDB !");
763 if (!fCalibrationData)
764 AliFatal("Calibration data object not defined");
766 if ( !fCalibrationData->Pedestals() )
768 AliFatal("Could not access pedestals from OCDB !");
770 if ( !fCalibrationData->Gains() )
772 AliFatal("Could not access gains from OCDB !");
776 AliInfo("Using trigger configuration from CDB");
778 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
780 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
781 (fGenerateNoisyDigits ? "":"NOT")));
783 fIsInitialized = kTRUE;
787 //_____________________________________________________________________________
789 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
790 const AliMUONVDigitStore& input,
793 /// Merge the sdigits in inputData with the digits already present in outputData
795 if ( !outputStore ) outputStore = input.Create();
797 TIter next(input.CreateIterator());
798 AliMUONVDigit* sdigit;
800 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
802 // Update the track references using the mask.
803 // FIXME: this is dirty, for backward compatibility only.
804 // Should re-design all this way of keeping track of MC information...
805 if ( mask ) sdigit->PatchTracks(mask);
806 // Then add or update the digit to the output.
807 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
810 AliError("Could not add digit in merge mode");
815 //_____________________________________________________________________________
817 AliMUONDigitizerV3::NoiseFunction()
819 /// Return noise function
823 f = new TF1("AliMUONDigitizerV3::NoiseFunction","gaus",fgNSigmas,fgNSigmas*10);
824 f->SetParameters(1,0,1);