]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONPadStatusMapMaker.cxx
Remove the dollar-id-dollar in the responsible name which interfere with CDB_MD metad...
[u/mrichter/AliRoot.git] / MUON / AliMUONPadStatusMapMaker.cxx
index 7fc6f875a5f12fb6a0fcd8fdf34e54e144267858..73469f7f65fe195fd8473a99b3795b71113f59e8 100644 (file)
 **************************************************************************/
 
 // $Id$
+
+//-----------------------------------------------------------------------------
 /// \class AliMUONPadStatusMapMaker
 /// 
-/// Convert a pad status container into a pad status *map* container
+/// Convert a pad statuses into pad status maps.
 /// 
 /// A pad status is one 32-bits word describing whether this pad pedestal, gains
 /// hv is correct or not.
 /// If a pad is at a physical boundary, is will for sure have some bits at 1
 /// (i.e. a non-existing neighbour is considered = bad).
 ///
+///
+/// add something about the reject list/probabilities here... (LA)
+///
 /// \author Laurent Aphecetche
+//-----------------------------------------------------------------------------
 
 #include "AliMUONPadStatusMapMaker.h"
 
+#include "AliCodeTimer.h"
 #include "AliLog.h"
+#include "AliMpDDLStore.h"
+#include "AliMpDetElement.h"
+#include "AliMpManuIterator.h"
 #include "AliMUON2DMap.h"
+#include "AliMUONCalibParamNF.h"
 #include "AliMUONCalibParamNI.h"
 #include "AliMUONCalibrationData.h"
-#include "AliMUONVStore.h"
+#include "AliMUONPadStatusMaker.h"
+#include "AliMUONRejectList.h"
 #include "AliMUONVCalibParam.h"
+#include "AliMUONVStore.h"
 #include "AliMpConstants.h"
-#include "AliMpIntPair.h"
-#include "AliMpManuList.h"
 #include <Riostream.h>
 #include <TList.h>
-#include <TStopwatch.h>
+#include "TRandom.h"
+#include <cassert>
 
 /// \cond CLASSIMP
 ClassImp(AliMUONPadStatusMapMaker)
@@ -69,177 +81,263 @@ ClassImp(AliMUONPadStatusMapMaker)
 Int_t AliMUONPadStatusMapMaker::fgkSelfDead = 1;
 
 //_____________________________________________________________________________
-AliMUONPadStatusMapMaker::AliMUONPadStatusMapMaker(const AliMUONCalibrationData& calibData) 
+AliMUONPadStatusMapMaker::AliMUONPadStatusMapMaker(const AliMUONPadStatusMaker& padStatusMaker,
+                                                   Int_t mask,
+                                                   Bool_t deferredInitialization) 
 : TObject(),
-fStatus(0x0),
-fMask(0),
-fCalibrationData(calibData)
+fkStatusMaker(padStatusMaker),
+fMask(mask),
+fStatusMap(new AliMUON2DMap(true)),
+fRejectProbabilities(new AliMUON2DMap(true)),
+fRejectList(0x0),
+fComputeOnDemand(deferredInitialization)
 {
   /// ctor
-}
-
-//_____________________________________________________________________________
-AliMUONPadStatusMapMaker::~AliMUONPadStatusMapMaker()
-{
-  /// dtor
-}
-
-//_____________________________________________________________________________
-Int_t
-AliMUONPadStatusMapMaker::ComputeStatusMap(const AliMUONVCalibParam& neighbours,
-                                          Int_t manuChannel,
-                                          Int_t detElemId) const
-{
-  /// Given a list of neighbours of one pad (which includes the pad itself)
-  /// compute the status map (aka deadmap) for that pad.
+  if (!deferredInitialization)
+  {
+    AliCodeTimerAuto("Computing complete status map at once",0);
+    AliMUONVStore* neighboursStore = padStatusMaker.NeighboursStore();
+    AliMUONVCalibParam* param;
+    TIter next(neighboursStore->CreateIterator());
+    while ( ( param = static_cast<AliMUONVCalibParam*>(next()) ) )
+    {
+      Int_t detElemId = param->ID0();
+      Int_t manuId = param->ID1();
+      ComputeStatusMap(detElemId,manuId);
+    }
+  }
   
-  Int_t statusMap(0);
-
-  //Compute the statusmap related to the status of neighbouring
-  //pads. An invalid pad means "outside of edges".
-
-  Int_t n = neighbours.Dimension();
-  for ( Int_t i = 0; i < n; ++i )
+  /// Whatever the deferred flag is, we *have* to compute the reject 
+  /// probabilities here and now, for *all* channels.
+  
+  AliMUONRejectList* rl = padStatusMaker.CalibrationData().RejectList();
+  
+  if (rl)
   {
-    Int_t x = neighbours.ValueAsInt(manuChannel,i);
-    Int_t m,c;
-    neighbours.UnpackValue(x,m,c);
-    if ( c < 0 ) continue;
-    Int_t status = 0;
-    if ( !m )
+    AliMpManuIterator it;
+    Int_t detElemId;
+    Int_t manuId;
+    
+    while ( it.Next(detElemId,manuId) )
     {
-      status = -1;
+      AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
+      Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
+      
+      AliMUONVCalibParam* param = new AliMUONCalibParamNF(1,AliMpConstants::ManuNofChannels(),detElemId,manuId,0);
+      
+      Int_t n(0);
+      
+      for ( Int_t i = 0; i < AliMpConstants::ManuNofChannels(); ++i ) 
+      {
+        Float_t proba(0.0);
+        
+        if ( de->IsConnectedChannel(manuId,i) )
+        {
+          proba = TMath::Max(rl->DetectionElementProbability(detElemId),rl->BusPatchProbability(busPatchId));
+          
+          proba = TMath::Max(proba,rl->ManuProbability(detElemId,manuId));
+          
+          proba = TMath::Max(proba,rl->ChannelProbability(detElemId,manuId,i));
+          
+          if ( proba > 0 ) 
+          {
+            ++n;
+            param->SetValueAsFloat(i,0,proba);
+          }
+        }
+      }
+      
+      if ( n > 0 ) 
+      {
+        fRejectProbabilities->Add(param);
+      }
+      else
+      {
+        // no need to add empty stuff...
+        delete param;
+      }
     }
-    else
+  
+    if ( rl->IsBinary())
     {
-      status = GetPadStatus(detElemId,m,c);
+      fRejectList = fRejectProbabilities;
+      fRejectProbabilities = 0x0;
+      AliDebug(1,"RejectList = RejectProbabilities");
+      StdoutToAliDebug(1,fRejectList->Print("","MEAN"));
     }
-    if ( ( fMask==0 && status !=0 ) || ( (status & fMask) != 0 ) )
+    else
     {
-      statusMap |= (1<<i);
+      AliWarning("Will run with non trivial survival probabilities for channels, manus, etc... Better check this is a simulation and not real data !");
+      fRejectList = new AliMUON2DMap(true);
     }
   }
-  return statusMap;
+  else
+  {
+    fRejectList = fRejectProbabilities;
+    fRejectProbabilities = 0x0;
+    AliInfo("No RejectList found, so no RejectList will be used.");
+  }
 }
 
 //_____________________________________________________________________________
-Int_t
-AliMUONPadStatusMapMaker::GetPadStatus(Int_t detElemId, 
-                                       Int_t manuId, Int_t manuChannel) const
-                                      
+AliMUONPadStatusMapMaker::~AliMUONPadStatusMapMaker()
 {
-  /// Get the pad status
-  AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(fStatus->FindObject(detElemId,manuId));
-  return param->ValueAsInt(manuChannel);
+  /// dtor
+  delete fStatusMap;
+  delete fRejectProbabilities;
+  delete fRejectList;
 }
 
 //_____________________________________________________________________________
-AliMUONVStore*
-AliMUONPadStatusMapMaker::MakeEmptyPadStatusMap()
+AliMUONVCalibParam*
+AliMUONPadStatusMapMaker::ComputeStatusMap(Int_t detElemId, Int_t manuId) const
 {
-  /// Make an empty (but complete) statusMap
-    
-    AliMUONVStore* store = new AliMUON2DMap(true);
-    
-    TList* list = AliMpManuList::ManuList();
-    
-    AliMpIntPair* pair;
+  /// Compute the status map for a given manu, and add it to our internal
+  /// fStatusMap internal storage
+  
+  AliCodeTimerAuto("(Int_t,Int_t)",0)
     
-    TIter next(list);
+  AliMUONVCalibParam* param = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),
+                                                      detElemId,manuId,-1);    
+                                    
+  Bool_t ok = fStatusMap->Add(param);
+  if (!ok)
+  {
+    AliFatal(Form("Could not add manu %d of de %d",manuId,detElemId));
+  }
+                                  
+  AliMUONVCalibParam* neighbours = fkStatusMaker.Neighbours(detElemId,manuId);
+  
+  AliMUONVCalibParam* statusParam = fkStatusMaker.PadStatus(detElemId,manuId);
+  
+  Int_t n = neighbours->Dimension();
+  
+  for ( Int_t manuChannel = 0; manuChannel < param->Size(); ++manuChannel )
+  {
+    Int_t statusMap(0);
     
-    while ( ( pair = static_cast<AliMpIntPair*>(next()) ) ) 
+    Int_t x = neighbours->ValueAsIntFast(manuChannel,0);
+    if ( x < 0 ) 
     {
-      Int_t detElemId = pair->GetFirst();
-      Int_t manuId = pair->GetSecond();
-      AliMUONVCalibParam* param = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),
-                                                          detElemId,manuId,
-                                                          0);
-      store->Add(param);
+      // channel is not a valid one (i.e. (manuId,manuChannel) is not an existing pad)
+      statusMap = -1;//fgkSelfDead;
+      continue;
     }
-    
-    delete list;
-    
-    return store;
+        
+    for ( Int_t i = 0; i < n; ++i )
+    {
+      // Compute the statusmap related to the status of neighbouring
+      // pads. An invalid pad means "outside of edges".
+            
+      Int_t y = neighbours->ValueAsIntFast(manuChannel,i);      
+      Int_t m,c;
+      neighbours->UnpackValue(y,m,c);
+      if ( c < 0 ) continue;
+      Int_t status = 0;
+      if ( !m )
+      {
+        status = -1;
+      }
+      else
+      {
+        status = statusParam->ValueAsIntFast(c); //fkStatusMaker.PadStatus(detElemId,m,c);
+      }
+      if ( ( fMask != 0 ) && ( (status & fMask) != 0 ) )
+      {
+        statusMap |= (1<<i);
+      }
+    }    
+    param->SetValueAsIntFast(manuChannel,0,statusMap);
+  }
+  return param;
 }
 
 //_____________________________________________________________________________
-AliMUONVStore*
-AliMUONPadStatusMapMaker::MakePadStatusMap(const AliMUONVStore& status,
-                                           Int_t mask)
+void 
+AliMUONPadStatusMapMaker::RefreshRejectProbabilities()
 {
-  /// Given the status store for all pads, compute a status map store
-  /// for all pads. 
-  /// \param status
-  /// \param mask is the status mask to be tested to tell if a pad is ok or not
-  
-  fStatus = &status;
-  fMask = mask;
+  /// From the (fixed) fRejectProbabilities, compute
+  /// a fRejectList that will be valid for one event
+  /// If fRejectProbabilities=0x0 it means we're dealing with
+  /// trivial probabilities (0 or 1) and those are assumed to be already
+  /// in fRejectList then.
   
-  TStopwatch timer;  
-  timer.Start(kTRUE);
+  if ( !fRejectProbabilities ) return;
   
-  AliMUONVStore* neighbourStore = fCalibrationData.Neighbours();
+  AliCodeTimerAuto("",0);
   
-  AliMUONVStore* statusMap = status.Create();
+  fRejectList->Clear();
   
-  TIter next(status.CreateIterator());
-  AliMUONVCalibParam* statusEntry;
+  TIter next(fRejectProbabilities->CreateIterator());
+  AliMUONVCalibParam* paramProba;
+  AliMUONVCalibParam* paramReject;
   
-  while ( ( statusEntry = static_cast<AliMUONVCalibParam*>(next()) ) )
+  while ( ( paramProba = static_cast<AliMUONVCalibParam*>(next()) ) )
   {
-    Int_t detElemId = statusEntry->ID0();
-    Int_t manuId = statusEntry->ID1();
-        
-    AliMUONVCalibParam* statusMapEntry = static_cast<AliMUONVCalibParam*>
-      (statusMap->FindObject(detElemId,manuId));
-
-    if (!statusMapEntry)
-    {
-      statusMapEntry = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),
-                                               detElemId,manuId,0);
-      statusMap->Add(statusMapEntry);
-    }
+    paramReject = new AliMUONCalibParamNF(1,paramProba->Size(),paramProba->ID0(),paramProba->ID1(),0.0);
     
-    AliMUONVCalibParam* neighbours = static_cast<AliMUONVCalibParam*>
-      (neighbourStore->FindObject(detElemId,manuId));
+    Int_t n(0);
     
-    if (!neighbours)
+    for ( Int_t i = 0; i < paramProba->Size(); ++i ) 
     {
-      AliFatal(Form("Could not find neighbours for DE %d manuId %d",
-                    detElemId,manuId));
-      continue;
-    }
-    
-    for ( Int_t manuChannel = 0; manuChannel < statusEntry->Size(); ++manuChannel ) 
-    {
-      // Loop over channels and for each channel loop on its immediate neighbours
-      // to produce a statusMap word for this channel.
+      Float_t proba = paramProba->ValueAsFloat(i);
+      Float_t x(proba);
       
-      Int_t statusMapValue(0);
-
-      Int_t x = neighbours->ValueAsInt(manuChannel,0);
-      
-      if ( x > 0 )
-      { 
-        // channel is a valid one (i.e. (manuId,manuChannel) is an existing pad)
-        statusMapValue = ComputeStatusMap(*neighbours,manuChannel,detElemId);
-      }
-      else
+      if ( proba > 0.0 && proba < 1.0 ) 
       {
-        statusMapValue = fgkSelfDead;
+        x = gRandom->Rndm();
+        proba = ( x < proba ) ? 1.0 : 0.0;
       }
       
-      statusMapEntry->SetValueAsInt(manuChannel,0,statusMapValue);
+      if (proba>0.0)
+      {
+        ++n;
+        paramReject->SetValueAsFloat(i,0,proba);
+      }
     }
+    if (n) fRejectList->Add(paramReject);
   }
-  timer.Stop();
+}
+
+//_____________________________________________________________________________
+Int_t
+AliMUONPadStatusMapMaker::StatusMap(Int_t detElemId, Int_t manuId, 
+                                    Int_t manuChannel) const
+                                      
+{
+  /// Get the pad status map
   
-  StdoutToAliInfo(
-                  cout << "MakePadStatusMap total timer : ";
-                  timer.Print();
-                  cout << endl;
-                  );
+  AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(fStatusMap->FindObject(detElemId,manuId));
+  if (!param)
+  {
+    if ( fComputeOnDemand ) 
+    {
+      // not yet computed, so do it now
+      param = ComputeStatusMap(detElemId,manuId);
+    }
+    else
+    {
+      // we're locked. probably a bad manuId ?
+      return fgkSelfDead;
+    }
+  }
+  
+  Int_t statusMap = param->ValueAsInt(manuChannel);
+  
+  AliMUONVCalibParam* r = static_cast<AliMUONVCalibParam*>(fRejectList->FindObject(detElemId,manuId));
+  
+  if (r)
+  {
+    Float_t v= r->ValueAsFloat(manuChannel);
+    
+    assert (v==0.0 || v==1.0 ); 
 
+    if ( v > 0 ) 
+    {
+      statusMap |= fgkSelfDead;
+    }
+  }
+  
   return statusMap;
+  
 }
-