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 "AliRunLoader.h"
49 #include <Riostream.h>
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 Double_t AliMUONDigitizerV3::fgNSigmas = 4.0;
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 fNoiseFunctionTrig(0x0),
102 fGenerateNoisyDigits(generateNoisyDigits),
103 fLogger(new AliMUONLogger(1000)),
104 fTriggerStore(new AliMUONTriggerStoreV1),
106 fOutputDigitStore(0x0),
107 fInputDigitStores(0x0)
111 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 fNoiseFunctionTrig;
125 delete fTriggerStore;
127 delete fOutputDigitStore;
128 delete fInputDigitStores;
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.IsChargeInFC() ? digit.Charge()*AliMUONConstants::FC2ADC() : 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 Int_t manuChannel = digit.ManuChannel();
168 if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() ||
169 pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() )
171 // protection against invalid pedestal value
176 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
179 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
186 Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise,
187 digit.IsNoiseOnly());
192 //_____________________________________________________________________________
194 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
195 AliMUONVDigitStore& filteredStore)
197 /// Loop over all chamber digits, and apply the response to them
198 /// Note that this method may remove digits.
200 filteredStore.Clear();
202 const Bool_t kAddNoise = kTRUE;
204 TIter next(store.CreateIterator());
205 AliMUONVDigit* digit;
207 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
209 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
211 if ( stationType != AliMp::kStationTrigger )
213 Bool_t addNoise = kAddNoise;
214 if (digit->IsConverted()) addNoise = kFALSE; // No need to add extra noise to a converted real digit
215 ApplyResponseToTrackerDigit(*digit,addNoise);
218 if ( digit->ADC() > 0 || digit->Charge() > 0 )
220 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
225 //_____________________________________________________________________________
227 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
228 const AliMUONVCalibParam& gains,
234 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
235 /// pedestal and gain parameters.
236 /// Must insure before calling that channel is valid (i.e. between 0 and
237 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
238 /// otherwise results are not predictible...)
240 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
242 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
243 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
245 AliDebugClass(1,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f",
246 pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma));
248 Float_t a0 = gains.ValueAsFloat(channel,0);
249 Float_t a1 = gains.ValueAsFloat(channel,1);
250 Int_t thres = gains.ValueAsInt(channel,2);
251 Int_t qual = gains.ValueAsInt(channel,3);
253 if ( qual <= 0 ) return 0;
255 Float_t chargeThres = a0*thres;
257 Float_t padc(0); // (adc - ped) value
259 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
263 if ( TMath::Abs(a0) > 1E-12 )
270 // linear + parabolic part
271 Double_t qt = chargeThres - charge;
272 Double_t delta = a0*a0-4*a1*qt;
275 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
276 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
277 delta,pedestals.ID0(),pedestals.ID1(),
278 channel, charge, a0, a1, thres, pedestalMean,
283 delta = TMath::Sqrt(delta);
285 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
291 if ( TMath::Abs(padc) > 1E-3)
293 // this is more than a precision problem : let's signal it !
294 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
295 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
296 padc,pedestals.ID0(),pedestals.ID1(),
297 channel, charge, a0, a1, thres, pedestalMean,
298 pedestalSigma,delta));
301 // ok. consider we're just at thres, let it be zero.
312 Float_t adcNoise = 0.0;
318 adcNoise = NoiseFunction()->GetRandom()*pedestalSigma;
322 adcNoise = gRandom->Gaus(0.0,pedestalSigma);
326 adc = TMath::Nint(padc + pedestalMean + adcNoise + 0.5);
328 if ( adc < TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5) )
330 // this is an error only in specific cases
331 if ( !addNoise || (addNoise && noiseOnly) )
333 AliErrorClass(Form(" DE %04d Manu %04d Channel %02d "
334 " a0 %7.2f a1 %7.2f thres %04d ped %7.2f pedsig %7.2f adcNoise %7.2f "
335 " charge=%7.2f padc=%7.2f adc=%04d ZS=%04d fgNSigmas=%e addNoise %d noiseOnly %d ",
336 pedestals.ID0(),pedestals.ID1(),channel,
337 a0, a1, thres, pedestalMean, pedestalSigma, adcNoise,
339 TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5),
340 fgNSigmas,addNoise,noiseOnly));
346 // be sure we stick to 12 bits.
355 //_____________________________________________________________________________
357 AliMUONDigitizerV3::CreateInputDigitStores()
359 /// Create input digit stores
362 if (fInputDigitStores)
364 AliFatal("Should be called only once !");
367 fInputDigitStores = new TObjArray;
369 fInputDigitStores->SetOwner(kTRUE);
371 for ( Int_t iFile = 0; iFile < fManager->GetNinputs(); ++iFile )
373 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
375 inputLoader->LoadSDigits("READ");
377 TTree* iTreeS = inputLoader->TreeS();
380 AliFatal(Form("Could not get access to input file #%d",iFile));
383 fInputDigitStores->AddAt(AliMUONVDigitStore::Create(*iTreeS),iFile);
387 //_____________________________________________________________________________
389 AliMUONDigitizerV3::Exec(Option_t*)
392 /// We first loop over input files, and merge the sdigits we found there.
393 /// Second, we digitize all the resulting sdigits
394 /// Then we generate noise-only digits (for tracker only)
395 /// And we finally generate the trigger outputs.
397 AliCodeTimerAuto("",0)
399 if ( fManager->GetNinputs() == 0 )
401 AliWarning("No input set. Nothing to do.");
405 if ( !fIsInitialized )
407 AliError("Not initialized. Cannot perform the work. Sorry");
411 Int_t nInputFiles = fManager->GetNinputs();
413 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
415 outputLoader->MakeDigitsContainer();
417 TTree* oTreeD = outputLoader->TreeD();
421 AliFatal("Cannot create output TreeD");
424 // Loop over all the input files, and merge the sdigits found in those
427 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
429 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
431 inputLoader->LoadSDigits("READ");
433 TTree* iTreeS = inputLoader->TreeS();
436 AliFatal(Form("Could not get access to input file #%d",iFile));
439 if (!fInputDigitStores)
441 CreateInputDigitStores();
444 AliMUONVDigitStore* dstore = static_cast<AliMUONVDigitStore*>(fInputDigitStores->At(iFile));
446 dstore->Connect(*iTreeS);
450 MergeWithSDigits(fDigitStore,*dstore,fManager->GetMask(iFile));
452 inputLoader->UnloadSDigits();
458 // At this point, we do have digit arrays (one per chamber) which contains
459 // the merging of all the sdigits of the input file(s).
460 // We now massage them to apply the detector response, i.e. this
461 // is here that we do the "digitization" work.
463 if (!fOutputDigitStore)
465 fOutputDigitStore = fDigitStore->Create();
468 if ( fGenerateNoisyDigits>=2 )
470 // Generate noise-only digits for trigger.
471 GenerateNoisyDigitsForTrigger(*fDigitStore);
473 ApplyResponse(*fDigitStore,*fOutputDigitStore);
475 if ( fGenerateNoisyDigits )
477 // Generate noise-only digits for tracker.
478 GenerateNoisyDigits(*fOutputDigitStore);
481 // We generate the global and local trigger decisions.
482 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
484 // Prepare output tree
485 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
486 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
489 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
493 // Fill the output treeD
496 // Write to the output tree(D).
497 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
498 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
500 outputLoader->WriteDigits("OVERWRITE");
502 outputLoader->UnloadDigits();
504 // Finally, we clean up after ourselves.
505 fTriggerStore->Clear();
506 fDigitStore->Clear();
507 fOutputDigitStore->Clear();
511 //_____________________________________________________________________________
513 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
515 /// According to a given probability, generate digits that
516 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
517 /// that are "only noise".
519 AliCodeTimerAuto("",0)
521 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
527 while ( !it.IsDone() )
529 for ( Int_t cathode = 0; cathode < 2; ++cathode )
531 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
538 //_____________________________________________________________________________
540 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
541 Int_t detElemId, Int_t cathode)
543 /// Generate noise-only digits for one cathode of one detection element.
544 /// Called by GenerateNoisyDigits()
546 const AliMpVSegmentation* seg
547 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
548 Int_t nofPads = seg->NofPads();
550 Int_t maxIx = seg->MaxPadIndexX();
551 Int_t maxIy = seg->MaxPadIndexY();
553 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgNSigmas/TMath::Sqrt(2.0)) / 2. ;
555 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
556 if ( !nofNoisyPads ) return;
559 TMath::Nint(gRandom->Gaus(nofNoisyPads,
560 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
562 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
564 for ( Int_t i = 0; i < nofNoisyPads; ++i )
571 ix = gRandom->Integer(maxIx+1);
572 iy = gRandom->Integer(maxIy+1);
573 pad = seg->PadByIndices(ix,iy,kFALSE);
574 } while ( !pad.IsValid() );
576 Int_t manuId = pad.GetManuId();
577 Int_t manuChannel = pad.GetManuChannel();
579 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
583 // no pedestal available for this channel, simply give up
587 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
591 d->SetCharge(0.0); // charge is zero, the ApplyResponseToTrackerDigit will add the noise
593 ApplyResponseToTrackerDigit(*d,kTRUE);
596 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
597 // this can happen (that we randomly chose a digit that is
598 // already there). We simply ignore this, but log the occurence
599 // to cross-check that it's not too frequent.
602 fLogger->Log("Collision while adding noiseOnly digit");
606 fLogger->Log("Added noiseOnly digit");
614 //_____________________________________________________________________________
616 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
618 /// Generate noise-only digits for one cathode of one detection element.
619 /// Called by GenerateNoisyDigits()
621 if ( !fNoiseFunctionTrig )
623 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
626 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
630 AliMUONVDigit *d[2]={0x0};
632 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
635 Int_t nofNoisyPads = 50;
637 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
640 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
642 for ( Int_t i = 0; i < nofNoisyPads; ++i )
644 //printf("Generating noise %i\n",i);
647 Bool_t isOk = kFALSE;
648 Int_t detElemId = -1;
650 //r = gRandom->Landau(9.85026, 9.35881e-02);
651 r = fNoiseFunctionTrig->GetRandom();
652 fi = 2. * TMath::Pi() * gRandom->Rndm();
653 //printf("r = %f\tfi = %f\n", r, fi);
654 gx = r * TMath::Cos(fi);
655 gy = r * TMath::Sin(fi);
657 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
658 Int_t currDetElemId = it.CurrentDEId();
659 const AliMpVSegmentation* seg
660 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
662 Float_t deltax = seg->GetDimensionX();
663 Float_t deltay = seg->GetDimensionY();
664 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
665 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
666 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
675 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
676 detElemId = currDetElemId;
677 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
678 pad[0] = seg->PadByPosition(x,y,kFALSE);
679 if(!pad[0].IsValid()) continue;
686 const AliMpVSegmentation* seg1
687 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
688 pad[1] = seg1->PadByPosition(x,y,kFALSE);
690 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
691 Int_t manuId = pad[cathode].GetLocalBoardId(0);
692 Int_t manuChannel = pad[cathode].GetLocalBoardChannel(0);
693 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
694 ix = pad[cathode].GetIx();
695 iy = pad[cathode].GetIy();
696 d[cathode]->SetPadXY(ix,iy);
697 //d[cathode].SetSignal(1);
698 //d[cathode].SetPhysicsSignal(0);
699 d[cathode]->SetCharge(1);
700 d[cathode]->NoiseOnly(kTRUE);
701 AliDebug(3,Form("Adding a pure noise digit :"));
703 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
706 fLogger->Log("Collision while adding TriggerNoise digit");
710 fLogger->Log("Added triggerNoise digit");
713 } // loop on noisy pads
714 } // loop on chambers
718 //_____________________________________________________________________________
720 AliMUONDigitizerV3::GetLoader(const TString& folderName)
722 /// Get a MUON loader
724 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
725 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
728 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
734 //_____________________________________________________________________________
736 AliMUONDigitizerV3::Init()
738 /// Initialization of the TTask :
739 /// a) create the calibrationData, according to run number
740 /// b) create the trigger processing task
744 if ( fIsInitialized )
746 AliError("Object already initialized.");
752 AliError("fManager is null !");
757 if ( ! AliMpCDB::LoadDDLStore() ) {
758 AliFatal("Could not access mapping from OCDB !");
761 if (!fCalibrationData)
762 AliFatal("Calibration data object not defined");
764 if ( !fCalibrationData->Pedestals() )
766 AliFatal("Could not access pedestals from OCDB !");
768 if ( !fCalibrationData->Gains() )
770 AliFatal("Could not access gains from OCDB !");
774 AliInfo("Using trigger configuration from CDB");
776 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
778 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
779 (fGenerateNoisyDigits ? "":"NOT")));
781 fIsInitialized = kTRUE;
785 //_____________________________________________________________________________
787 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
788 const AliMUONVDigitStore& input,
791 /// Merge the sdigits in inputData with the digits already present in outputData
793 if ( !outputStore ) outputStore = input.Create();
795 TIter next(input.CreateIterator());
796 AliMUONVDigit* sdigit;
798 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
800 // Update the track references using the mask.
801 // FIXME: this is dirty, for backward compatibility only.
802 // Should re-design all this way of keeping track of MC information...
803 if ( mask ) sdigit->PatchTracks(mask);
804 // Then add or update the digit to the output.
805 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
808 AliError("Could not add digit in merge mode");
813 //_____________________________________________________________________________
815 AliMUONDigitizerV3::NoiseFunction()
817 /// Return noise function
821 f = new TF1("AliMUONDigitizerV3::NoiseFunction","gaus",fgNSigmas,fgNSigmas*10);
822 f->SetParameters(1,0,1);