]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONDigitizerV3.cxx
Fixing wrong usage of bits
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitizerV3.cxx
index 7aed1f714f64b14bf528e6c5dcce5f27ed1fd958..bd7d91be4ccb0bd8d124781484ca2253e2b301fd 100644 (file)
@@ -35,7 +35,6 @@
 #include "AliMpConstants.h"
 #include "AliMpDEIterator.h"
 #include "AliMpDEManager.h"
-#include "AliMpIntPair.h"
 #include "AliMpPad.h"
 #include "AliMpStationType.h"
 #include "AliMpVSegmentation.h"
@@ -45,6 +44,7 @@
 #include "AliLog.h"
 #include "AliRun.h"
 #include "AliRunDigitizer.h"
+#include "AliLoader.h"
 #include "AliRunLoader.h"
 
 #include <Riostream.h>
@@ -54,6 +54,7 @@
 #include <TRandom.h>
 #include <TString.h>
 #include <TSystem.h>
+#include <TTree.h>
 
 //-----------------------------------------------------------------------------
 /// \class AliMUONDigitizerV3
@@ -86,7 +87,7 @@ namespace
   }
 }
 
-const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
+Double_t AliMUONDigitizerV3::fgNSigmas = 4.0;
 
 /// \cond CLASSIMP
 ClassImp(AliMUONDigitizerV3)
@@ -99,17 +100,18 @@ AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
 fIsInitialized(kFALSE),
 fCalibrationData(0x0),
 fTriggerProcessor(0x0),
-fNoiseFunction(0x0),
 fNoiseFunctionTrig(0x0),
-  fGenerateNoisyDigits(generateNoisyDigits),
-  fLogger(new AliMUONLogger(1000)),
+fGenerateNoisyDigits(generateNoisyDigits),
+fLogger(new AliMUONLogger(1000)),
 fTriggerStore(new AliMUONTriggerStoreV1),
 fDigitStore(0x0),
-fOutputDigitStore(0x0)
+fOutputDigitStore(0x0),
+fInputDigitStores(0x0)
 {
   /// Ctor.
 
   AliDebug(1,Form("AliRunDigitizer=%p",fManager));
+
 }
 
 //_____________________________________________________________________________
@@ -119,14 +121,14 @@ AliMUONDigitizerV3::~AliMUONDigitizerV3()
 
   AliDebug(1,"dtor");
 
-  delete fCalibrationData;
// delete fCalibrationData;
   delete fTriggerProcessor;
-  delete fNoiseFunction;
   delete fNoiseFunctionTrig;
   delete fTriggerStore;
   delete fDigitStore;
   delete fOutputDigitStore;
-   
+  delete fInputDigitStores;
+  
   AliInfo("Summary of messages");
   fLogger->Print();
   
@@ -144,7 +146,7 @@ AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t add
   /// - 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
 
-  Float_t charge = digit.Charge();
+  Float_t charge = digit.IsChargeInFC() ? digit.Charge()*AliMUONConstants::FC2ADC() : digit.Charge();
   
   // We set the charge to 0, as the only relevant piece of information
   // after Digitization is the ADC value.  
@@ -163,6 +165,16 @@ AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t add
     return;    
   }
   
+  Int_t manuChannel = digit.ManuChannel();
+  
+  if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() ||
+      pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() )
+  {
+    // protection against invalid pedestal value
+    digit.SetADC(0);
+    return;
+  }
+      
   AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
   if (!gain)
   {
@@ -173,11 +185,8 @@ AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t add
     return;        
   }    
 
-  Int_t manuChannel = digit.ManuChannel();
-  
-
-  
-  Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise);
+  Int_t adc = DecalibrateTrackerDigit(*pedestal,*gain,manuChannel,charge,addNoise,
+                                      digit.IsNoiseOnly());
   
   digit.SetADC(adc);
 }
@@ -203,7 +212,9 @@ AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
     
     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);
     }
 
     if ( digit->ADC() > 0  || digit->Charge() > 0 )
@@ -219,7 +230,8 @@ AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
                                             const AliMUONVCalibParam& gains,
                                             Int_t channel,
                                             Float_t charge,
-                                            Bool_t addNoise)
+                                            Bool_t addNoise,
+                                            Bool_t noiseOnly)
 {
   /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
   /// pedestal and gain parameters.
@@ -232,10 +244,14 @@ AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
   Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
   Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
   
+  AliDebugClass(1,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f",
+                      pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma));
+  
   Float_t a0 = gains.ValueAsFloat(channel,0);
   Float_t a1 = gains.ValueAsFloat(channel,1);
   Int_t thres = gains.ValueAsInt(channel,2);
   Int_t qual = gains.ValueAsInt(channel,3);
+
   if ( qual <= 0 ) return 0;
   
   Float_t chargeThres = a0*thres;
@@ -295,13 +311,38 @@ AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
   
   Int_t adc(0);
   
-  if ( padc > 0 ) 
+  Float_t adcNoise = 0.0;
+    
+  if ( addNoise ) 
   {
-    Float_t adcNoise = 0.0;
+    if ( noiseOnly )
+    {      
+      adcNoise = NoiseFunction()->GetRandom()*pedestalSigma;
+    }
+    else
+    {
+      adcNoise = gRandom->Gaus(0.0,pedestalSigma);
+    }
+  }
     
-    if ( addNoise ) 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) ) 
+    {
+      AliErrorClass(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 = TMath::Nint(padc + pedestalMean + adcNoise);
+    adc = 0;
   }
   
   // be sure we stick to 12 bits.
@@ -313,6 +354,38 @@ AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
   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 < fManager->GetNinputs(); ++iFile )
+  {    
+    AliLoader* inputLoader = GetLoader(fManager->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*)
@@ -323,9 +396,7 @@ AliMUONDigitizerV3::Exec(Option_t*)
   /// Then we generate noise-only digits (for tracker only)
   /// And we finally generate the trigger outputs.
     
-  AliCodeTimerAuto("")
-  
-  AliDebug(1, "Running digitizer.");
+  AliCodeTimerAuto("",0)
   
   if ( fManager->GetNinputs() == 0 )
   {
@@ -356,7 +427,7 @@ AliMUONDigitizerV3::Exec(Option_t*)
   // files.
   
   for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
-  {    
+  {  
     AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
 
     inputLoader->LoadSDigits("READ");
@@ -366,18 +437,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,fManager->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).
@@ -394,9 +472,8 @@ AliMUONDigitizerV3::Exec(Option_t*)
     // Generate noise-only digits for trigger.
     GenerateNoisyDigitsForTrigger(*fDigitStore);
   }
-
   ApplyResponse(*fDigitStore,*fOutputDigitStore);
-  
+
   if ( fGenerateNoisyDigits )
   {
     // Generate noise-only digits for tracker.
@@ -441,15 +518,7 @@ AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
   /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
   /// that are "only noise".
   
-  AliCodeTimerAuto("")
-  
-  if ( !fNoiseFunction )
-  {
-    fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
-                             fgkNSigmas,fgkNSigmas*10);
-    
-    fNoiseFunction->SetParameters(1,0,1);
-  }
+  AliCodeTimerAuto("",0)
   
   for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
   {
@@ -483,7 +552,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;
@@ -503,11 +572,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);
     
@@ -521,14 +590,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);
@@ -544,11 +608,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;
   }
 }
@@ -602,8 +661,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;
@@ -618,7 +677,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;
@@ -628,14 +687,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);
@@ -696,14 +755,14 @@ AliMUONDigitizerV3::Init()
     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 !");
@@ -712,6 +771,10 @@ AliMUONDigitizerV3::Init()
   {
     AliFatal("Could not access gains from OCDB !");
   }
+  
+  
+   AliInfo("Using trigger configuration from CDB");
+  
   fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
   
   AliDebug(1, Form("Will %s generate noise-only digits for tracker",
@@ -748,3 +811,18 @@ 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;
+}
+