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 "AliMUONTriggerElectronics.h"
30 #include "AliMUONTriggerStoreV1.h"
31 #include "AliMUONVCalibParam.h"
32 #include "AliMUONVDigitStore.h"
34 #include "AliMpCathodType.h"
35 #include "AliMpConstants.h"
36 #include "AliMpDEIterator.h"
37 #include "AliMpDEManager.h"
38 #include "AliMpIntPair.h"
40 #include "AliMpSegmentation.h"
41 #include "AliMpStationType.h"
42 #include "AliMpVSegmentation.h"
44 #include "AliRunDigitizer.h"
45 #include "AliRunLoader.h"
46 #include <Riostream.h>
54 #include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
56 //-----------------------------------------------------------------------------
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
71 //-----------------------------------------------------------------------------
77 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
80 //ADDED for trigger noise
81 const AliMUONGeometryTransformer* GetTransformer()
83 return muon()->GetGeometryTransformer();
87 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
90 ClassImp(AliMUONDigitizerV3)
93 //_____________________________________________________________________________
94 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
95 Int_t generateNoisyDigits)
96 : AliDigitizer(manager),
97 fIsInitialized(kFALSE),
98 fCalibrationData(0x0),
99 fTriggerProcessor(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::ApplyResponse(const AliMUONVDigitStore& store,
186 AliMUONVDigitStore& filteredStore)
188 /// Loop over all chamber digits, and apply the response to them
189 /// Note that this method may remove digits.
191 filteredStore.Clear();
193 const Bool_t kAddNoise = kTRUE;
195 TIter next(store.CreateIterator());
196 AliMUONVDigit* digit;
198 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
200 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
202 if ( stationType != AliMp::kStationTrigger )
204 ApplyResponseToTrackerDigit(*digit,kAddNoise);
207 if ( digit->ADC() > 0 || digit->Charge() > 0 )
209 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
214 //_____________________________________________________________________________
216 AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
217 const AliMUONVCalibParam& gains,
222 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
223 /// pedestal and gain parameters.
224 /// Must insure before calling that channel is valid (i.e. between 0 and
225 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
226 /// otherwise results are not predictible...)
228 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
230 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
231 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
233 Float_t a0 = gains.ValueAsFloat(channel,0);
234 Float_t a1 = gains.ValueAsFloat(channel,1);
235 Int_t thres = gains.ValueAsInt(channel,2);
236 Int_t qual = gains.ValueAsInt(channel,3);
237 if ( qual <= 0 ) return 0;
239 Float_t chargeThres = a0*thres;
241 Float_t padc(0); // (adc - ped) value
243 if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
247 if ( TMath::Abs(a0) > 1E-12 )
254 // linear + parabolic part
255 Double_t qt = chargeThres - charge;
256 Double_t delta = a0*a0-4*a1*qt;
259 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
260 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
261 delta,pedestals.ID0(),pedestals.ID1(),
262 channel, charge, a0, a1, thres, pedestalMean,
267 delta = TMath::Sqrt(delta);
269 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
275 if ( TMath::Abs(padc) > 1E-3)
277 // this is more than a precision problem : let's signal it !
278 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
279 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
280 padc,pedestals.ID0(),pedestals.ID1(),
281 channel, charge, a0, a1, thres, pedestalMean,
282 pedestalSigma,delta));
285 // ok. consider we're just at thres, let it be zero.
298 Float_t adcNoise = 0.0;
300 if ( addNoise ) adcNoise = gRandom->Gaus(0.0,pedestalSigma);
302 adc = TMath::Nint(padc + pedestalMean + adcNoise);
305 // be sure we stick to 12 bits.
314 //_____________________________________________________________________________
316 AliMUONDigitizerV3::Exec(Option_t*)
319 /// We first loop over input files, and merge the sdigits we found there.
320 /// Second, we digitize all the resulting sdigits
321 /// Then we generate noise-only digits (for tracker only)
322 /// And we finally generate the trigger outputs.
326 AliDebug(1, "Running digitizer.");
328 if ( fManager->GetNinputs() == 0 )
330 AliWarning("No input set. Nothing to do.");
334 if ( !fIsInitialized )
336 AliError("Not initialized. Cannot perform the work. Sorry");
340 Int_t nInputFiles = fManager->GetNinputs();
342 AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
344 outputLoader->MakeDigitsContainer();
346 TTree* oTreeD = outputLoader->TreeD();
350 AliFatal("Cannot create output TreeD");
353 // Loop over all the input files, and merge the sdigits found in those
356 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
358 AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
360 inputLoader->LoadSDigits("READ");
362 TTree* iTreeS = inputLoader->TreeS();
365 AliFatal(Form("Could not get access to input file #%d",iFile));
368 AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
369 inputStore->Connect(*iTreeS);
373 MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
375 inputLoader->UnloadSDigits();
380 // At this point, we do have digit arrays (one per chamber) which contains
381 // the merging of all the sdigits of the input file(s).
382 // We now massage them to apply the detector response, i.e. this
383 // is here that we do the "digitization" work.
385 if (!fOutputDigitStore)
387 fOutputDigitStore = fDigitStore->Create();
390 if ( fGenerateNoisyDigits>=2 )
392 // Generate noise-only digits for trigger.
393 GenerateNoisyDigitsForTrigger(*fDigitStore);
396 ApplyResponse(*fDigitStore,*fOutputDigitStore);
398 if ( fGenerateNoisyDigits )
400 // Generate noise-only digits for tracker.
401 GenerateNoisyDigits(*fOutputDigitStore);
404 // We generate the global and local trigger decisions.
405 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
407 // Prepare output tree
408 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
409 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
412 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
416 // Fill the output treeD
419 // Write to the output tree(D).
420 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
421 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
423 outputLoader->WriteDigits("OVERWRITE");
425 outputLoader->UnloadDigits();
427 // Finally, we clean up after ourselves.
428 fTriggerStore->Clear();
429 fDigitStore->Clear();
430 fOutputDigitStore->Clear();
434 //_____________________________________________________________________________
436 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
438 /// According to a given probability, generate digits that
439 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
440 /// that are "only noise".
444 if ( !fNoiseFunction )
446 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
447 fgkNSigmas,fgkNSigmas*10);
449 fNoiseFunction->SetParameters(1,0,1);
452 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
458 while ( !it.IsDone() )
460 for ( Int_t cathode = 0; cathode < 2; ++cathode )
462 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
469 //_____________________________________________________________________________
471 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
472 Int_t detElemId, Int_t cathode)
474 /// Generate noise-only digits for one cathode of one detection element.
475 /// Called by GenerateNoisyDigits()
477 const AliMpVSegmentation* seg
478 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
479 Int_t nofPads = seg->NofPads();
481 Int_t maxIx = seg->MaxPadIndexX();
482 Int_t maxIy = seg->MaxPadIndexY();
484 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
486 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
487 if ( !nofNoisyPads ) return;
490 TMath::Nint(gRandom->Gaus(nofNoisyPads,
491 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
493 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
495 for ( Int_t i = 0; i < nofNoisyPads; ++i )
502 ix = gRandom->Integer(maxIx+1);
503 iy = gRandom->Integer(maxIy+1);
504 pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
505 } while ( !pad.IsValid() );
507 Int_t manuId = pad.GetLocation().GetFirst();
508 Int_t manuChannel = pad.GetLocation().GetSecond();
510 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
514 // no pedestal available for this channel, simply give up
518 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
522 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
523 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
525 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
527 d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
529 ApplyResponseToTrackerDigit(*d,kFALSE);
532 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
533 // this can happen (that we randomly chose a digit that is
534 // already there). We simply ignore this, but log the occurence
535 // to cross-check that it's not too frequent.
538 fLogger->Log("Collision while adding noiseOnly digit");
542 fLogger->Log("Added noiseOnly digit");
547 AliError("Pure noise below threshold. This should not happen. Not adding "
555 //_____________________________________________________________________________
557 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
559 /// Generate noise-only digits for one cathode of one detection element.
560 /// Called by GenerateNoisyDigits()
562 if ( !fNoiseFunctionTrig )
564 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
567 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
571 AliMUONVDigit *d[2]={0x0};
573 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
576 Int_t nofNoisyPads = 50;
578 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
581 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
583 for ( Int_t i = 0; i < nofNoisyPads; ++i )
585 //printf("Generating noise %i\n",i);
588 Bool_t isOk = kFALSE;
589 Int_t detElemId = -1;
591 //r = gRandom->Landau(9.85026, 9.35881e-02);
592 r = fNoiseFunctionTrig->GetRandom();
593 fi = 2. * TMath::Pi() * gRandom->Rndm();
594 //printf("r = %f\tfi = %f\n", r, fi);
595 gx = r * TMath::Cos(fi);
596 gy = r * TMath::Sin(fi);
598 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
599 Int_t currDetElemId = it.CurrentDEId();
600 const AliMpVSegmentation* seg
601 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
603 Float_t deltax = seg->Dimensions().X();
604 Float_t deltay = seg->Dimensions().Y();
605 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
606 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
607 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
616 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
617 detElemId = currDetElemId;
618 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
619 pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
620 if(!pad[0].IsValid()) continue;
627 const AliMpVSegmentation* seg1
628 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
629 pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
631 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
632 Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
633 Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();
634 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
635 ix = pad[cathode].GetIndices().GetFirst();
636 iy = pad[cathode].GetIndices().GetSecond();
637 d[cathode]->SetPadXY(ix,iy);
638 //d[cathode].SetSignal(1);
639 //d[cathode].SetPhysicsSignal(0);
640 d[cathode]->SetCharge(1);
641 d[cathode]->NoiseOnly(kTRUE);
642 AliDebug(3,Form("Adding a pure noise digit :"));
644 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
647 fLogger->Log("Collision while adding TriggerNoise digit");
651 fLogger->Log("Added triggerNoise digit");
654 } // loop on noisy pads
655 } // loop on chambers
659 //_____________________________________________________________________________
661 AliMUONDigitizerV3::GetLoader(const TString& folderName)
663 /// Get a MUON loader
665 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
666 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
669 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
675 //_____________________________________________________________________________
677 AliMUONDigitizerV3::Init()
679 /// Initialization of the TTask :
680 /// a) create the calibrationData, according to run number
681 /// b) create the trigger processing task
685 if ( fIsInitialized )
687 AliError("Object already initialized.");
693 AliError("fManager is null !");
697 Int_t runnumber = AliCDBManager::Instance()->GetRun();
699 if ( ! AliMpCDB::LoadMpSegmentation() )
701 AliFatal("Could not access mapping from OCDB !");
704 if ( ! AliMpCDB::LoadDDLStore() )
706 AliFatal("Could not access DDL Store from OCDB !");
709 fCalibrationData = new AliMUONCalibrationData(runnumber);
710 if ( !fCalibrationData->Pedestals() )
712 AliFatal("Could not access pedestals from OCDB !");
714 if ( !fCalibrationData->Gains() )
716 AliFatal("Could not access gains from OCDB !");
718 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
720 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
721 (fGenerateNoisyDigits ? "":"NOT")));
723 fIsInitialized = kTRUE;
727 //_____________________________________________________________________________
729 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
730 const AliMUONVDigitStore& input,
733 /// Merge the sdigits in inputData with the digits already present in outputData
735 if ( !outputStore ) outputStore = input.Create();
737 TIter next(input.CreateIterator());
738 AliMUONVDigit* sdigit;
740 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
742 // Update the track references using the mask.
743 // FIXME: this is dirty, for backward compatibility only.
744 // Should re-design all this way of keeping track of MC information...
745 if ( mask ) sdigit->PatchTracks(mask);
746 // Then add or update the digit to the output.
747 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
750 AliError("Could not add digit in merge mode");