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"
38 #include "AliMpIntPair.h"
40 #include "AliMpStationType.h"
41 #include "AliMpVSegmentation.h"
43 #include "AliCDBManager.h"
44 #include "AliCodeTimer.h"
47 #include "AliRunDigitizer.h"
48 #include "AliRunLoader.h"
50 #include <Riostream.h>
58 //-----------------------------------------------------------------------------
59 /// \class AliMUONDigitizerV3
60 /// The digitizer is performing the transformation to go from SDigits (digits
61 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
63 /// The decalibration is performed by doing the reverse operation of the
64 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
66 /// Note also that the digitizer takes care of merging sdigits that belongs
67 /// to the same pad, either because we're merging several input sdigit files
68 /// or with a single file because the sdigitizer does not merge sdigits itself
69 /// (for performance reason mainly, and because anyway we know we have to do it
70 /// here, at the digitization level).
72 /// \author Laurent Aphecetche
73 //-----------------------------------------------------------------------------
79 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
82 //ADDED for trigger noise
83 const AliMUONGeometryTransformer* GetTransformer()
85 return muon()->GetGeometryTransformer();
89 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
92 ClassImp(AliMUONDigitizerV3)
95 //_____________________________________________________________________________
96 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
97 Int_t generateNoisyDigits)
98 : AliDigitizer(manager),
99 fIsInitialized(kFALSE),
100 fCalibrationData(0x0),
101 fTriggerProcessor(0x0),
102 fNoiseFunctionTrig(0x0),
103 fGenerateNoisyDigits(generateNoisyDigits),
104 fLogger(new AliMUONLogger(1000)),
105 fTriggerStore(new AliMUONTriggerStoreV1),
107 fOutputDigitStore(0x0),
108 fInputDigitStore(0x0)
112 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
116 //_____________________________________________________________________________
117 AliMUONDigitizerV3::~AliMUONDigitizerV3()
119 /// Dtor. Note we're the owner of some pointers.
123 // delete fCalibrationData;
124 delete fTriggerProcessor;
125 delete fNoiseFunctionTrig;
126 delete fTriggerStore;
128 delete fOutputDigitStore;
129 delete fInputDigitStore;
131 AliInfo("Summary of messages");
137 //_____________________________________________________________________________
139 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
141 /// For tracking digits, starting from an ideal digit's charge, we :
143 /// - "divide" by a gain (thus decalibrating the digit)
144 /// - add a pedestal (thus decalibrating the digit)
145 /// - add some electronics noise (thus leading to a realistic adc), if requested to do so
146 /// - sets the signal to zero if below 3*sigma of the noise
148 Float_t charge = digit.Charge();
150 // We set the charge to 0, as the only relevant piece of information
151 // after Digitization is the ADC value.
154 Int_t detElemId = digit.DetElemId();
155 Int_t manuId = digit.ManuId();
157 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
160 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
167 Int_t manuChannel = digit.ManuChannel();
169 if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() ||
170 pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() )
172 // protection against invalid pedestal value
177 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
180 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
187 Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise,
188 digit.IsNoiseOnly());
193 //_____________________________________________________________________________
195 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
196 AliMUONVDigitStore& filteredStore)
198 /// Loop over all chamber digits, and apply the response to them
199 /// Note that this method may remove digits.
201 filteredStore.Clear();
203 const Bool_t kAddNoise = kTRUE;
205 TIter next(store.CreateIterator());
206 AliMUONVDigit* digit;
208 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
210 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
212 if ( stationType != AliMp::kStationTrigger )
214 ApplyResponseToTrackerDigit(*digit,kAddNoise);
217 if ( digit->ADC() > 0 || digit->Charge() > 0 )
219 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
224 //_____________________________________________________________________________
226 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
227 const AliMUONVCalibParam& gains,
233 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
234 /// pedestal and gain parameters.
235 /// Must insure before calling that channel is valid (i.e. between 0 and
236 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
237 /// otherwise results are not predictible...)
239 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
241 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
242 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
244 AliDebugClass(1,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f",
245 pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma));
247 Float_t a0 = gains.ValueAsFloat(channel,0);
248 Float_t a1 = gains.ValueAsFloat(channel,1);
249 Int_t thres = gains.ValueAsInt(channel,2);
250 Int_t qual = gains.ValueAsInt(channel,3);
251 if ( qual <= 0 ) return 0;
253 Float_t chargeThres = a0*thres;
255 Float_t padc(0); // (adc - ped) value
257 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
261 if ( TMath::Abs(a0) > 1E-12 )
268 // linear + parabolic part
269 Double_t qt = chargeThres - charge;
270 Double_t delta = a0*a0-4*a1*qt;
273 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
274 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
275 delta,pedestals.ID0(),pedestals.ID1(),
276 channel, charge, a0, a1, thres, pedestalMean,
281 delta = TMath::Sqrt(delta);
283 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
289 if ( TMath::Abs(padc) > 1E-3)
291 // this is more than a precision problem : let's signal it !
292 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
293 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
294 padc,pedestals.ID0(),pedestals.ID1(),
295 channel, charge, a0, a1, thres, pedestalMean,
296 pedestalSigma,delta));
299 // ok. consider we're just at thres, let it be zero.
310 Float_t adcNoise = 0.0;
316 adcNoise = NoiseFunction()->GetRandom()*pedestalSigma;
320 adcNoise = gRandom->Gaus(0.0,pedestalSigma);
324 adc = TMath::Nint(padc + pedestalMean + adcNoise + 0.5);
326 if ( adc < TMath::Nint(pedestalMean + fgkNSigmas*pedestalSigma + 0.5) )
331 // be sure we stick to 12 bits.
340 //_____________________________________________________________________________
342 AliMUONDigitizerV3::CreateInputDigitStore()
344 /// Create an input digit store, and check that all input files
345 /// actually contains the same type of AliMUONVDigitStore
347 fInputDigitStore = 0x0;
349 for ( Int_t iFile = 0; iFile < fManager->GetNinputs(); ++iFile )
351 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
353 inputLoader->LoadSDigits("READ");
355 TTree* iTreeS = inputLoader->TreeS();
358 AliFatal(Form("Could not get access to input file #%d",iFile));
361 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
363 if (!fInputDigitStore)
365 fInputDigitStore = inputStore;
369 if ( inputStore->IsA() != fInputDigitStore->IsA() )
371 AliFatal("Got different types of AliMUONVDigitStore here. Please implement me.");
377 //_____________________________________________________________________________
379 AliMUONDigitizerV3::Exec(Option_t*)
382 /// We first loop over input files, and merge the sdigits we found there.
383 /// Second, we digitize all the resulting sdigits
384 /// Then we generate noise-only digits (for tracker only)
385 /// And we finally generate the trigger outputs.
389 AliDebug(1, "Running digitizer.");
391 if ( fManager->GetNinputs() == 0 )
393 AliWarning("No input set. Nothing to do.");
397 if ( !fIsInitialized )
399 AliError("Not initialized. Cannot perform the work. Sorry");
403 Int_t nInputFiles = fManager->GetNinputs();
405 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
407 outputLoader->MakeDigitsContainer();
409 TTree* oTreeD = outputLoader->TreeD();
413 AliFatal("Cannot create output TreeD");
416 // Loop over all the input files, and merge the sdigits found in those
419 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
421 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
423 inputLoader->LoadSDigits("READ");
425 TTree* iTreeS = inputLoader->TreeS();
428 AliFatal(Form("Could not get access to input file #%d",iFile));
431 if (!fInputDigitStore)
433 CreateInputDigitStore();
435 fInputDigitStore->Connect(*iTreeS);
439 MergeWithSDigits(fDigitStore,*fInputDigitStore,fManager->GetMask(iFile));
441 inputLoader->UnloadSDigits();
443 fInputDigitStore->Clear();
446 // At this point, we do have digit arrays (one per chamber) which contains
447 // the merging of all the sdigits of the input file(s).
448 // We now massage them to apply the detector response, i.e. this
449 // is here that we do the "digitization" work.
451 if (!fOutputDigitStore)
453 fOutputDigitStore = fDigitStore->Create();
456 if ( fGenerateNoisyDigits>=2 )
458 // Generate noise-only digits for trigger.
459 GenerateNoisyDigitsForTrigger(*fDigitStore);
462 ApplyResponse(*fDigitStore,*fOutputDigitStore);
464 if ( fGenerateNoisyDigits )
466 // Generate noise-only digits for tracker.
467 GenerateNoisyDigits(*fOutputDigitStore);
470 // We generate the global and local trigger decisions.
471 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
473 // Prepare output tree
474 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
475 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
478 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
482 // Fill the output treeD
485 // Write to the output tree(D).
486 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
487 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
489 outputLoader->WriteDigits("OVERWRITE");
491 outputLoader->UnloadDigits();
493 // Finally, we clean up after ourselves.
494 fTriggerStore->Clear();
495 fDigitStore->Clear();
496 fOutputDigitStore->Clear();
500 //_____________________________________________________________________________
502 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
504 /// According to a given probability, generate digits that
505 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
506 /// that are "only noise".
510 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
516 while ( !it.IsDone() )
518 for ( Int_t cathode = 0; cathode < 2; ++cathode )
520 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
527 //_____________________________________________________________________________
529 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
530 Int_t detElemId, Int_t cathode)
532 /// Generate noise-only digits for one cathode of one detection element.
533 /// Called by GenerateNoisyDigits()
535 const AliMpVSegmentation* seg
536 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
537 Int_t nofPads = seg->NofPads();
539 Int_t maxIx = seg->MaxPadIndexX();
540 Int_t maxIy = seg->MaxPadIndexY();
542 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
544 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
545 if ( !nofNoisyPads ) return;
548 TMath::Nint(gRandom->Gaus(nofNoisyPads,
549 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
551 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
553 for ( Int_t i = 0; i < nofNoisyPads; ++i )
560 ix = gRandom->Integer(maxIx+1);
561 iy = gRandom->Integer(maxIy+1);
562 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
563 } while ( !pad.IsValid() );
565 Int_t manuId = pad.GetLocation().GetFirst();
566 Int_t manuChannel = pad.GetLocation().GetSecond();
568 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
572 // no pedestal available for this channel, simply give up
576 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
580 d->SetCharge(0.0); // charge is zero, the ApplyResponseToTrackerDigit will add the noise
582 ApplyResponseToTrackerDigit(*d,kTRUE);
585 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
586 // this can happen (that we randomly chose a digit that is
587 // already there). We simply ignore this, but log the occurence
588 // to cross-check that it's not too frequent.
591 fLogger->Log("Collision while adding noiseOnly digit");
595 fLogger->Log("Added noiseOnly digit");
603 //_____________________________________________________________________________
605 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
607 /// Generate noise-only digits for one cathode of one detection element.
608 /// Called by GenerateNoisyDigits()
610 if ( !fNoiseFunctionTrig )
612 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
615 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
619 AliMUONVDigit *d[2]={0x0};
621 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
624 Int_t nofNoisyPads = 50;
626 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
629 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
631 for ( Int_t i = 0; i < nofNoisyPads; ++i )
633 //printf("Generating noise %i\n",i);
636 Bool_t isOk = kFALSE;
637 Int_t detElemId = -1;
639 //r = gRandom->Landau(9.85026, 9.35881e-02);
640 r = fNoiseFunctionTrig->GetRandom();
641 fi = 2. * TMath::Pi() * gRandom->Rndm();
642 //printf("r = %f\tfi = %f\n", r, fi);
643 gx = r * TMath::Cos(fi);
644 gy = r * TMath::Sin(fi);
646 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
647 Int_t currDetElemId = it.CurrentDEId();
648 const AliMpVSegmentation* seg
649 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
651 Float_t deltax = seg->Dimensions().X();
652 Float_t deltay = seg->Dimensions().Y();
653 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
654 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
655 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
664 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
665 detElemId = currDetElemId;
666 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
667 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
668 if(!pad[0].IsValid()) continue;
675 const AliMpVSegmentation* seg1
676 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
677 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
679 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
680 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
681 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
682 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
683 ix = pad[cathode].GetIndices().GetFirst();
684 iy = pad[cathode].GetIndices().GetSecond();
685 d[cathode]->SetPadXY(ix,iy);
686 //d[cathode].SetSignal(1);
687 //d[cathode].SetPhysicsSignal(0);
688 d[cathode]->SetCharge(1);
689 d[cathode]->NoiseOnly(kTRUE);
690 AliDebug(3,Form("Adding a pure noise digit :"));
692 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
695 fLogger->Log("Collision while adding TriggerNoise digit");
699 fLogger->Log("Added triggerNoise digit");
702 } // loop on noisy pads
703 } // loop on chambers
707 //_____________________________________________________________________________
709 AliMUONDigitizerV3::GetLoader(const TString& folderName)
711 /// Get a MUON loader
713 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
714 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
717 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
723 //_____________________________________________________________________________
725 AliMUONDigitizerV3::Init()
727 /// Initialization of the TTask :
728 /// a) create the calibrationData, according to run number
729 /// b) create the trigger processing task
733 if ( fIsInitialized )
735 AliError("Object already initialized.");
741 AliError("fManager is null !");
746 if ( ! AliMpCDB::LoadDDLStore() ) {
747 AliFatal("Could not access mapping from OCDB !");
750 if (!fCalibrationData)
751 AliFatal("Calibration data object not defined");
753 if ( !fCalibrationData->Pedestals() )
755 AliFatal("Could not access pedestals from OCDB !");
757 if ( !fCalibrationData->Gains() )
759 AliFatal("Could not access gains from OCDB !");
763 AliInfo("Using trigger configuration from CDB");
765 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
767 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
768 (fGenerateNoisyDigits ? "":"NOT")));
770 fIsInitialized = kTRUE;
774 //_____________________________________________________________________________
776 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
777 const AliMUONVDigitStore& input,
780 /// Merge the sdigits in inputData with the digits already present in outputData
782 if ( !outputStore ) outputStore = input.Create();
784 TIter next(input.CreateIterator());
785 AliMUONVDigit* sdigit;
787 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
789 // Update the track references using the mask.
790 // FIXME: this is dirty, for backward compatibility only.
791 // Should re-design all this way of keeping track of MC information...
792 if ( mask ) sdigit->PatchTracks(mask);
793 // Then add or update the digit to the output.
794 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
797 AliError("Could not add digit in merge mode");
802 //_____________________________________________________________________________
804 AliMUONDigitizerV3::NoiseFunction()
806 /// Return noise function
810 f = new TF1("AliMUONDigitizerV3::NoiseFunction","gaus",fgkNSigmas,fgkNSigmas*10);
811 f->SetParameters(1,0,1);