]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONDigitizerV3.cxx
New option to discard mono-cathod clusters (by changing their resolution) and refit...
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitizerV3.cxx
index d391b4002abb38edf2e1e0fc4a21b7b7748fd36c..91fb1e8d2cd0edc6de9c2cbc9333a981b369f8bd 100644 (file)
 
 #include "AliMUONDigitizerV3.h"
 
-#include "AliCDBManager.h"
-#include "AliLog.h"
 #include "AliMUON.h"
 #include "AliMUONCalibrationData.h"
 #include "AliMUONConstants.h"
 #include "AliMUONDigit.h"
 #include "AliMUONLogger.h"
-#include "AliMUONTriggerEfficiencyCells.h"
 #include "AliMUONTriggerElectronics.h"
 #include "AliMUONTriggerStoreV1.h"
 #include "AliMUONVCalibParam.h"
 #include "AliMUONVDigitStore.h"
+#include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
+#include "AliMUONRecoParam.h"
+#include "AliMUONTriggerChamberEfficiency.h"
+#include "AliMUONTriggerUtilities.h"
+
+#include "AliMpCDB.h"
+#include "AliMpSegmentation.h"
 #include "AliMpCathodType.h"
 #include "AliMpConstants.h"
 #include "AliMpDEIterator.h"
 #include "AliMpDEManager.h"
-#include "AliMpDEManager.h"
-#include "AliMpIntPair.h"
 #include "AliMpPad.h"
-#include "AliMpSegmentation.h"
 #include "AliMpStationType.h"
 #include "AliMpVSegmentation.h"
+#include "AliMpDDLStore.h"
+
+#include "AliCDBManager.h"
+#include "AliCodeTimer.h"
+#include "AliLog.h"
 #include "AliRun.h"
-#include "AliRunDigitizer.h"
+#include "AliDigitizationInput.h"
+#include "AliLoader.h"
 #include "AliRunLoader.h"
+
 #include <Riostream.h>
 #include <TF1.h>
 #include <TFile.h>
 #include <TRandom.h>
 #include <TString.h>
 #include <TSystem.h>
+#include <TTree.h>
 
-#include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
-///
+//-----------------------------------------------------------------------------
 /// \class AliMUONDigitizerV3
+///
 /// The digitizer is performing the transformation to go from SDigits (digits
 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
 /// 
 /// (for performance reason mainly, and because anyway we know we have to do it
 /// here, at the digitization level).
 ///
+/// August 2011. In order to remove the need for specific MC OCDB storages,
+/// we're introducing a dependence of simulation on AliMUONRecoParam (stored
+/// in MUON/Calib/RecoParam in OCDB), which is normally (or conceptually, if
+/// you will) only a reconstruction object. That's not a pretty solution, but,
+/// well, we have to do it...
+/// This dependence comes from the fact that we must know how to decalibrate
+/// the digits, so that the decalibration (done here) - calibration (done during
+/// reco) process is (as much as possible) neutral.
+///
+///
 /// \author Laurent Aphecetche
+///
+//-----------------------------------------------------------------------------
 
 namespace
 {
@@ -82,35 +103,35 @@ namespace
   }
 }
 
-const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
+Double_t AliMUONDigitizerV3::fgNSigmas = 4.0;
 
 /// \cond CLASSIMP
 ClassImp(AliMUONDigitizerV3)
 /// \endcond
 
 //_____________________________________________________________________________
-AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager
+AliMUONDigitizerV3::AliMUONDigitizerV3(AliDigitizationInput* digInput
                                        Int_t generateNoisyDigits)
-: AliDigitizer(manager),
+: AliDigitizer(digInput),
 fIsInitialized(kFALSE),
 fCalibrationData(0x0),
 fTriggerProcessor(0x0),
-fTriggerEfficiency(0x0),
-fGenerateNoisyDigitsTimer(),
-fExecTimer(),
-fNoiseFunction(0x0),
 fNoiseFunctionTrig(0x0),
-  fGenerateNoisyDigits(generateNoisyDigits),
-  fLogger(new AliMUONLogger(1000)),
+fGenerateNoisyDigits(generateNoisyDigits),
+fLogger(new AliMUONLogger(4207)), /* 4207 = 25% of the 16828 MCH manus */
 fTriggerStore(new AliMUONTriggerStoreV1),
 fDigitStore(0x0),
-fOutputDigitStore(0x0)
+fOutputDigitStore(0x0),
+fInputDigitStores(0x0),
+fRecoParam(0x0),
+fTriggerEfficiency(0x0),
+fTriggerUtilities(0x0),
+fEfficiencyResponse(2*AliMUONConstants::NTriggerCh()*AliMUONConstants::NTriggerCircuit())
 {
   /// Ctor.
 
-  AliDebug(1,Form("AliRunDigitizer=%p",fManager));
-  fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
-  fExecTimer.Start(kTRUE); fExecTimer.Stop();
+  AliDebug(1,Form("AliDigitizationInput=%p",fDigInput));
+
 }
 
 //_____________________________________________________________________________
@@ -120,23 +141,15 @@ AliMUONDigitizerV3::~AliMUONDigitizerV3()
 
   AliDebug(1,"dtor");
 
-  delete fCalibrationData;
// delete fCalibrationData;
   delete fTriggerProcessor;
-  delete fNoiseFunction;
   delete fNoiseFunctionTrig;
   delete fTriggerStore;
   delete fDigitStore;
   delete fOutputDigitStore;
+  delete fInputDigitStores;
+  delete fTriggerUtilities;
   
-  if ( fGenerateNoisyDigits )
-  {
-    AliDebug(1, Form("Execution time for GenerateNoisyDigits() : R:%.2fs C:%.2fs",
-                 fGenerateNoisyDigitsTimer.RealTime(),
-                 fGenerateNoisyDigitsTimer.CpuTime()));
-  }
-  AliDebug(1, Form("Execution time for Exec() : R:%.2fs C:%.2fs",
-               fExecTimer.RealTime(),fExecTimer.CpuTime()));
   AliInfo("Summary of messages");
   fLogger->Print();
   
@@ -149,29 +162,26 @@ AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t add
 {
   /// For tracking digits, starting from an ideal digit's charge, we :
   ///
-  /// - add some noise (thus leading to a realistic charge), if requested to do so
   /// - "divide" by a gain (thus decalibrating the digit)
   /// - add a pedestal (thus decalibrating the digit)
+  /// - add some electronics noise (thus leading to a realistic adc), if requested to do so
   /// - sets the signal to zero if below 3*sigma of the noise
 
-  static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
-  
   Float_t charge = digit.Charge();
   
-  // We set the charge to 0, as the only relevant piece of information
-  // after Digitization is the ADC value.  
-  digit.SetCharge(0);
-  
-  if ( !addNoise )
+  if (!digit.IsChargeInFC())
   {
-    digit.SetADC(TMath::Min(kMaxADC,TMath::Nint(charge)));
-    return;
+    charge *= AliMUONConstants::DefaultADC2MV()*AliMUONConstants::DefaultA0()*AliMUONConstants::DefaultCapa();
+    fLogger->Log("CHECK ME ! WAS NOT SUPPOSED TO BE HERE !!! ARE YOU RECONSTRUCTING OLD SIMULATIONS ? ");
+    AliError("CHECK ME ! WAS NOT SUPPOSED TO BE HERE !!! ARE YOU RECONSTRUCTING OLD SIMULATIONS ? ");
   }
   
+  // We set the charge to 0, as the only relevant piece of information
+  // after Digitization is the ADC value.  
+  digit.SetCharge(0);
+    
   Int_t detElemId = digit.DetElemId();
-  
   Int_t manuId = digit.ManuId();
-  Int_t manuChannel = digit.ManuChannel();
   
   AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
   if (!pedestal)
@@ -182,124 +192,92 @@ AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t add
     digit.SetADC(0);
     return;    
   }
-  Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
-  Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
   
-  AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
-  if (!gain)
+  Int_t manuChannel = digit.ManuChannel();
+  
+  if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() ||
+      pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() )
   {
-    fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
-                      __FILE__,__LINE__,
-                      detElemId,manuId));
+    // protection against invalid pedestal value
     digit.SetADC(0);
-    return;        
-  }    
+    return;
+  }
 
-  Float_t a0 = gain->ValueAsFloat(manuChannel,0);
-  Float_t a1 = gain->ValueAsFloat(manuChannel,1);
+  TString calibrationMode(fRecoParam->GetCalibrationMode());
+  calibrationMode.ToUpper();
 
-  Int_t thres = gain->ValueAsInt(manuChannel,2);
-  Float_t chargeThres = a0*thres;
-  
-  Float_t padc(0); // (adc - ped) value
-  
-  if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 ) 
+  AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
+  if (!gain)
   {
-    // linear part only
     
-    if ( TMath::Abs(a0) > 1E-12 ) 
+    if (!calibrationMode.Contains("NOGAIN") )
     {
-      padc = charge/a0;    
+      fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
+                        __FILE__,__LINE__,
+                        detElemId,manuId));
+      digit.SetADC(0);
+      return;        
     }
-  }
-  else // charge > chargeThres && a1 not zero
-  {
-    // parabolic part
-    padc = TMath::Sqrt((chargeThres-charge)/a1) + thres;
-  }
+  }    
 
-  Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
-  Int_t adc(0);
-  
-  if ( padc > 0 ) 
-  {
-    adc = TMath::Nint(padc + pedestalMean + adcNoise);
-  }
-  
-  // be sure we stick to 12 bits.
-  if ( adc > kMaxADC )
-  {
-    adc = kMaxADC;
-  }
+  Int_t adc = DecalibrateTrackerDigit(*pedestal,gain,manuChannel,charge,addNoise,
+                                      digit.IsNoiseOnly(),
+                                      calibrationMode);
   
-  AliDebug(3,Form("DE %4d Manu %4d Ch %2d Charge %e A0 %e A1 %e Thres %d padc %e ADC %4d",
-                  detElemId,manuId,manuChannel,charge,
-                  gain->ValueAsFloat(manuChannel,0),
-                  gain->ValueAsFloat(manuChannel,1),
-                  gain->ValueAsInt(manuChannel,2),
-                  padc,
-                  adc));
-                  
   digit.SetADC(adc);
 }
 
+
 //_____________________________________________________________________________
 void 
-AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
-                                                AliMUONVDigit& digit)
+AliMUONDigitizerV3::ApplyResponseToTriggerDigit(AliMUONVDigit& digit)
 {
-  /// \todo add comment
-
-  if ( !fTriggerEfficiency ) return;
-
-  if (digit.IsEfficiencyApplied()) return;
-
-  AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
-
-  if (!correspondingDigit) return; //reject bad correspondences
-
+  /// For trigger digits, starting from an ideal digit, we :
+  ///
+  /// - apply efficiency (on demand)
+  /// - apply trigger masks
+    
   Int_t detElemId = digit.DetElemId();
-
-  AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
-  const AliMpVSegmentation* segment[2] = 
-  {
-    segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())), 
-    segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
-  };
-
-  AliMpPad pad[2] = 
-  {
-    segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE), 
-    segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
-  };
-
-  Int_t p0(1);
-  if (digit.Cathode()==0) p0=0;
-
-  AliMpIntPair location = pad[p0].GetLocation(0);
-  Int_t nboard = location.GetFirst();
-
-  Bool_t isTrig[2];
-
-  fTriggerEfficiency->IsTriggered(detElemId, nboard, 
-                                  isTrig[0], isTrig[1]);
-  digit.EfficiencyApplied(kTRUE);
-  correspondingDigit->EfficiencyApplied(kTRUE);
-
-  if (!isTrig[digit.Cathode()])
-  {
-         digit.SetCharge(0);
+  Int_t localCircuit = digit.ManuId();
+  Int_t strip = digit.ManuChannel();
+  Int_t cathode = digit.Cathode();
+  Int_t trigCh = detElemId/100 - 11;
+  
+  // Masked channels
+  Bool_t isMasked = fTriggerUtilities->IsMasked(digit);
+  AliDebug(1,Form("detElemId %i  cath %i  board %i  strip %i  is masked %i\n", detElemId, cathode, localCircuit, strip, isMasked));
+  if ( isMasked ) {
+    digit.SetCharge(0);
+    digit.SetADC(0);
+    //AliDebug(1,Form("ch %i  cath %i  board %i  strip %i  masked\n", trigCh, cathode, localCircuit, strip));
+    return;
   }
   
-  if ( &digit != correspondingDigit )
-  {
-         if (!isTrig[correspondingDigit->Cathode()])
-    {
-      correspondingDigit->SetCharge(0);
-         }
-  }
+  
+  Int_t arrayIndex = GetArrayIndex(cathode, trigCh, localCircuit);
+  
+  // Trigger chamber efficiency
+  if ( fTriggerEfficiency ) {
+    if ( fEfficiencyResponse[arrayIndex] < 0 ) {
+      Bool_t isTrig[2] = {kTRUE, kTRUE};
+      fTriggerEfficiency->IsTriggered(detElemId, localCircuit, isTrig[0], isTrig[1]);
+      Int_t arrayIndexBend = GetArrayIndex(0, trigCh, localCircuit);
+      Int_t arrayIndexNonBend = GetArrayIndex(1, trigCh, localCircuit);
+      fEfficiencyResponse[arrayIndexBend] = isTrig[0];
+      fEfficiencyResponse[arrayIndexNonBend] = isTrig[1];
+    }
+    AliDebug(1,Form("detElemId %i  cath %i  board %i  strip %i  efficiency %i\n", detElemId, cathode, localCircuit, strip, fEfficiencyResponse[arrayIndex]));
+    if ( fEfficiencyResponse[arrayIndex] == 0 ) {
+      digit.SetCharge(0);
+      digit.SetADC(0);
+      //AliDebug(1,Form("ch %i  cath %i  board %i  strip %i  NOT efficient\n", trigCh, cathode, localCircuit, strip));
+      return;
+    }
+  }  
 }
 
+
+
 //_____________________________________________________________________________
 void
 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
@@ -315,18 +293,22 @@ AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
   TIter next(store.CreateIterator());
   AliMUONVDigit* digit;
   
+  if ( fTriggerEfficiency ) fEfficiencyResponse.Reset(-1);
+  
   while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
   {
     AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
     
     if ( stationType != AliMp::kStationTrigger )
     {
-      ApplyResponseToTrackerDigit(*digit,kAddNoise);
+      Bool_t addNoise = kAddNoise;
+      if (digit->IsConverted()) addNoise = kFALSE; // No need to add extra noise to a converted real digit
+      ApplyResponseToTrackerDigit(*digit,addNoise);
     }
-    else
-    {
-      ApplyResponseToTriggerDigit(store,*digit);
+    else {
+      ApplyResponseToTriggerDigit(*digit);
     }
+
     if ( digit->ADC() > 0  || digit->Charge() > 0 )
     {
       filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
@@ -334,9 +316,199 @@ AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
   }
 }    
 
+//_____________________________________________________________________________
+Int_t 
+AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
+                                            const AliMUONVCalibParam* gains,
+                                            Int_t channel,
+                                            Float_t charge,
+                                            Bool_t addNoise,
+                                            Bool_t noiseOnly,
+                                            const TString& calibrationMode)
+{
+  /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
+  /// pedestal and gain parameters.
+  /// Must insure before calling that channel is valid (i.e. between 0 and
+  /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
+  /// otherwise results are not predictible...)
+  ///
+  /// This method is completely tied to what happens in its sister method :
+  /// AliMUONDigitCalibrator::CalibrateDigit, which is doing the reverse work...
+  ///
+  
+  static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
+  
+  Bool_t nogain = calibrationMode.Contains("NOGAIN");
+  
+  Float_t a1(0.0);
+  Int_t thres(4095);
+  Int_t qual(0xF);
+  Float_t capa(AliMUONConstants::DefaultCapa()); // capa = 0.2 and a0 = 1.25
+  Float_t a0(AliMUONConstants::DefaultA0());  // is equivalent to gain = 4 mV/fC
+  Float_t adc2mv(AliMUONConstants::DefaultADC2MV()); // 1 ADC channel = 0.61 mV
+               
+  if ( ! nogain )
+  {
+    if  (!gains)
+    {
+      AliFatalClass("Cannot make gain decalibration without gain values !");
+    }
+    a0 = gains->ValueAsFloat(channel,0);
+    a1 = gains->ValueAsFloat(channel,1);
+    thres = gains->ValueAsInt(channel,2);
+    qual = gains->ValueAsInt(channel,3);
+  }
+  
+  Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
+  Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
+  
+  AliDebugClass(2,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f",
+                      pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma));
+  
+  if ( qual <= 0 ) return 0;
+  
+  Float_t chargeThres = a0*thres;
+  
+  Float_t padc(0); // (adc - ped) value
+  
+  if ( nogain || charge <= chargeThres || TMath::Abs(a1) < 1E-12 ) 
+  {
+    // linear part only
+    
+    if ( TMath::Abs(a0) > 1E-12 ) 
+    {
+      padc = charge/a0;    
+    }
+  }
+  else 
+  {
+    // FIXME: when we'll use capacitances and real gains, revise this part
+    // to take capa properly into account...
+    
+    AliWarningClass("YOU PROBABLY NEED TO REVISE THIS PART OF CODE !!!");
+    
+    // linear + parabolic part
+    Double_t qt = chargeThres - charge;
+    Double_t delta = a0*a0-4*a1*qt;
+    if ( delta < 0 ) 
+    {
+      AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
+                    " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
+                    delta,pedestals.ID0(),pedestals.ID1(),
+                    channel, charge, a0, a1, thres, pedestalMean, 
+                    pedestalSigma));      
+    }      
+    else
+    {
+      delta = TMath::Sqrt(delta);
+      
+      padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
+      
+      padc /= 2*a1;
+    
+      if ( padc < 0 )
+      {
+        if ( TMath::Abs(padc) > 1E-3) 
+        {
+          // this is more than a precision problem : let's signal it !
+          AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
+                             " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
+                             padc,pedestals.ID0(),pedestals.ID1(),
+                             channel, charge, a0, a1, thres, pedestalMean, 
+                             pedestalSigma,delta));
+        }
+
+        // ok. consider we're just at thres, let it be zero.
+        padc = 0;
+      }
+
+      padc += thres;
+
+    }
+  }
+  
+  padc /= capa*adc2mv;
+  
+  Int_t adc(0);
+  
+  Float_t adcNoise = 0.0;
+    
+  if ( addNoise ) 
+  {
+    if ( noiseOnly )
+    {      
+      adcNoise = NoiseFunction()->GetRandom()*pedestalSigma;
+    }
+    else
+    {
+      adcNoise = gRandom->Gaus(0.0,pedestalSigma);
+    }
+  }
+    
+  adc = TMath::Nint(padc + pedestalMean + adcNoise + 0.5);
+  
+  if ( adc < TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5) ) 
+  {
+    // this is an error only in specific cases
+    if ( !addNoise || (addNoise && noiseOnly) ) 
+    {
+      AliDebugClass(1,Form(" DE %04d Manu %04d Channel %02d "
+                                                                                                        " a0 %7.2f a1 %7.2f thres %04d ped %7.2f pedsig %7.2f adcNoise %7.2f "
+                                                                                                        " charge=%7.2f padc=%7.2f adc=%04d ZS=%04d fgNSigmas=%e addNoise %d noiseOnly %d ",
+                                                                                                        pedestals.ID0(),pedestals.ID1(),channel, 
+                                                                                                        a0, a1, thres, pedestalMean, pedestalSigma, adcNoise,
+                                                                                                        charge, padc, adc, 
+                                                                                                        TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5),
+                                                                                                        fgNSigmas,addNoise,noiseOnly));
+    }
+    
+    adc = 0;
+  }
+  
+  // be sure we stick to 12 bits.
+  if ( adc > kMaxADC )
+  {
+    adc = kMaxADC;
+  }
+  
+  return adc;
+}
+
+//_____________________________________________________________________________
+void
+AliMUONDigitizerV3::CreateInputDigitStores()
+{
+  /// Create input digit stores
+  /// 
+  
+  if (fInputDigitStores)
+  {
+    AliFatal("Should be called only once !");
+  }
+  
+  fInputDigitStores = new TObjArray;
+  
+  fInputDigitStores->SetOwner(kTRUE);
+  
+  for ( Int_t iFile = 0; iFile < fDigInput->GetNinputs(); ++iFile )
+  {    
+    AliLoader* inputLoader = GetLoader(fDigInput->GetInputFolderName(iFile));
+    
+    inputLoader->LoadSDigits("READ");
+    
+    TTree* iTreeS = inputLoader->TreeS();
+    if (!iTreeS)
+    {
+      AliFatal(Form("Could not get access to input file #%d",iFile));
+    }
+    
+    fInputDigitStores->AddAt(AliMUONVDigitStore::Create(*iTreeS),iFile);
+  }
+}
+
 //_____________________________________________________________________________
 void
-AliMUONDigitizerV3::Exec(Option_t*)
+AliMUONDigitizerV3::Digitize(Option_t*)
 {
   /// Main method.
   /// We first loop over input files, and merge the sdigits we found there.
@@ -344,9 +516,9 @@ AliMUONDigitizerV3::Exec(Option_t*)
   /// Then we generate noise-only digits (for tracker only)
   /// And we finally generate the trigger outputs.
     
-  AliDebug(1, "Running digitizer.");
+  AliCodeTimerAuto("",0)
   
-  if ( fManager->GetNinputs() == 0 )
+  if ( fDigInput->GetNinputs() == 0 )
   {
     AliWarning("No input set. Nothing to do.");
     return;
@@ -358,11 +530,9 @@ AliMUONDigitizerV3::Exec(Option_t*)
     return;
   }
   
-  fExecTimer.Start(kFALSE);
-
-  Int_t nInputFiles = fManager->GetNinputs();
+  Int_t nInputFiles = fDigInput->GetNinputs();
   
-  AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
+  AliLoader* outputLoader = GetLoader(fDigInput->GetOutputFolderName());
   
   outputLoader->MakeDigitsContainer();
   
@@ -377,8 +547,8 @@ AliMUONDigitizerV3::Exec(Option_t*)
   // files.
   
   for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
-  {    
-    AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
+  {  
+    AliLoader* inputLoader = GetLoader(fDigInput->GetInputFolderName(iFile));
 
     inputLoader->LoadSDigits("READ");
 
@@ -387,18 +557,25 @@ AliMUONDigitizerV3::Exec(Option_t*)
     {
       AliFatal(Form("Could not get access to input file #%d",iFile));
     }
+
+    if (!fInputDigitStores)
+    {
+      CreateInputDigitStores();      
+    }
     
-    AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
-    inputStore->Connect(*iTreeS);
+    AliMUONVDigitStore* dstore = static_cast<AliMUONVDigitStore*>(fInputDigitStores->At(iFile));
     
-    iTreeS->GetEvent(0);
+    dstore->Connect(*iTreeS);
     
-    MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
+    iTreeS->GetEvent(0);
+
+    MergeWithSDigits(fDigitStore,*dstore,fDigInput->GetMask(iFile));
 
     inputLoader->UnloadSDigits();
     
-    delete inputStore;
+    dstore->Clear();
   }
+
   
   // At this point, we do have digit arrays (one per chamber) which contains 
   // the merging of all the sdigits of the input file(s).
@@ -415,9 +592,8 @@ AliMUONDigitizerV3::Exec(Option_t*)
     // Generate noise-only digits for trigger.
     GenerateNoisyDigitsForTrigger(*fDigitStore);
   }
-
   ApplyResponse(*fDigitStore,*fOutputDigitStore);
-  
+
   if ( fGenerateNoisyDigits )
   {
     // Generate noise-only digits for tracker.
@@ -451,30 +627,6 @@ AliMUONDigitizerV3::Exec(Option_t*)
   fTriggerStore->Clear();
   fDigitStore->Clear();
   fOutputDigitStore->Clear();
-  fExecTimer.Stop();
-}
-
-//_____________________________________________________________________________
-AliMUONVDigit* 
-AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
-                                           AliMUONVDigit& digit) const
-{                                                
-  /// Find, if it exists, the digit corresponding to digit.Hit(), in the 
-  /// other cathode
-
-  TIter next(digitStore.CreateIterator());
-  AliMUONVDigit* d;
-  
-  while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
-  {
-    if ( d->DetElemId() == digit.DetElemId() &&
-         d->Hit() == digit.Hit() &&
-         d->Cathode() != digit.Cathode() )
-    {
-      return d;
-    }      
-  }    
-  return 0x0;
 }
 
 
@@ -486,15 +638,7 @@ AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
   /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
   /// that are "only noise".
   
-  if ( !fNoiseFunction )
-  {
-    fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
-                             fgkNSigmas,fgkNSigmas*10);
-    
-    fNoiseFunction->SetParameters(1,0,1);
-  }
-  
-  fGenerateNoisyDigitsTimer.Start(kFALSE);
+  AliCodeTimerAuto("",0)
   
   for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
   {
@@ -511,8 +655,6 @@ AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
       it.Next();
     }
   }
-  
-  fGenerateNoisyDigitsTimer.Stop();
 }
  
 //_____________________________________________________________________________
@@ -530,7 +672,7 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitSt
   Int_t maxIx = seg->MaxPadIndexX();
   Int_t maxIy = seg->MaxPadIndexY();
   
-  static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
+  static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgNSigmas/TMath::Sqrt(2.0)) / 2. ;
   
   Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
   if ( !nofNoisyPads ) return;
@@ -550,11 +692,11 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitSt
     do {
       ix = gRandom->Integer(maxIx+1);
       iy = gRandom->Integer(maxIy+1);
-      pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
+      pad = seg->PadByIndices(ix,iy,kFALSE);
     } while ( !pad.IsValid() );
 
-    Int_t manuId = pad.GetLocation().GetFirst();
-    Int_t manuChannel = pad.GetLocation().GetSecond();    
+    Int_t manuId = pad.GetManuId();
+    Int_t manuChannel = pad.GetManuChannel();    
 
     AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
     
@@ -568,14 +710,9 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitSt
     
     d->SetPadXY(ix,iy);
     
-    Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
-    Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
-    
-    Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
-
-    d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
+    d->SetCharge(0.0); // charge is zero, the ApplyResponseToTrackerDigit will add the noise
     d->NoiseOnly(kTRUE);
-    ApplyResponseToTrackerDigit(*d,kFALSE);
+    ApplyResponseToTrackerDigit(*d,kTRUE);
     if ( d->ADC() > 0 )
     {
       Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
@@ -591,11 +728,6 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitSt
         fLogger->Log("Added noiseOnly digit");
       }
     }
-    else
-    {
-      AliError("Pure noise below threshold. This should not happen. Not adding "
-               "this digit.");
-    }
     delete d;
   }
 }
@@ -649,8 +781,8 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore
                const AliMpVSegmentation* seg
                    = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
                if (!seg) continue;
-               Float_t deltax = seg->Dimensions().X();
-               Float_t deltay = seg->Dimensions().Y();
+               Float_t deltax = seg->GetDimensionX();
+               Float_t deltay = seg->GetDimensionY();
                GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
                GetTransformer()->Local2Global(currDetElemId,  deltax,  deltay, 0, xg02, yg02, zg);
                Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
@@ -665,7 +797,7 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore
                if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
                    detElemId = currDetElemId;
                    GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
-                   pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
+                   pad[0] = seg->PadByPosition(x,y,kFALSE);
                    if(!pad[0].IsValid()) continue;
                    isOk = kTRUE;
                    break;
@@ -675,14 +807,14 @@ AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore
 
        const AliMpVSegmentation* seg1
            = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
-       pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
+       pad[1] = seg1->PadByPosition(x,y,kFALSE);
 
        for ( Int_t cathode = 0; cathode < 2; ++cathode ){
-         Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
-         Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();    
+         Int_t manuId = pad[cathode].GetLocalBoardId(0);
+         Int_t manuChannel = pad[cathode].GetLocalBoardChannel(0);    
          d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
-         ix = pad[cathode].GetIndices().GetFirst();
-         iy = pad[cathode].GetIndices().GetSecond();
+         ix = pad[cathode].GetIx();
+         iy = pad[cathode].GetIy();
          d[cathode]->SetPadXY(ix,iy);
          //d[cathode].SetSignal(1);
          //d[cathode].SetPhysicsSignal(0);
@@ -725,7 +857,7 @@ AliMUONDigitizerV3::GetLoader(const TString& folderName)
 Bool_t
 AliMUONDigitizerV3::Init()
 {
-  /// Initialization of the TTask :
+  /// Initialization of the digitization :
   /// a) create the calibrationData, according to run number
   /// b) create the trigger processing task
 
@@ -737,15 +869,20 @@ AliMUONDigitizerV3::Init()
     return kFALSE;
   }
   
-  if (!fManager)
+  if (!fDigInput)
   {
-    AliError("fManager is null !");
+    AliError("fDigInput is null !");
     return kFALSE;
   }
   
-  Int_t runnumber = AliCDBManager::Instance()->GetRun();
+  // Load mapping
+  if ( ! AliMpCDB::LoadDDLStore() ) {
+    AliFatal("Could not access mapping from OCDB !");
+  }
   
-  fCalibrationData = new AliMUONCalibrationData(runnumber);
+  if (!fCalibrationData)
+      AliFatal("Calibration data object not defined");
+
   if ( !fCalibrationData->Pedestals() )
   {
     AliFatal("Could not access pedestals from OCDB !");
@@ -754,29 +891,38 @@ AliMUONDigitizerV3::Init()
   {
     AliFatal("Could not access gains from OCDB !");
   }
-  fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
   
-  if ( muon()->GetTriggerEffCells() )
-  {
-    fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
-    if ( fTriggerEfficiency )
-    {
-      AliDebug(1, "Will apply trigger efficiency");
-    }
-    else
-    {
-      AliFatal("I was requested to apply trigger efficiency, but I could "
-               "not get it !");
-    }
-  }
+  
+   AliInfo("Using trigger configuration from CDB");
+  
+  fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
   
   AliDebug(1, Form("Will %s generate noise-only digits for tracker",
                      (fGenerateNoisyDigits ? "":"NOT")));
+  
+  fTriggerUtilities = new AliMUONTriggerUtilities(fCalibrationData);
+  
+  if ( muon()->GetTriggerEffCells() ) {
+    // Apply trigger efficiency
+    AliDebug(1, "Will apply trigger efficiency");
+    fTriggerEfficiency = new AliMUONTriggerChamberEfficiency(fCalibrationData->TriggerEfficiency());
+  }
 
   fIsInitialized = kTRUE;
   return kTRUE;
 }
 
+
+//_____________________________________________________________________________
+Int_t AliMUONDigitizerV3::GetArrayIndex(Int_t cathode, Int_t trigCh, Int_t localCircuit)
+{
+  /// Get index of array with trigger status map or efficiency
+  return
+    AliMUONConstants::NTriggerCircuit() * AliMUONConstants::NTriggerCh() * cathode +
+    AliMUONConstants::NTriggerCircuit() * trigCh + localCircuit-1;
+}
+
+
 //_____________________________________________________________________________
 void 
 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
@@ -804,3 +950,30 @@ AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
     }
   }
 }
+
+//_____________________________________________________________________________
+TF1*
+AliMUONDigitizerV3::NoiseFunction()
+{
+  /// Return noise function
+  static TF1* f = 0x0;
+  if (!f)
+  {
+    f = new TF1("AliMUONDigitizerV3::NoiseFunction","gaus",fgNSigmas,fgNSigmas*10);
+    f->SetParameters(1,0,1);
+  }
+  return f;
+}
+
+//_____________________________________________________________________________
+void AliMUONDigitizerV3::SetCalibrationData(AliMUONCalibrationData* calibrationData, 
+                                            AliMUONRecoParam* recoParam) 
+{
+  fCalibrationData = calibrationData;
+  fRecoParam = recoParam;
+  if (!fRecoParam)
+  {
+    AliError("Cannot work (e.g. decalibrate) without recoparams !");
+  }
+}
+