]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrackerData.cxx
Fix coverity defect
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerData.cxx
index e3824904fcc82bb9ef4c3a0a3d46e8a0378604d4..b595d0e0f9ddbf649b1efd48f988ae7e14b4fe73 100644 (file)
@@ -24,6 +24,7 @@
 #include "AliMUON1DMap.h"
 #include "AliMUON2DMap.h"
 #include "AliMUONCalibParamND.h"
+#include "AliMUONRejectList.h"
 #include "AliMUONSparseHisto.h"
 #include "AliMUONVStore.h"
 #include "AliMpBusPatch.h"
 #include "AliMpDEIterator.h"
 #include "AliMpDEManager.h"
 #include "AliMpDetElement.h"
-#include "AliMpHVNamer.h"
+#include "AliMpDCSNamer.h"
 #include "AliMpManuIterator.h"
+#include "AliMpEncodePair.h"
+#include "AliMpSegmentation.h"
 #include <Riostream.h>
 #include <TClass.h>
 #include <TMath.h>
 #include <TObjArray.h>
 #include <TObjString.h>
 #include <TString.h>
+#include <TTimeStamp.h>
 #include <TVector2.h>
+#include <cassert>
 #include <float.h>
 
 /// \class AliMUONTrackerData
@@ -81,11 +86,13 @@ fXmin(0.0),
 fXmax(0.0),
 fIsChannelLevelEnabled(kTRUE),
 fIsManuLevelEnabled(kTRUE),
+fIsBustPatchLevelEnabled(kTRUE),
+fIsPCBLevelEnabled(kTRUE),
 fNofDDLs(0),
 fNofEventsPerDDL(0x0)
 {  
   /// ctor
-  memset(fHistogramming,0,sizeof(Int_t)); // histogramming is off by default. Use MakeHistogramForDimension to turn it on.
+  memset(fHistogramming,0,fExternalDimension*sizeof(Int_t)); // histogramming is off by default. Use MakeHistogramForDimension to turn it on.
   fExternalDimensionNames->SetOwner(kTRUE);
   fDimensionNames->SetOwner(kTRUE);  
   fDimensionNames->AddAt(new TObjString("occ"),IndexOfOccupancyDimension());
@@ -94,6 +101,524 @@ fNofEventsPerDDL(0x0)
   Clear();
 }
 
+//_____________________________________________________________________________
+AliMUONTrackerData::AliMUONTrackerData(const char* name, const char* title,
+                                       const AliMUONVStore& manuValues)
+: AliMUONVTrackerData(name,title),
+fIsSingleEvent(kFALSE),
+fChannelValues(0x0),
+fManuValues(0x0),
+fBusPatchValues(0x0),
+fDEValues(0x0),
+fChamberValues(0x0),
+fPCBValues(0x0),
+fDimension(0),
+fNevents(0),
+fDimensionNames(0x0),
+fExternalDimensionNames(0x0),
+fExternalDimension(0),
+fHistogramming(0x0),
+fHistos(0x0),
+fXmin(0.0),
+fXmax(0.0),
+fIsChannelLevelEnabled(kFALSE),
+fIsManuLevelEnabled(kTRUE),
+fIsBustPatchLevelEnabled(kTRUE),
+fIsPCBLevelEnabled(kTRUE),
+fNofDDLs(0),
+fNofEventsPerDDL(0x0)
+{  
+  /// ctor with pre-computed values at the manu level
+  /// In this case, we force fIsChannelLevelEnabled = kFALSE
+  /// ctor
+  
+  if (manuValues.GetSize()==0)
+  {
+    AliFatal("Cannot create a tracker data from nothing in that case !");
+  }
+  
+  if ( !AliMpDDLStore::Instance(kFALSE) && !AliMpManuStore::Instance(kFALSE) )
+  {
+    AliError("Cannot work without (full) mapping");
+    return;
+  }
+  
+  TIter next(manuValues.CreateIterator());
+  AliMUONVCalibParam* m = static_cast<AliMUONVCalibParam*>(next());
+  
+  Int_t dimension = ( m->Dimension() - fgkExtraDimension - fgkVirtualExtraDimension ) / 2;
+  
+  fDimension = External2Internal(dimension)+fgkExtraDimension;
+  
+  fDimensionNames = new TObjArray(fDimension+fgkVirtualExtraDimension);
+  fExternalDimensionNames = new TObjArray(dimension);
+  fExternalDimension = dimension;
+  fHistogramming = new Int_t[fExternalDimension];
+  memset(fHistogramming,0,fExternalDimension*sizeof(Int_t)); // histogramming is off by default. Use MakeHistogramForDimension to turn it on.
+
+  fExternalDimensionNames->SetOwner(kTRUE);
+  fDimensionNames->SetOwner(kTRUE);  
+  fDimensionNames->AddAt(new TObjString("occ"),IndexOfOccupancyDimension());
+  fDimensionNames->AddAt(new TObjString("N"),IndexOfNumberDimension());
+  fDimensionNames->AddAt(new TObjString("n"),NumberOfDimensions()-fgkVirtualExtraDimension);
+  Clear();
+  TArrayI nevents(AliDAQ::NumberOfDdls("MUONTRK"));
+  AssertStores();
+  
+  next.Reset();
+  AliMUONVCalibParam* external;
+  
+  while ( ( external = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    Int_t detElemId, manuId;
+    
+    GetDEManu(*external,detElemId,manuId);
+    
+    AliMUONVCalibParam* chamber(0x0);
+    AliMUONVCalibParam* de(0x0);
+    AliMUONVCalibParam* busPatch(0x0);
+    AliMUONVCalibParam* pcb(0x0);
+    AliMUONVCalibParam* manu(0x0);
+    AliMUONVCalibParam* channel(0x0);
+    AliMpDetElement* mpde(0x0);
+    
+    AliMUONVCalibParam* wec = new AliMUONCalibParamND(external->Dimension()-1,1,detElemId,manuId,0);
+    // as external, but without event count
+    wec->SetValueAsDouble(0,0,external->ValueAsDouble(0,0));
+    wec->SetValueAsDouble(0,1,external->ValueAsDouble(0,1));
+    wec->SetValueAsDouble(0,2,external->ValueAsDouble(0,2));
+    wec->SetValueAsDouble(0,3,external->ValueAsDouble(0,3));
+    
+    Int_t mid = GetParts(wec,chamber,de,busPatch,pcb,manu,channel,mpde);
+
+    if ( manuId != mid ) 
+    {
+      AliError(Form("Something is wrong for DE %5d : manuId = %d vs mid = %d",detElemId,manuId,mid));
+      continue;
+    }
+    
+    if ( manuId < 0 ) 
+    {
+      AliError("Got a < 0 manuId. Should not happen here !");
+      continue;
+    }
+    
+    assert(channel==0x0);
+    
+    Int_t n1 = manu->ValueAsInt(0,IndexOfNumberDimension());
+    Int_t n2 = external->ValueAsInt(0,IndexOfNumberDimension());
+    if  ( n1 != n2 )
+    {
+      AliError(Form("Incoherent number of manu channels for DE %5d MANU %5d : %d vs %d",
+                    detElemId,manuId,n1,n2));
+    }
+    
+    Int_t nevt = external->ValueAsInt(0,4);
+    
+    Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
+
+    Int_t ddl = AliMpDDLStore::Instance()->GetDDLfromBus(busPatchId);
+
+    if ( nevents[ddl] == 0 ) 
+    {
+      nevents[ddl] = nevt;
+    }
+    else
+    {
+      if ( nevents.At(ddl) != nevt ) 
+      {
+        AliError(Form("Nevt mismatch for DE %5d MANU %5d DDL %d : %d vs %d",
+                      detElemId,manuId,ddl,nevents.At(ddl),nevt));
+        continue;
+      }
+    }
+    
+    for ( Int_t i = 0; i < wec->Dimension()-1; ++i ) 
+    {
+      manu->SetValueAsDouble(0,i,manu->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+      
+      busPatch->SetValueAsDouble(0,i,busPatch->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+      
+      de->SetValueAsDouble(0,i,de->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+      
+      chamber->SetValueAsDouble(0,i,chamber->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+    }    
+    delete wec;
+  }  
+  
+  UpdateNumberOfEvents(&nevents);
+
+}
+
+//_____________________________________________________________________________
+AliMUONTrackerData::AliMUONTrackerData(const char* name, const char* title,
+                                       const AliMUONVStore& deOrBpValues, Int_t val)
+: AliMUONVTrackerData(name,title),
+fIsSingleEvent(kFALSE),
+fChannelValues(0x0),
+fManuValues(0x0),
+fBusPatchValues(0x0),
+fDEValues(0x0),
+fChamberValues(0x0),
+fPCBValues(0x0),
+fDimension(0),
+fNevents(0),
+fDimensionNames(0x0),
+fExternalDimensionNames(0x0),
+fExternalDimension(0),
+fHistogramming(0x0),
+fHistos(0x0),
+fXmin(0.0),
+fXmax(0.0),
+fIsChannelLevelEnabled(kFALSE),
+fIsManuLevelEnabled(kFALSE),
+fIsBustPatchLevelEnabled(kFALSE),
+fIsPCBLevelEnabled(kFALSE),
+fNofDDLs(0),
+fNofEventsPerDDL(0x0)
+{  
+  /// ctor with values at the detection element OR bus patch level
+  /// In this case, we force fIsChannelLevelEnabled = fIsManuLevelEnabled = kFALSE
+  /// ctor
+  
+  if (deOrBpValues.GetSize()==0)
+  {
+    AliFatal("Cannot create a tracker data from nothing in that case !");
+  }
+  
+  if ( !AliMpDDLStore::Instance(kFALSE) && !AliMpManuStore::Instance(kFALSE) )
+  {
+    AliError("Cannot work without (full) mapping");
+    return;
+  }
+  
+  if ( val == 1 ) 
+  {
+    BuildFromDEStore(deOrBpValues);
+  }
+  else if ( val == 2 )
+  {
+    BuildFromBPStore(deOrBpValues);
+  }
+  else
+  {
+    AliFatal("Wrong parameter. Must be 1 or 2");
+  }
+  
+  
+}
+
+//_____________________________________________________________________________
+void AliMUONTrackerData::BuildFromDEStore(const AliMUONVStore& deValues)
+{
+  /// Fill internals from a store of values at the detection element level
+  
+  TIter next(deValues.CreateIterator());
+  AliMUONVCalibParam* m = static_cast<AliMUONVCalibParam*>(next());
+  
+  Int_t dimension = ( m->Dimension() - fgkExtraDimension - fgkVirtualExtraDimension ) / 2;
+  
+  fDimension = External2Internal(dimension)+fgkExtraDimension;
+  
+  fDimensionNames = new TObjArray(fDimension+fgkVirtualExtraDimension);
+  fExternalDimensionNames = new TObjArray(dimension);
+  fExternalDimension = dimension;
+  fHistogramming = new Int_t[fExternalDimension];
+  memset(fHistogramming,0,fExternalDimension*sizeof(Int_t)); // histogramming is off by default. Use MakeHistogramForDimension to turn it on.
+  
+  fExternalDimensionNames->SetOwner(kTRUE);
+  fDimensionNames->SetOwner(kTRUE);  
+  fDimensionNames->AddAt(new TObjString("occ"),IndexOfOccupancyDimension());
+  fDimensionNames->AddAt(new TObjString("N"),IndexOfNumberDimension());
+  fDimensionNames->AddAt(new TObjString("n"),NumberOfDimensions()-fgkVirtualExtraDimension);
+  Clear();
+  TArrayI nevents(AliDAQ::NumberOfDdls("MUONTRK"));
+  AssertStores();
+  
+  next.Reset();
+  AliMUONVCalibParam* external;
+  
+  while ( ( external = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    Int_t detElemId = external->ID0();
+    
+    AliMpDetElement* mpde = AliMpDDLStore::Instance()->GetDetElement(detElemId,kFALSE);
+    
+    if (!mpde)
+    {
+      AliError(Form("Got an invalid DE (%d) from external store",detElemId));
+      continue;
+    }
+    
+    Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
+    AliMUONVCalibParam* chamber = ChamberParam(chamberId,kTRUE);
+    AliMUONVCalibParam* de = DetectionElementParam(detElemId,kTRUE);
+    
+    AliMUONVCalibParam* wec = new AliMUONCalibParamND(external->Dimension()-1,1,detElemId,0,0);
+    // as external, but without event count
+    wec->SetValueAsDouble(0,0,external->ValueAsDouble(0,0));
+    wec->SetValueAsDouble(0,1,external->ValueAsDouble(0,1));
+    wec->SetValueAsDouble(0,2,external->ValueAsDouble(0,2));
+    wec->SetValueAsDouble(0,3,external->ValueAsDouble(0,3));
+    
+    Int_t n1 = de->ValueAsInt(0,IndexOfNumberDimension());
+    Int_t n2 = external->ValueAsInt(0,IndexOfNumberDimension());
+    if  ( n1 != n2 )
+    {
+      AliError(Form("Incoherent number of dimensions for DE%d : %d vs %d",
+                    detElemId,n1,n2));
+      continue;
+    }
+    
+    Int_t nevt = external->ValueAsInt(0,4);
+    
+    Int_t ddl = mpde->GetDdlId();
+    
+    if ( nevents[ddl] == 0 ) 
+    {
+      nevents[ddl] = nevt;
+    }
+    else
+    {
+      if ( nevents.At(ddl) != nevt ) 
+      {
+        AliError(Form("Nevt mismatch for DE %5d  DDL %d : %d vs %d",
+                      detElemId,ddl,nevents.At(ddl),nevt));
+        continue;
+      }
+    }
+    
+    for ( Int_t i = 0; i < wec->Dimension()-1; ++i ) 
+    {
+      de->SetValueAsDouble(0,i,de->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+      
+      chamber->SetValueAsDouble(0,i,chamber->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+    }    
+    delete wec;
+  }  
+  
+  UpdateNumberOfEvents(&nevents);
+}
+
+//_____________________________________________________________________________
+void AliMUONTrackerData::BuildFromBPStore(const AliMUONVStore& bpValues)
+{
+  /// Fill internals from a store of values at the bus patch level
+  
+  fIsBustPatchLevelEnabled = kTRUE;
+  
+  TIter next(bpValues.CreateIterator());
+  AliMUONVCalibParam* m = static_cast<AliMUONVCalibParam*>(next());
+  
+  Int_t dimension = ( m->Dimension() - fgkExtraDimension - fgkVirtualExtraDimension ) / 2;
+  
+  fDimension = External2Internal(dimension)+fgkExtraDimension;
+  
+  fDimensionNames = new TObjArray(fDimension+fgkVirtualExtraDimension);
+  fExternalDimensionNames = new TObjArray(dimension);
+  fExternalDimension = dimension;
+  fHistogramming = new Int_t[fExternalDimension];
+  memset(fHistogramming,0,fExternalDimension*sizeof(Int_t)); // histogramming is off by default. Use MakeHistogramForDimension to turn it on.
+  
+  fExternalDimensionNames->SetOwner(kTRUE);
+  fDimensionNames->SetOwner(kTRUE);  
+  fDimensionNames->AddAt(new TObjString("occ"),IndexOfOccupancyDimension());
+  fDimensionNames->AddAt(new TObjString("N"),IndexOfNumberDimension());
+  fDimensionNames->AddAt(new TObjString("n"),NumberOfDimensions()-fgkVirtualExtraDimension);
+  Clear();
+  TArrayI nevents(AliDAQ::NumberOfDdls("MUONTRK"));
+  AssertStores();
+  
+  next.Reset();
+  AliMUONVCalibParam* external;
+  
+  while ( ( external = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    Int_t busPatchId = external->ID0();
+    
+    AliMpBusPatch* mpbp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId,kFALSE);
+    
+    if (!mpbp)
+    {
+      AliError(Form("Got an invalid buspatchId (%d) from external store",busPatchId));
+      continue;
+    }
+    
+    Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
+    Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
+    AliMUONVCalibParam* chamber = ChamberParam(chamberId,kTRUE);
+    AliMUONVCalibParam* de = DetectionElementParam(detElemId,kTRUE);
+    AliMUONVCalibParam* bp = BusPatchParam(busPatchId,kTRUE);
+    
+    AliMUONVCalibParam* wec = new AliMUONCalibParamND(external->Dimension()-1,1,busPatchId,0,0);
+    // as external, but without event count
+    wec->SetValueAsDouble(0,0,external->ValueAsDouble(0,0));
+    wec->SetValueAsDouble(0,1,external->ValueAsDouble(0,1));
+    wec->SetValueAsDouble(0,2,external->ValueAsDouble(0,2));
+    wec->SetValueAsDouble(0,3,external->ValueAsDouble(0,3));
+    
+    Int_t n1 = bp->ValueAsInt(0,IndexOfNumberDimension());
+    Int_t n2 = external->ValueAsInt(0,IndexOfNumberDimension());
+    if  ( n1 != n2 )
+    {
+      AliError(Form("Incoherent number of dimensions for BP%d : %d vs %d",
+                    busPatchId,n1,n2));
+      continue;
+    }
+    
+    Int_t nevt = external->ValueAsInt(0,4);
+    
+    Int_t ddl = mpbp->GetDdlId();
+    
+    if ( nevents[ddl] == 0 ) 
+    {
+      nevents[ddl] = nevt;
+    }
+    else
+    {
+      if ( nevents.At(ddl) != nevt ) 
+      {
+        AliError(Form("Nevt mismatch for BP %5d  DDL %d : %d vs %d",
+                      busPatchId,ddl,nevents.At(ddl),nevt));
+        continue;
+      }
+    }
+    
+    for ( Int_t i = 0; i < wec->Dimension()-1; ++i ) 
+    {
+      bp->SetValueAsDouble(0,i,bp->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+      
+      de->SetValueAsDouble(0,i,de->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+      
+      chamber->SetValueAsDouble(0,i,chamber->ValueAsDouble(0,i) + wec->ValueAsDouble(0,i));
+    }    
+    delete wec;
+  }  
+  
+  UpdateNumberOfEvents(&nevents);  
+}
+
+//_____________________________________________________________________________
+AliMUONTrackerData::AliMUONTrackerData(const char* name, const char* title,
+                                       const AliMUONRejectList& rejectList)
+: AliMUONVTrackerData(name,title),
+fIsSingleEvent(kFALSE),
+fChannelValues(0x0),
+fManuValues(0x0),
+fBusPatchValues(0x0),
+fDEValues(0x0),
+fChamberValues(0x0),
+fPCBValues(0x0),
+fDimension(External2Internal(1)+fgkExtraDimension),
+fNevents(0),
+fDimensionNames(new TObjArray(fDimension+fgkVirtualExtraDimension)),
+fExternalDimensionNames(new TObjArray(1)),
+fExternalDimension(1),
+fHistogramming(new Int_t[fExternalDimension]),
+fHistos(0x0),
+fXmin(0.0),
+fXmax(0.0),
+fIsChannelLevelEnabled(kTRUE),
+fIsManuLevelEnabled(kTRUE),
+fIsBustPatchLevelEnabled(kTRUE),
+fIsPCBLevelEnabled(kFALSE),
+fNofDDLs(0),
+fNofEventsPerDDL(0x0)
+{  
+  
+   /// ctor with values from the given rejectlist
+    
+  if ( !AliMpDDLStore::Instance(kFALSE) && !AliMpManuStore::Instance(kFALSE) )
+  {
+    AliError("Cannot work without (full) mapping");
+    return;
+  }
+  
+  memset(fHistogramming,0,fExternalDimension*sizeof(Int_t)); // histogramming is off by default. Use MakeHistogramForDimension to turn it on.
+  
+  fExternalDimensionNames->SetOwner(kTRUE);
+  fDimensionNames->SetOwner(kTRUE);  
+  fDimensionNames->AddAt(new TObjString("occ"),IndexOfOccupancyDimension());
+  fDimensionNames->AddAt(new TObjString("N"),IndexOfNumberDimension());
+  fDimensionNames->AddAt(new TObjString("n"),NumberOfDimensions()-fgkVirtualExtraDimension);
+  Clear();
+  TArrayI nevents(AliDAQ::NumberOfDdls("MUONTRK"));
+  AssertStores();
+  
+  
+  for ( Int_t chamberId = 0; chamberId < AliMpConstants::NofChambers(); ++chamberId ) 
+  {
+//    AliMUONVCalibParam* chamber = ChamberParam(chamberId,kTRUE);
+
+    // FIXME : update the chamber value ?
+    
+    AliMpDEIterator deit;
+  
+    deit.First(chamberId);
+
+    while ( !deit.IsDone() ) 
+    {
+      AliMpDetElement* mpde = deit.CurrentDE();
+      
+      Int_t detElemId = mpde->GetId();
+
+      AliMUONVCalibParam* de = DetectionElementParam(detElemId,kTRUE);
+
+      DispatchValue(*de,0,rejectList.DetectionElementProbability(detElemId),0.0,mpde->NofChannels());
+      
+      for ( Int_t iBusPatch = 0; iBusPatch < mpde->GetNofBusPatches(); ++iBusPatch ) 
+      {
+        Int_t busPatchId = mpde->GetBusPatchId(iBusPatch);
+        
+        AliMUONVCalibParam* bp = BusPatchParam(busPatchId,kTRUE);
+
+        AliMpBusPatch* mpbp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
+
+        Int_t nch(0);
+        
+        for ( Int_t iManu = 0 ;iManu < mpbp->GetNofManus(); ++iManu )
+        {
+          Int_t manuId = mpbp->GetManuId(iManu);
+          
+          nch += mpde->NofChannelsInManu(manuId);
+          
+          AliMUONVCalibParam* manu = ManuParam(detElemId,manuId,kTRUE);
+          
+          DispatchValue(*manu,0,rejectList.ManuProbability(detElemId,manuId),0.0,mpde->NofChannelsInManu(manuId));
+          
+          AliMUONVCalibParam* c = ChannelParam(detElemId,manuId);
+          
+          if (!c)
+          {
+            c = new AliMUONCalibParamND(Dimension(),
+                                        AliMpConstants::ManuNofChannels(),
+                                        detElemId,
+                                        manuId,
+                                        0.0);
+            fChannelValues->Add(c);
+          }
+          
+          for ( Int_t manuChannel = 0; manuChannel < AliMpConstants::ManuNofChannels(); ++manuChannel )
+          {
+            DispatchValue(*c,manuChannel,rejectList.ChannelProbability(detElemId,manuId,manuChannel),0.0,1);
+          }
+        }
+
+        DispatchValue(*bp,0,rejectList.BusPatchProbability(busPatchId),0.0,nch);
+
+      }
+      
+      deit.Next();
+    }
+  }
+  
+  
+  SetDimensionName(0,"RejectProba");
+
+  UpdateNumberOfEvents(0x0);
+}
+
 //_____________________________________________________________________________
 AliMUONTrackerData::~AliMUONTrackerData()
 {
@@ -131,6 +656,8 @@ AliMUONTrackerData::Add(const AliMUONTrackerData& data)
 
   // First cross check we have compatible objects.
   
+  AliCodeTimerAuto("",0);
+  
   if ( fIsChannelLevelEnabled != data.fIsChannelLevelEnabled ) 
   {
     AliError("Incompatible IsChannelLevelEnabled status");
@@ -164,7 +691,7 @@ AliMUONTrackerData::Add(const AliMUONTrackerData& data)
   if ( ( !fHistogramming && data.fHistogramming ) || ( fHistogramming && !data.fHistogramming ) 
       || fXmin != data.fXmin || fXmax != data.fXmax ) 
   {
-    AliError(Form("Incompatible histogramming (%x vs %x) (xmax = %e vs %e ; xmin = %e vs %e)",
+    AliError(Form("Incompatible histogramming (%p vs %p) (xmax = %e vs %e ; xmin = %e vs %e)",
              fHistogramming,data.fHistogramming,fXmax,data.fXmax,fXmin,data.fXmin));
     return kFALSE;
   }
@@ -252,6 +779,11 @@ AliMUONTrackerData::Add(const AliMUONTrackerData& data)
     }
   }
   
+  for ( Int_t i = 0 ; i < fNofDDLs; ++i ) 
+  {
+    fNevents = TMath::Max(fNevents,fNofEventsPerDDL[i]);
+  }
+  
   return kTRUE;
 }
 
@@ -329,21 +861,10 @@ AliMUONTrackerData::Replace(const AliMUONVStore& store)
 
 //_____________________________________________________________________________
 Bool_t
-AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bool_t replace)
+AliMUONTrackerData::UpdateNumberOfEvents(TArrayI* nevents)
 {
-  /// Add the given external store to our internal store
+  /// Update the number of events
   
-  AliCodeTimerAuto(GetName());
-    
-  if ( !replace)
-  {
-    if ( IsSingleEvent() && NumberOfEvents(-1) == 1 ) 
-    {
-      AliError(Form("%s is supposed to be single event only",GetName()));
-      return kFALSE;
-    }  
-  }
-
   if (!fNofDDLs)
   {
     fNofDDLs = AliDAQ::NumberOfDdls("MUONTRK");
@@ -353,7 +874,7 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bo
       fNofEventsPerDDL[i] = 0;
     }
   }
-    
+  
   if (nevents)
   {
     if (nevents->GetSize() != fNofDDLs ) 
@@ -377,6 +898,14 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bo
       fNevents = TMath::Max(fNevents,fNofEventsPerDDL[i]);
     }
   }
+  return kTRUE;
+}
+
+//_____________________________________________________________________________
+void
+AliMUONTrackerData::AssertStores()
+{
+  /// Insure our stores are allocated
   
   if (!fChamberValues)
   {
@@ -386,6 +915,11 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bo
     // get number of bus patches and number of detection element
     // to initialize fBusPatchValues and fDEValues below
     
+    if (!AliMpDDLStore::Instance(false))
+    {
+      AliMpCDB::LoadAll();
+    }
+    
     TIter next(AliMpDDLStore::Instance()->CreateBusPatchIterator());
     while ( next() ) ++numberOfBusPatches;
     AliMpDEIterator deIt;
@@ -396,19 +930,47 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bo
       deIt.Next();
     }
     
-               if ( fIsChannelLevelEnabled ) 
-               {
-                       fChannelValues = new AliMUON2DMap(kTRUE);
-               }
+    if ( fIsChannelLevelEnabled ) 
+    {
+      fChannelValues = new AliMUON2DMap(kTRUE);
+    }
     if  ( fIsManuLevelEnabled ) 
     {
       fManuValues = new AliMUON2DMap(kTRUE);
     }
-    fPCBValues = new AliMUON2DMap(kFALSE);
-    fBusPatchValues = new AliMUON1DMap(numberOfBusPatches);
+    if ( fIsPCBLevelEnabled )
+    {
+      fPCBValues = new AliMUON2DMap(kFALSE);
+    }
+    if ( fIsBustPatchLevelEnabled )
+    {
+      fBusPatchValues = new AliMUON1DMap(numberOfBusPatches);
+    }
     fDEValues = new AliMUON1DMap(numberOfDEs);
     fChamberValues = new AliMUON1DArray;
   }
+}
+
+//_____________________________________________________________________________
+Bool_t
+AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bool_t replace)
+{
+  /// Add the given external store to our internal store
+  
+  AliCodeTimerAuto(GetName(),0);
+    
+  if ( !replace)
+  {
+    if ( IsSingleEvent() && NumberOfEvents(-1) == 1 ) 
+    {
+      AliError(Form("%s is supposed to be single event only",GetName()));
+      return kFALSE;
+    }  
+  }
+
+  UpdateNumberOfEvents(nevents);
+  
+  AssertStores();
   
   TIter next(store.CreateIterator());
   AliMUONVCalibParam* external;
@@ -426,9 +988,13 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, TArrayI* nevents, Bo
       return kFALSE;
     }
     
-
-    AliMUONVCalibParam* chamber, *de, *busPatch, *pcb, *manu, *channel;
-    AliMpDetElement* mpde;
+    AliMUONVCalibParam* chamber(0x0);
+    AliMUONVCalibParam* de(0x0);
+    AliMUONVCalibParam* busPatch(0x0);
+    AliMUONVCalibParam* pcb(0x0);
+    AliMUONVCalibParam* manu(0x0);
+    AliMUONVCalibParam* channel(0x0);
+    AliMpDetElement* mpde(0x0);
     
     Int_t manuId = GetParts(external,chamber,de,busPatch,pcb,manu,channel,mpde);
     
@@ -567,7 +1133,7 @@ AliMUONTrackerData::CreateBusPatchParam(Int_t busPatchId) const
 {
   /// Create storage for one bus patch
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMpBusPatch* bp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
   
@@ -637,7 +1203,7 @@ AliMUONTrackerData::CreateChamberParam(Int_t chamberId) const
 {
   /// Create storage for one chamber
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMUONVCalibParam* chamber = new AliMUONCalibParamND(Dimension(),1,chamberId,0,0.0);
   
@@ -687,7 +1253,7 @@ AliMUONTrackerData::Channel(Int_t detElemId, Int_t manuId,
 //_____________________________________________________________________________
 AliMUONVCalibParam* 
 AliMUONTrackerData::ChannelParam(Int_t detElemId, Int_t manuId,
-                                 AliMUONVCalibParam* external) const
+                                 const AliMUONVCalibParam* external) const
 {
   /// Return (if it exist) the VCalibParam for a given manu
   
@@ -719,6 +1285,7 @@ AliMUONTrackerData::Clear(Option_t*)
   {
     fNofEventsPerDDL[i] = 0;
   }
+  fNevents = 0;
   NumberOfEventsChanged();
 }
 
@@ -739,7 +1306,7 @@ AliMUONTrackerData::CreateDouble(const AliMUONVCalibParam& param,
 {
   /// Create a double version of VCalibParam, for internal use
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMUONVCalibParam* c = new AliMUONCalibParamND(Dimension(),
                                                   param.Size(),
@@ -796,7 +1363,7 @@ AliMUONTrackerData::CreateDetectionElementParam(Int_t detElemId) const
 {
   /// Create storage for one detection element
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMUONVCalibParam*  de = new AliMUONCalibParamND(Dimension(),1,detElemId,0,0.0);
   
@@ -1078,13 +1645,13 @@ AliMUONTrackerData::GetDEManu(const AliMUONVCalibParam& param,
   {
     // we (probably) get a manu serial number
     Int_t serial = param.ID0();
-    AliMpIntPair pair = AliMpManuStore::Instance()->GetDetElemIdManu(serial);
-    detElemId = pair.GetFirst();
-    manuId = pair.GetSecond();
+    MpPair_t pair = AliMpManuStore::Instance()->GetDetElemIdManu(serial);
+    detElemId = AliMp::PairFirst(pair);
+    manuId = AliMp::PairSecond(pair);
     if ( !detElemId ) 
     {
-      AliError(Form("DE %d manuId %d from serial %d is not correct !",
-                    detElemId,manuId,serial));
+      AliDebug(1,Form("DE %d manuId %d from serial %d is not correct !",
+                      detElemId,manuId,serial));
     }
   }
   else
@@ -1109,6 +1676,9 @@ AliMUONTrackerData::GetParts(AliMUONVCalibParam* external,
 {
   /// Get containers at all levels
  
+  chamber = de = busPatch = pcb = manu = channel = 0x0;
+  mpde = 0x0;
+  
   AliMpDDLStore* ddlStore = AliMpDDLStore::Instance();
   
   Int_t detElemId;
@@ -1116,28 +1686,44 @@ AliMUONTrackerData::GetParts(AliMUONVCalibParam* external,
   
   GetDEManu(*external,detElemId,manuId);
   
-  mpde = ddlStore->GetDetElement(detElemId);
+  mpde = ddlStore->GetDetElement(detElemId,kFALSE);
 
   if (!mpde) // can happen if reading e.g. capacitances store where we have data for non-connected manus
   {
     return -1;
   }
   
+  // explicitely check that de,manu is correct
+  const AliMpVSegmentation* mpseg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId, manuId,kFALSE);
+  
+  if (!mpseg)
+  {
+    return -1;
+  }
+  
   Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
     
   Int_t busPatchId = ddlStore->GetBusPatchId(detElemId,manuId);
   
+  if ( busPatchId <= 0 )
+  {
+    return -1;
+  }
+  
   Int_t pcbIndex = -1;
   
   AliMp::StationType stationType = mpde->GetStationType();
   
   if ( stationType == AliMp::kStation345 ) 
   {
-    AliMpHVNamer namer;
+    AliMpDCSNamer namer("TRACKER");
     pcbIndex = namer.ManuId2PCBIndex(detElemId,manuId);
   }
   
-  channel = ChannelParam(detElemId,manuId,external);
+  if ( fIsChannelLevelEnabled ) 
+  {
+    channel = ChannelParam(detElemId,manuId,external);
+  }
   
   manu = ManuParam(detElemId,manuId,kTRUE);
   
@@ -1231,7 +1817,7 @@ AliMUONTrackerData::CreateManuParam(Int_t detElemId, Int_t manuId) const
 {
   /// Create storage for one manu
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMUONVCalibParam* manu = new AliMUONCalibParamND(Dimension(),1,detElemId,manuId,0.0);
   
@@ -1260,7 +1846,7 @@ AliMUONTrackerData::Merge(TCollection* list)
   while ( ( o = next() ) )
   {
     const AliMUONTrackerData* data = dynamic_cast<const AliMUONTrackerData*>(o);
-    if (!o)
+    if (!data)
     {
       AliError(Form("Object named %s is not an AliMUONTrackerData ! Skipping it",
                     o->GetName()));
@@ -1344,9 +1930,9 @@ AliMUONTrackerData::CreatePCBParam(Int_t detElemId, Int_t pcbIndex) const
 {
   /// Create storage for one PCB (station345 only)
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
-  AliMpHVNamer namer;
+  AliMpDCSNamer namer("TRACKER");
   
   AliMUONVCalibParam* pcb = new AliMUONCalibParamND(Dimension(),
                                                     namer.NumberOfPCBs(detElemId),
@@ -1449,7 +2035,9 @@ AliMUONTrackerData::SetDimensionName(Int_t index, const char* name)
 
   if ( index >= fExternalDimension ) 
   {
-    AliError(Form("Index out of bounds : %d / %d",index,fExternalDimension));
+    AliError(Form("%s : dimension %s : Index out of bounds : %d / %d",
+                  GetName(),
+                  name,index,fExternalDimension));
     return;
   }
   
@@ -1485,6 +2073,8 @@ AliMUONTrackerData::MakeHistogramForDimension(Int_t index, Bool_t value, Double_
     return;
   }
   
+  AliWarning(Form("Will %s make histogram for data %s index %d : that might ressemble a memory leak depending on the input data",
+                  value ? "":"not", GetName(),index));
   fHistogramming[index] = value;
   fXmin = xmin;
   fXmax = xmax;
@@ -1615,4 +2205,83 @@ AliMUONTrackerData::Streamer(TBuffer &R__b)
   }
 }
 
+//_____________________________________________________________________________
+Bool_t 
+AliMUONTrackerData::ExportAsASCIIOccupancyFile(const char* filename, Int_t runNumber) const
+{
+  /// Export only the occupancy part, in a format compatible with what
+  /// the online occupancy DA is writing
+  
+  if ( ! AliMpDDLStore::Instance(kFALSE) )
+  {
+    AliError("Mapping not loaded. Cannot work");
+    return kFALSE;
+  }
+  
+  if (!fManuValues)
+  {
+    AliError("No manu values. Cannot work");
+    return kFALSE;
+  }
+  
+  ofstream out(filename);
+  
+  if (out.bad())
+  {
+    AliError(Form("Cannot create file %s",filename));
+    return kFALSE;
+  }
+  
+  out << "//===========================================================================" << endl;
+  out << "//  Hit counter exported from $Id$" << endl;
+  out << "//===========================================================================" << endl;
+  out << "//" << endl;
+  out << "//       * Run Number          : " << runNumber << endl;
+  out << "//       * File Creation Date  : " << TTimeStamp().AsString("l") << endl;
+  out << "//---------------------------------------------------------------------------" << endl;
+  out << "//  BP   MANU  SUM_N  NEVENTS" << endl;
+  out << "//---------------------------------------------------------------------------" << endl;
+  
+  TIter next(fManuValues->CreateIterator());
+  AliMUONVCalibParam* manu;
+  
+  while ( ( manu = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    Int_t detElemId = manu->ID0();
+    Int_t manuId = manu->ID1();
+    Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
+    Int_t ddl = AliMpDDLStore::Instance()->GetDDLfromBus( busPatchId);
+    if ( busPatchId < 0 || ddl < 0 )
+    {
+      AliError(Form("Got invalid (DE,manu,bp,ddl)=(%d,%d,%d,%d). Skipping it",detElemId,manuId,busPatchId,ddl));
+      continue;
+    }
 
+    Int_t nevents = fNofEventsPerDDL[ddl];
+    
+    out << Form("%5d %5d %10d %10d",busPatchId,manuId,manu->ValueAsInt(0,IndexOfOccupancyDimension()),nevents) << endl;
+  }
+  
+  out.close();
+  return kTRUE;
+}
+  
+//_____________________________________________________________________________
+void AliMUONTrackerData::DispatchValue(AliMUONVCalibParam& param, 
+                                       Int_t index,
+                                       Double_t y, 
+                                       Double_t ey,
+                                       Int_t nchannels)
+{
+  /// fills the calibparam with a single value
+  
+  Double_t sumn = 1000.0; // or any value strictly above 1
+  Double_t sumw = sumn*y;
+  Double_t sumw2 = (sumn-1)*ey*ey+sumw*sumw/sumn;
+  
+  param.SetValueAsDouble(index,0,sumw);
+  param.SetValueAsDouble(index,1,sumw2);
+  param.SetValueAsDouble(index,2,sumn);
+  param.SetValueAsDouble(index,3,nchannels);
+  
+}