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),
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::ApplyResponse(const AliMUONVDigitStore& store,
188 AliMUONVDigitStore& filteredStore)
190 /// Loop over all chamber digits, and apply the response to them
191 /// Note that this method may remove digits.
193 filteredStore.Clear();
195 const Bool_t kAddNoise = kTRUE;
197 TIter next(store.CreateIterator());
198 AliMUONVDigit* digit;
200 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
202 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
204 if ( stationType != AliMp::kStationTrigger )
206 ApplyResponseToTrackerDigit(*digit,kAddNoise);
209 if ( digit->ADC() > 0 || digit->Charge() > 0 )
211 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
216 //_____________________________________________________________________________
218 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
219 const AliMUONVCalibParam& gains,
224 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
225 /// pedestal and gain parameters.
226 /// Must insure before calling that channel is valid (i.e. between 0 and
227 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
228 /// otherwise results are not predictible...)
230 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
232 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
233 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
235 Float_t a0 = gains.ValueAsFloat(channel,0);
236 Float_t a1 = gains.ValueAsFloat(channel,1);
237 Int_t thres = gains.ValueAsInt(channel,2);
238 Int_t qual = gains.ValueAsInt(channel,3);
239 if ( qual <= 0 ) return 0;
241 Float_t chargeThres = a0*thres;
243 Float_t padc(0); // (adc - ped) value
245 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
249 if ( TMath::Abs(a0) > 1E-12 )
256 // linear + parabolic part
257 Double_t qt = chargeThres - charge;
258 Double_t delta = a0*a0-4*a1*qt;
261 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
262 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
263 delta,pedestals.ID0(),pedestals.ID1(),
264 channel, charge, a0, a1, thres, pedestalMean,
269 delta = TMath::Sqrt(delta);
271 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
277 if ( TMath::Abs(padc) > 1E-3)
279 // this is more than a precision problem : let's signal it !
280 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
281 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
282 padc,pedestals.ID0(),pedestals.ID1(),
283 channel, charge, a0, a1, thres, pedestalMean,
284 pedestalSigma,delta));
287 // ok. consider we're just at thres, let it be zero.
300 Float_t adcNoise = 0.0;
302 if ( addNoise ) adcNoise = gRandom->Gaus(0.0,pedestalSigma);
304 adc = TMath::Nint(padc + pedestalMean + adcNoise);
307 // be sure we stick to 12 bits.
316 //_____________________________________________________________________________
318 AliMUONDigitizerV3::Exec(Option_t*)
321 /// We first loop over input files, and merge the sdigits we found there.
322 /// Second, we digitize all the resulting sdigits
323 /// Then we generate noise-only digits (for tracker only)
324 /// And we finally generate the trigger outputs.
328 AliDebug(1, "Running digitizer.");
330 if ( fManager->GetNinputs() == 0 )
332 AliWarning("No input set. Nothing to do.");
336 if ( !fIsInitialized )
338 AliError("Not initialized. Cannot perform the work. Sorry");
342 Int_t nInputFiles = fManager->GetNinputs();
344 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
346 outputLoader->MakeDigitsContainer();
348 TTree* oTreeD = outputLoader->TreeD();
352 AliFatal("Cannot create output TreeD");
355 // Loop over all the input files, and merge the sdigits found in those
358 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
360 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
362 inputLoader->LoadSDigits("READ");
364 TTree* iTreeS = inputLoader->TreeS();
367 AliFatal(Form("Could not get access to input file #%d",iFile));
370 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
371 inputStore->Connect(*iTreeS);
375 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
377 inputLoader->UnloadSDigits();
382 // At this point, we do have digit arrays (one per chamber) which contains
383 // the merging of all the sdigits of the input file(s).
384 // We now massage them to apply the detector response, i.e. this
385 // is here that we do the "digitization" work.
387 if (!fOutputDigitStore)
389 fOutputDigitStore = fDigitStore->Create();
392 if ( fGenerateNoisyDigits>=2 )
394 // Generate noise-only digits for trigger.
395 GenerateNoisyDigitsForTrigger(*fDigitStore);
398 ApplyResponse(*fDigitStore,*fOutputDigitStore);
400 if ( fGenerateNoisyDigits )
402 // Generate noise-only digits for tracker.
403 GenerateNoisyDigits(*fOutputDigitStore);
406 // We generate the global and local trigger decisions.
407 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
409 // Prepare output tree
410 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
411 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
414 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
418 // Fill the output treeD
421 // Write to the output tree(D).
422 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
423 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
425 outputLoader->WriteDigits("OVERWRITE");
427 outputLoader->UnloadDigits();
429 // Finally, we clean up after ourselves.
430 fTriggerStore->Clear();
431 fDigitStore->Clear();
432 fOutputDigitStore->Clear();
436 //_____________________________________________________________________________
438 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
440 /// According to a given probability, generate digits that
441 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
442 /// that are "only noise".
446 if ( !fNoiseFunction )
448 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
449 fgkNSigmas,fgkNSigmas*10);
451 fNoiseFunction->SetParameters(1,0,1);
454 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
460 while ( !it.IsDone() )
462 for ( Int_t cathode = 0; cathode < 2; ++cathode )
464 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
471 //_____________________________________________________________________________
473 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
474 Int_t detElemId, Int_t cathode)
476 /// Generate noise-only digits for one cathode of one detection element.
477 /// Called by GenerateNoisyDigits()
479 const AliMpVSegmentation* seg
480 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
481 Int_t nofPads = seg->NofPads();
483 Int_t maxIx = seg->MaxPadIndexX();
484 Int_t maxIy = seg->MaxPadIndexY();
486 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
488 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
489 if ( !nofNoisyPads ) return;
492 TMath::Nint(gRandom->Gaus(nofNoisyPads,
493 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
495 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
497 for ( Int_t i = 0; i < nofNoisyPads; ++i )
504 ix = gRandom->Integer(maxIx+1);
505 iy = gRandom->Integer(maxIy+1);
506 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
507 } while ( !pad.IsValid() );
509 Int_t manuId = pad.GetLocation().GetFirst();
510 Int_t manuChannel = pad.GetLocation().GetSecond();
512 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
516 // no pedestal available for this channel, simply give up
520 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
524 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
525 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
527 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
529 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
531 ApplyResponseToTrackerDigit(*d,kFALSE);
534 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
535 // this can happen (that we randomly chose a digit that is
536 // already there). We simply ignore this, but log the occurence
537 // to cross-check that it's not too frequent.
540 fLogger->Log("Collision while adding noiseOnly digit");
544 fLogger->Log("Added noiseOnly digit");
549 AliError("Pure noise below threshold. This should not happen. Not adding "
557 //_____________________________________________________________________________
559 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
561 /// Generate noise-only digits for one cathode of one detection element.
562 /// Called by GenerateNoisyDigits()
564 if ( !fNoiseFunctionTrig )
566 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
569 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
573 AliMUONVDigit *d[2]={0x0};
575 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
578 Int_t nofNoisyPads = 50;
580 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
583 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
585 for ( Int_t i = 0; i < nofNoisyPads; ++i )
587 //printf("Generating noise %i\n",i);
590 Bool_t isOk = kFALSE;
591 Int_t detElemId = -1;
593 //r = gRandom->Landau(9.85026, 9.35881e-02);
594 r = fNoiseFunctionTrig->GetRandom();
595 fi = 2. * TMath::Pi() * gRandom->Rndm();
596 //printf("r = %f\tfi = %f\n", r, fi);
597 gx = r * TMath::Cos(fi);
598 gy = r * TMath::Sin(fi);
600 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
601 Int_t currDetElemId = it.CurrentDEId();
602 const AliMpVSegmentation* seg
603 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
605 Float_t deltax = seg->Dimensions().X();
606 Float_t deltay = seg->Dimensions().Y();
607 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
608 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
609 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
618 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
619 detElemId = currDetElemId;
620 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
621 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
622 if(!pad[0].IsValid()) continue;
629 const AliMpVSegmentation* seg1
630 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
631 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
633 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
634 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
635 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
636 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
637 ix = pad[cathode].GetIndices().GetFirst();
638 iy = pad[cathode].GetIndices().GetSecond();
639 d[cathode]->SetPadXY(ix,iy);
640 //d[cathode].SetSignal(1);
641 //d[cathode].SetPhysicsSignal(0);
642 d[cathode]->SetCharge(1);
643 d[cathode]->NoiseOnly(kTRUE);
644 AliDebug(3,Form("Adding a pure noise digit :"));
646 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
649 fLogger->Log("Collision while adding TriggerNoise digit");
653 fLogger->Log("Added triggerNoise digit");
656 } // loop on noisy pads
657 } // loop on chambers
661 //_____________________________________________________________________________
663 AliMUONDigitizerV3::GetLoader(const TString& folderName)
665 /// Get a MUON loader
667 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
668 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
671 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
677 //_____________________________________________________________________________
679 AliMUONDigitizerV3::Init()
681 /// Initialization of the TTask :
682 /// a) create the calibrationData, according to run number
683 /// b) create the trigger processing task
687 if ( fIsInitialized )
689 AliError("Object already initialized.");
695 AliError("fManager is null !");
699 Int_t runnumber = AliCDBManager::Instance()->GetRun();
702 if ( ! AliMpCDB::LoadDDLStore() ) {
703 AliFatal("Could not access mapping from OCDB !");
706 fCalibrationData = new AliMUONCalibrationData(runnumber);
707 if ( !fCalibrationData->Pedestals() )
709 AliFatal("Could not access pedestals from OCDB !");
711 if ( !fCalibrationData->Gains() )
713 AliFatal("Could not access gains from OCDB !");
715 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
717 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
718 (fGenerateNoisyDigits ? "":"NOT")));
720 fIsInitialized = kTRUE;
724 //_____________________________________________________________________________
726 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
727 const AliMUONVDigitStore& input,
730 /// Merge the sdigits in inputData with the digits already present in outputData
732 if ( !outputStore ) outputStore = input.Create();
734 TIter next(input.CreateIterator());
735 AliMUONVDigit* sdigit;
737 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
739 // Update the track references using the mask.
740 // FIXME: this is dirty, for backward compatibility only.
741 // Should re-design all this way of keeping track of MC information...
742 if ( mask ) sdigit->PatchTracks(mask);
743 // Then add or update the digit to the output.
744 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
747 AliError("Could not add digit in merge mode");