]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrackerData.cxx
Updated AddTask for pPb
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerData.cxx
index dbf97858fde738e0269080c248d419f64a4b0be0..56bf930ac55713a281e4d44d8a6296e2febfc9c3 100644 (file)
 #include "AliMUONTrackerData.h"
 
 #include "AliCodeTimer.h"
+#include "AliDAQ.h"
 #include "AliLog.h"
 #include "AliMUON1DArray.h"
 #include "AliMUON1DMap.h"
 #include "AliMUON2DMap.h"
 #include "AliMUONCalibParamND.h"
+#include "AliMUONRejectList.h"
 #include "AliMUONSparseHisto.h"
 #include "AliMUONVStore.h"
 #include "AliMpBusPatch.h"
 #include "AliMpConstants.h"
+#include "AliMpCDB.h"
 #include "AliMpDDLStore.h"
+#include "AliMpManuStore.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 <TH1.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
@@ -48,6 +56,8 @@
 ///
 /// \author Laurent Aphecetche, Subatech
 
+using std::cout;
+using std::endl;
 ///\cond CLASSIMP
 ClassImp(AliMUONTrackerData)
 ///\endcond
@@ -58,25 +68,33 @@ const Int_t AliMUONTrackerData::fgkVirtualExtraDimension = 1;
 //_____________________________________________________________________________
 AliMUONTrackerData::AliMUONTrackerData(const char* name, const char* title,
                                        Int_t dimension,
-                                       Bool_t runnable)
+                                       Bool_t issingleevent)
 : AliMUONVTrackerData(name,title),
+fIsSingleEvent(issingleevent),
 fChannelValues(0x0),
 fManuValues(0x0),
 fBusPatchValues(0x0),
 fDEValues(0x0),
 fChamberValues(0x0),
 fPCBValues(0x0),
-fDimension(dimension*2+fgkExtraDimension),
-fNevents(0x0),
+fDimension(External2Internal(dimension)+fgkExtraDimension),
+fNevents(0),
 fDimensionNames(new TObjArray(fDimension+fgkVirtualExtraDimension)),
 fExternalDimensionNames(new TObjArray(dimension)),
 fExternalDimension(dimension),
-fIsRunnable(runnable),
 fHistogramming(new Int_t[fExternalDimension]),
-fChannelHistos(0x0)
+fHistos(0x0),
+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 SetHistogramDimension 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());
@@ -85,6 +103,524 @@ fChannelHistos(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()
 {
@@ -98,33 +634,353 @@ AliMUONTrackerData::~AliMUONTrackerData()
   delete fDimensionNames;
   delete fExternalDimensionNames;
   delete[] fHistogramming;
-  delete fChannelHistos;
+  delete fHistos;
+  delete[] fNofEventsPerDDL;
 }
 
 //_____________________________________________________________________________
 Bool_t
-AliMUONTrackerData::Add(const AliMUONVStore& store)
+AliMUONTrackerData::Add(const AliMUONVStore& store, TArrayI* nevents)
 {
   /// Add the given external store to our internal store
+  return InternalAdd(store,nevents,kFALSE);
+}
+
+//_____________________________________________________________________________
+Bool_t 
+AliMUONTrackerData::Add(const AliMUONTrackerData& data)
+{
+  /// Add data to *this
+  // We do this by looping on all VCalibParam stored in the various containers,
+  // and simply adding the values there.
+  // Same thing for the number of events per DDL.
+  // Same thing for sparsehistograms, if we have some.
+
+  // First cross check we have compatible objects.
+  
+  AliCodeTimerAuto("",0);
+  
+  if ( fIsChannelLevelEnabled != data.fIsChannelLevelEnabled ) 
+  {
+    AliError("Incompatible IsChannelLevelEnabled status");
+    return kFALSE;
+  }
+  
+  if ( fIsManuLevelEnabled != data.fIsManuLevelEnabled ) 
+  {
+    AliError("Incompatible IsManuLevelEnabled status");
+    return kFALSE;
+  }
+  
+  if ( fIsSingleEvent != data.fIsSingleEvent ) 
+  {
+    AliError("Incompatible IsSingleEvent status");
+    return kFALSE;
+  }
+  
+  if ( fDimension != data.fDimension || fExternalDimension != data.fExternalDimension ) 
+  {
+    AliError("Incompatible dimensions");
+    return kFALSE;
+  }
+
+  if ( fNofDDLs != data.fNofDDLs ) 
+  {
+    AliError("Incompatible number of Ddls");
+    return kFALSE;
+  }
+  
+  if ( ( !fHistogramming && data.fHistogramming ) || ( fHistogramming && !data.fHistogramming ) 
+      || fXmin != data.fXmin || fXmax != data.fXmax ) 
+  {
+    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;
+  }
+
+  if ( fHistogramming )
+  {
+    for ( Int_t i = 0; i < fExternalDimension; ++i ) 
+    {
+      if ( fHistogramming[i] != data.fHistogramming[i] )
+      {
+        AliError(Form("Incompatible histogramming for external dimension %d",i));
+        return kFALSE;
+      }
+    }
+  }
+  
+  // OK. Seems we have compatible objects, so we can proceed with the actual
+  // merging...
+  
+  if ( data.fChannelValues ) 
+  {
+    Add2D(*(data.fChannelValues),*fChannelValues);
+  }
+  
+  if ( data.fManuValues ) 
+  {
+    Add2D(*(data.fManuValues),*fManuValues);
+  }
+  
+  if ( data.fPCBValues ) 
+  {
+    Add2D(*(data.fPCBValues),*fPCBValues);
+  }
+  
+  if ( data.fBusPatchValues ) 
+  {
+    Add1D(*(data.fBusPatchValues),*fBusPatchValues);
+  }
+  
+  if ( data.fDEValues ) 
+  {
+    Add1D(*(data.fDEValues),*fDEValues);
+  }
+  
+  if ( data.fChamberValues ) 
+  {
+    Add1D(*(data.fChamberValues),*fChamberValues);
+  }
+
+  for ( Int_t i = 0; i < fNofDDLs; ++i ) 
+  {
+    fNofEventsPerDDL[i] += data.fNofEventsPerDDL[i];
+  }
+  
+  if ( data.fHistos ) 
+  {
+    TIter nexthisto(data.fHistos->CreateIterator());
+    AliMUONVStore* store;
+    while ( ( store = static_cast<AliMUONVStore*>(nexthisto()) ) )
+    {
+      TIter ns(store->CreateIterator());
+      AliMUONSparseHisto* h;
+      while ( ( h = static_cast<AliMUONSparseHisto*>(ns()) ) )
+      {
+        AliMUONVStore* thisStore = static_cast<AliMUONVStore*>(fHistos->FindObject(store->GetUniqueID()));
+        
+        if (!thisStore)
+        {
+          thisStore = store->Create();
+          thisStore->SetUniqueID(store->GetUniqueID());
+          fHistos->Add(thisStore);
+        }
+        
+        AliMUONSparseHisto* mine = static_cast<AliMUONSparseHisto*>(thisStore->FindObject(h->GetUniqueID()));
+        
+        if (!mine) 
+        {
+          thisStore->Add(h);
+        }
+        else
+        {
+          mine->Add(*h);
+        }
+      }
+    }
+  }
   
-  AliCodeTimerAuto(GetName());
+  for ( Int_t i = 0 ; i < fNofDDLs; ++i ) 
+  {
+    fNevents = TMath::Max(fNevents,fNofEventsPerDDL[i]);
+  }
+  
+  return kTRUE;
+}
+
+//_____________________________________________________________________________
+void 
+AliMUONTrackerData::Add2D(const AliMUONVStore& src, AliMUONVStore& dest) const
+{
+  /// Add one 2d store to another
+  
+  TIter next(src.CreateIterator());
+  AliMUONVCalibParam* p;
+  
+  while ( ( p = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    AliMUONVCalibParam* a = static_cast<AliMUONVCalibParam*>(dest.FindObject(p->ID0(),p->ID1()));
     
-  ++fNevents;
-  NumberOfEventsChanged();
+    if (!a)
+    {
+      dest.Add(static_cast<AliMUONVCalibParam*>(p->Clone()));
+    }
+    else
+    {
+      AddCalibParams(*p,*a);
+    }
+  }
+}
+
+//_____________________________________________________________________________
+void 
+AliMUONTrackerData::Add1D(const AliMUONVStore& src, AliMUONVStore& dest) const
+{
+  /// Add one 1d store to another
+  
+  TIter next(src.CreateIterator());
+  AliMUONVCalibParam* p;
+  
+  while ( ( p = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    AliMUONVCalibParam* a = static_cast<AliMUONVCalibParam*>(dest.FindObject(p->GetUniqueID()));
+    
+    if (!a)
+    {
+      dest.Add(static_cast<AliMUONVCalibParam*>(p->Clone()));
+    }
+    else
+    {
+      AddCalibParams(*p,*a);
+    }
+  }
+}
+
+//_____________________________________________________________________________
+void
+AliMUONTrackerData::AddCalibParams(const AliMUONVCalibParam& src, AliMUONVCalibParam& dest) const
+{
+  /// Add src to dest
+  for ( Int_t i = 0; i < src.Size(); ++i ) 
+  {
+    for ( Int_t j = 0; j < src.Dimension(); ++j )
+    {
+      dest.SetValueAsFloat(i,j,src.ValueAsFloat(i,j));
+    }
+  }
+}
+
+//_____________________________________________________________________________
+Bool_t
+AliMUONTrackerData::Replace(const AliMUONVStore& store)
+{
+  /// Replace our values by values from the given external store
+  Bool_t rv = InternalAdd(store,0x0,kTRUE);
+  AliMUONVTrackerData::Replace(store);
+  return rv;
+}
+
+//_____________________________________________________________________________
+Bool_t
+AliMUONTrackerData::UpdateNumberOfEvents(TArrayI* nevents)
+{
+  /// Update the number of events
+  
+  if (!fNofDDLs)
+  {
+    fNofDDLs = AliDAQ::NumberOfDdls("MUONTRK");
+    fNofEventsPerDDL = new Int_t[fNofDDLs];
+    for ( Int_t i = 0; i < fNofDDLs; ++i ) 
+    {
+      fNofEventsPerDDL[i] = 0;
+    }
+  }
   
-  if (!fChannelValues)
+  if (nevents)
+  {
+    if (nevents->GetSize() != fNofDDLs ) 
+    {
+      AliError(Form("nof of ddl per event array size is incorrect : got %d, expecting %d",
+                    nevents->GetSize(),fNofDDLs));
+      return kFALSE;
+    }
+    
+    for ( Int_t i = 0 ; i < fNofDDLs; ++i ) 
+    {
+      fNofEventsPerDDL[i] += nevents->At(i);
+      fNevents = TMath::Max(fNevents,fNofEventsPerDDL[i]);
+    }
+  }
+  else
   {
-    fChannelValues = store.Create();
-    fManuValues = store.Create();
-    fPCBValues = store.Create();
-    fBusPatchValues = new AliMUON1DMap;
-    fDEValues = new AliMUON1DMap;
+    for ( Int_t i = 0 ; i < fNofDDLs; ++i ) 
+    {
+      ++fNofEventsPerDDL[i];
+      fNevents = TMath::Max(fNevents,fNofEventsPerDDL[i]);
+    }
+  }
+  return kTRUE;
+}
+
+//_____________________________________________________________________________
+void
+AliMUONTrackerData::AssertStores()
+{
+  /// Insure our stores are allocated
+  
+  if (!fChamberValues)
+  {
+    Int_t numberOfBusPatches(0);
+    Int_t numberOfDEs(0);
+    
+    // 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;
+    deIt.First();
+    while (!deIt.IsDone())
+    {
+      ++numberOfDEs;
+      deIt.Next();
+    }
+    
+    if ( fIsChannelLevelEnabled ) 
+    {
+      fChannelValues = new AliMUON2DMap(kTRUE);
+    }
+    if  ( fIsManuLevelEnabled ) 
+    {
+      fManuValues = new AliMUON2DMap(kTRUE);
+    }
+    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;
   
+  Int_t nk(2);
+  
+  if ( IsSingleEvent() ) nk = 1;
+
   while ( ( external = static_cast<AliMUONVCalibParam*>(next()) ) )
   {
     if ( external->Dimension() != ExternalDimension() )
@@ -134,12 +990,18 @@ AliMUONTrackerData::Add(const AliMUONVStore& store)
       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);
     
+    if ( manuId < 0 ) continue;
+    
     Int_t detElemId = mpde->GetId();
     
     Double_t value[] = { 0.0, 0.0 };
@@ -174,34 +1036,50 @@ AliMUONTrackerData::Add(const AliMUONVStore& store)
           
           if ( IsHistogrammed(j) )
           {
-            FillChannel(detElemId,manuId,i,j,vext);
+            FillHisto(detElemId,manuId,i,j,vext);
           }
           
-          for ( Int_t k = 0; k < 2; ++k ) 
+          for ( Int_t k = 0; k < nk; ++k ) 
           {
-            channel->SetValueAsDoubleFast(i,ix+k,channel->ValueAsDoubleFast(i,ix+k)+value[k]);
-
-            manu->SetValueAsDoubleFast(0,ix+k,manu->ValueAsDoubleFast(0,ix+k)+value[k]);            
+            Double_t e = ( replace && channel ) ? channel->ValueAsDoubleFast(i,ix+k) : 0.0;
+            
+                                               if ( channel ) 
+                                               {
+                                                       channel->SetValueAsDoubleFast(i,ix+k,channel->ValueAsDoubleFast(i,ix+k)-e+value[k]);
+                                               }
+                                               
+            if (manu)
+            {
+              manu->SetValueAsDoubleFast(0,ix+k,manu->ValueAsDoubleFast(0,ix+k)-e+value[k]);            
+            }
+            
+            busPatch->SetValueAsDoubleFast(0,ix+k,busPatch->ValueAsDoubleFast(0,ix+k)-e+value[k]);
+            
+            de->SetValueAsDoubleFast(0,ix+k,de->ValueAsDoubleFast(0,ix+k)-e+value[k]);
+            
+            chamber->SetValueAsDoubleFast(0,ix+k,chamber->ValueAsDoubleFast(0,ix+k)-e+value[k]);
             
-            busPatch->SetValueAsDoubleFast(0,ix+k,busPatch->ValueAsDoubleFast(0,ix+k)+value[k]);
-          
-            de->SetValueAsDoubleFast(0,ix+k,de->ValueAsDoubleFast(0,ix+k)+value[k]);
-          
-            chamber->SetValueAsDoubleFast(0,ix+k,chamber->ValueAsDoubleFast(0,ix+k)+value[k]);
-          
             if ( pcb ) 
             {
-              pcb->SetValueAsDoubleFast(0,ix+k,pcb->ValueAsDoubleFast(0,ix+k)+value[k]);
+              pcb->SetValueAsDoubleFast(0,ix+k,pcb->ValueAsDoubleFast(0,ix+k)-e+value[k]);
             }
           }
         }
         
-        if ( validChannel )
+        if ( validChannel && !replace )
         {
-          channel->SetValueAsDoubleFast(i,IndexOfOccupancyDimension(),
-                                        channel->ValueAsDoubleFast(i,IndexOfOccupancyDimension())+1.0);
-          manu->SetValueAsDoubleFast(0,IndexOfOccupancyDimension(),
-                                                 manu->ValueAsDoubleFast(0,IndexOfOccupancyDimension())+1.0);        
+                                       if ( channel ) 
+                                       {
+                                               channel->SetValueAsDoubleFast(i,IndexOfOccupancyDimension(),
+                                                                                                                                                                       channel->ValueAsDoubleFast(i,IndexOfOccupancyDimension())+1.0);
+                                       }
+                                       
+          if (manu)
+          {
+            manu->SetValueAsDoubleFast(0,IndexOfOccupancyDimension(),
+                                       manu->ValueAsDoubleFast(0,IndexOfOccupancyDimension())+1.0);        
+          }
+          
           busPatch->SetValueAsDoubleFast(0,IndexOfOccupancyDimension(),
                                                          busPatch->ValueAsDoubleFast(0,IndexOfOccupancyDimension())+1.0);        
           de->SetValueAsDoubleFast(0,IndexOfOccupancyDimension(),
@@ -218,6 +1096,8 @@ AliMUONTrackerData::Add(const AliMUONVStore& store)
     }
   }
   
+  NumberOfEventsChanged();
+  
   return kTRUE;
 }
 
@@ -228,7 +1108,7 @@ AliMUONTrackerData::BusPatch(Int_t busPatchId, Int_t dim) const
   /// Return the value of a given buspatch for a given dimension
   /// or 0 if not existing
   AliMUONVCalibParam* param = BusPatchParam(busPatchId);
-  return param ? Value(*param,0,dim) : 0.0;
+  return param ? Value(*param,0,dim,DdlIdFromBusPatchId(busPatchId)) : 0.0;
 }
 
 //_____________________________________________________________________________
@@ -255,7 +1135,7 @@ AliMUONTrackerData::CreateBusPatchParam(Int_t busPatchId) const
 {
   /// Create storage for one bus patch
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMpBusPatch* bp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
   
@@ -292,8 +1172,13 @@ AliMUONTrackerData::Chamber(Int_t chamberId, Int_t dim) const
 {
   /// Return the value fo a given chamber for a given dimension,
   /// or zero if not existing
+  
+  // FIXME: is the Value() correct wrt to number of events in the case of
+  // chamber ? Or should we do something custom at the chamber level 
+  // (as it spans several ddls) ?
+  
   AliMUONVCalibParam* param = ChamberParam(chamberId);
-  return param ? Value(*param,0,dim) : 0.0;
+  return param ? Value(*param,0,dim,DdlIdFromChamberId(chamberId)) : 0.0;
 }
 
 //_____________________________________________________________________________
@@ -320,7 +1205,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);
   
@@ -364,13 +1249,13 @@ AliMUONTrackerData::Channel(Int_t detElemId, Int_t manuId,
   
   AliMUONVCalibParam* param = ChannelParam(detElemId,manuId);
   
-  return param ? Value(*param,manuChannel,dim) : 0.0;
+  return param ? Value(*param,manuChannel,dim,DdlIdFromDetElemId(detElemId)) : 0.0;
 }
 
 //_____________________________________________________________________________
 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
   
@@ -379,7 +1264,7 @@ AliMUONTrackerData::ChannelParam(Int_t detElemId, Int_t manuId,
   
   if (!param && external && fChannelValues)
   {
-    param = CreateDouble(*external);
+    param = CreateDouble(*external,detElemId,manuId);
     fChannelValues->Add(param);
   }
   
@@ -397,7 +1282,11 @@ AliMUONTrackerData::Clear(Option_t*)
   if ( fPCBValues ) fPCBValues->Clear();
   if ( fDEValues) fDEValues->Clear();
   if ( fChamberValues ) fChamberValues->Clear();
-  if ( fChannelHistos ) fChannelHistos->Clear();
+  if ( fHistos ) fHistos->Clear();
+  for ( Int_t i = 0; i < fNofDDLs; ++i ) 
+  {
+    fNofEventsPerDDL[i] = 0;
+  }
   fNevents = 0;
   NumberOfEventsChanged();
 }
@@ -414,21 +1303,28 @@ AliMUONTrackerData::Count(Int_t detElemId, Int_t manuId,
 
 //_____________________________________________________________________________
 AliMUONVCalibParam*
-AliMUONTrackerData::CreateDouble(const AliMUONVCalibParam& param) const
+AliMUONTrackerData::CreateDouble(const AliMUONVCalibParam& param, 
+                                 Int_t detElemId, Int_t manuId) const
 {
   /// Create a double version of VCalibParam, for internal use
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMUONVCalibParam* c = new AliMUONCalibParamND(Dimension(),
                                                   param.Size(),
-                                                  param.ID0(),
-                                                  param.ID1(),
+                                                  detElemId,
+                                                  manuId,
                                                   0.0);
   
+  AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId,manuId);
+  
   for ( Int_t i = 0; i < c->Size(); ++i ) 
   {
-    c->SetValueAsDouble(i,IndexOfNumberDimension(),1.0);
+    Double_t value(0.0);
+    
+    if ( de->IsConnectedChannel(manuId,i) ) value = 1.0;
+      
+    c->SetValueAsDouble(i,IndexOfNumberDimension(),value);
   }
   
   return c;
@@ -440,7 +1336,7 @@ AliMUONTrackerData::DetectionElement(Int_t detElemId, Int_t dim) const
 {
   /// Return the value for a given detection element for a given dimension
   AliMUONVCalibParam* param = DetectionElementParam(detElemId);
-  return param ? Value(*param,0,dim) : 0.0;
+  return param ? Value(*param,0,dim,DdlIdFromDetElemId(detElemId)) : 0.0;
 
 }
 
@@ -464,32 +1360,87 @@ AliMUONTrackerData::DetectionElementParam(Int_t detElemId, Bool_t create) const
 }
 
 //_____________________________________________________________________________
-AliMUONVCalibParam* 
-AliMUONTrackerData::CreateDetectionElementParam(Int_t detElemId) const
+AliMUONVCalibParam* 
+AliMUONTrackerData::CreateDetectionElementParam(Int_t detElemId) const
+{
+  /// Create storage for one detection element
+  
+  AliCodeTimerAuto("",0);
+  
+  AliMUONVCalibParam*  de = new AliMUONCalibParamND(Dimension(),1,detElemId,0,0.0);
+  
+  AliMpDetElement* det = AliMpDDLStore::Instance()->GetDetElement(detElemId);
+  Int_t nchannels(0);
+  
+  for ( Int_t i = 0; i < det->GetNofBusPatches(); ++i ) 
+  {
+    Int_t busPatchId = det->GetBusPatchId(i);
+    AliMpBusPatch* bp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
+    for ( Int_t j = 0; j < bp->GetNofManus(); ++j ) 
+    {
+      Int_t manuId = bp->GetManuId(j);
+      nchannels += det->NofChannelsInManu(manuId);
+    }        
+  }
+  
+  de->SetValueAsDouble(0,IndexOfNumberDimension(),nchannels);
+  
+  return de;
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerData::DdlIdFromBusPatchId(Int_t buspatchid) const
+{
+  /// Get the "local" ddlid (0..19) of a given buspatch
+  AliMpBusPatch* bp = AliMpDDLStore::Instance()->GetBusPatch(buspatchid);
+  if (bp)
+  {
+    return bp->GetDdlId();
+  }
+  return -1;
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerData::DdlIdFromDetElemId(Int_t detelemid) const
+{
+  /// Get the "local" ddlid (0..19) of a given detection element
+  AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detelemid);
+  if (de)
+  {
+    return de->GetDdlId();
+  }
+  return -1;
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerData::DdlIdFromChamberId(Int_t chamberid) const
 {
-  /// Create storage for one detection element
+  /// Get the "local" ddlid (0..19) of a given chamber
+  /// This has no real meaning (as there are several ddls per chamber),
+  /// so we take the ddlid where we got the max number of events
   
-  AliCodeTimerAuto("");
-  
-  AliMUONVCalibParam*  de = new AliMUONCalibParamND(Dimension(),1,detElemId,0,0.0);
+  AliMpDEIterator it;
   
-  AliMpDetElement* det = AliMpDDLStore::Instance()->GetDetElement(detElemId);
-  Int_t nchannels(0);
+  it.First(chamberid);
+  Int_t n(0);
+  Int_t d(-1);
   
-  for ( Int_t i = 0; i < det->GetNofBusPatches(); ++i ) 
+  while (!it.IsDone())
   {
-    Int_t busPatchId = det->GetBusPatchId(i);
-    AliMpBusPatch* bp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
-    for ( Int_t j = 0; j < bp->GetNofManus(); ++j ) 
+    Int_t detElemId = it.CurrentDEId();
+    Int_t ddlId = DdlIdFromDetElemId(detElemId);
+    if ( NumberOfEvents(ddlId) > n ) 
     {
-      Int_t manuId = bp->GetManuId(j);
-      nchannels += det->NofChannelsInManu(manuId);
-    }        
+      n = NumberOfEvents(ddlId);
+      d = ddlId;
+    }
+    it.Next();
   }
   
-  de->SetValueAsDouble(0,IndexOfNumberDimension(),nchannels);
-  
-  return de;
+  return d;
 }
 
 //_____________________________________________________________________________
@@ -508,6 +1459,37 @@ AliMUONTrackerData::DimensionName(Int_t dim) const
   }  
 }
 
+//_____________________________________________________________________________
+void 
+AliMUONTrackerData::DisableChannelLevel()
+{ 
+  /// Disable the storing of data at channel level
+  
+  delete fChannelValues;
+  fChannelValues = 0x0;
+  fIsChannelLevelEnabled = kFALSE; 
+}
+
+//_____________________________________________________________________________
+void 
+AliMUONTrackerData::DisableManuLevel()
+{ 
+  /// Disable the storing of data at manu level (and below)
+  
+  DisableChannelLevel();
+  delete fManuValues;
+  fManuValues = 0x0;
+  fIsManuLevelEnabled = kFALSE; 
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerData::External2Internal(Int_t index) const 
+{
+  /// From external to internal dimension
+  return IsSingleEvent() ? index : index*2;
+}
+
 //_____________________________________________________________________________
 TString 
 AliMUONTrackerData::ExternalDimensionName(Int_t dim) const
@@ -527,294 +1509,162 @@ AliMUONTrackerData::ExternalDimensionName(Int_t dim) const
 
 //_____________________________________________________________________________
 void
-AliMUONTrackerData::FillChannel(Int_t detElemId, Int_t manuId, Int_t manuChannel,
-                                Int_t dim, Double_t value)
+AliMUONTrackerData::FillHisto(Int_t detElemId, Int_t manuId, Int_t manuChannel,
+                              Int_t dim, Double_t value)
 {
   /// Fill histogram of a given channel
   
-  AliMUONSparseHisto* h = GetChannelHisto(detElemId, manuId, manuChannel,dim);
-  h->Fill(static_cast<Int_t>(TMath::Nint(value)));
-}
-
-//_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreateChannelHisto(Int_t detElemId, Int_t manuId, 
-                                       Int_t manuChannel, Int_t dim)
-{
-  /// Create histogram of a given channel. Note that in order
-  /// to keep memory footprint as low as possible, you should delete
-  /// the returned pointer as soon as possible...
-
-  if ( HasChannel(detElemId, manuId, manuChannel) && IsHistogrammed(dim) )
-  {
-    AliMUONSparseHisto* sh = GetChannelHisto(detElemId,manuId,manuChannel,dim);
+  AliMUONSparseHisto* h(0x0);
   
-    if ( sh ) 
-    {
-      TH1* h = new TH1I(Form("DE%04dMANU%04dCH%02d_%d",detElemId,manuId,manuChannel,dim),
-                        Form("Data=%s Dim=%s",GetName(),ExternalDimensionName(dim).Data()),
-                        4096,-0.5,4095.5);
-      
-      Add(*h,*sh);
-      return h;
-    }
+       if ( fIsChannelLevelEnabled ) 
+       {
+               h = GetChannelSparseHisto(detElemId, manuId, manuChannel,dim);
   }
-  return 0x0;
-}
-
-//_____________________________________________________________________________
-void
-AliMUONTrackerData::Add(TH1& h, const AliMUONSparseHisto& sh)
-{
-  /// Add sparse histo content to histogram.
-  
-  Double_t entries(h.GetEntries());
-  
-  for ( Int_t i = 0; i < sh.GetNbins(); ++i ) 
+  else if ( fIsManuLevelEnabled ) 
   {
-    Int_t x = sh.GetBinContent(i);
-    Int_t adc, count;
-    sh.Decode(x,adc,count);
-    h.Fill(adc,count);
-    entries += count;
+    h = GetManuSparseHisto(detElemId,manuId,dim);
   }
   
-  h.SetEntries(entries);
-}
-
-//_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreateHisto(const char* name, Int_t dim) const
-{
-  /// Create a single histogram
-  return new TH1I(name,Form("Data=%s Dim=%s",GetName(),ExternalDimensionName(dim).Data()),
-                  4096,-0.5,4095.5);
+  AliDebug(1,Form("DE %04d MANU %04d CH %02d dim %d value %e h %p",detElemId,manuId,manuChannel,dim,value,h));
+  
+  if (h)
+  {
+               h->Fill(static_cast<Int_t>(TMath::Nint(value)));
+       }
 }
 
 //_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreateBusPatchHisto(Int_t busPatchId, Int_t dim)
+AliMUONSparseHisto*
+AliMUONTrackerData::GetManuSparseHisto(Int_t detElemId, Int_t manuId, 
+                                       Int_t dim) const
 {
-  /// Create histogram of a given bus patch. Note that in order
-  /// to keep memory footprint as low as possible, you should delete
-  /// the returned pointer as soon as possible...
+  /// Get histogram of a given manu
   
-  TH1* h(0x0);
+  if (!fHistos) return 0x0;
   
-  if ( HasBusPatch(busPatchId) && IsHistogrammed(dim)) 
-  {
-    h = CreateHisto(Form("BP%04d_%d",busPatchId,dim),dim);
-    AddBusPatchHisto(*h,busPatchId,dim);
-  }
+  AliMUON1DArray* m = static_cast<AliMUON1DArray*>(fHistos->FindObject(detElemId,manuId));
+  if (!m) return 0x0;
+  
+  AliMUONSparseHisto* h = static_cast<AliMUONSparseHisto*>(m->FindObject(dim));
   
   return h;
-}  
-//_____________________________________________________________________________
-void
-AliMUONTrackerData::AddBusPatchHisto(TH1& h, Int_t busPatchId, Int_t dim)
-{
-  /// Add data from one bus patch to the histogram
-      
-  if ( HasBusPatch(busPatchId ) )
-  {
-    AliMpBusPatch* busPatch = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
-    for ( Int_t i = 0; i < busPatch->GetNofManus(); ++i ) 
-    {
-      Int_t manuId = busPatch->GetManuId(i);
-      AddManuHisto(h,busPatch->GetDEId(),manuId,dim);
-    }
-  }
 }
 
-
 //_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreateDEHisto(Int_t detElemId, Int_t dim)
+AliMUONSparseHisto*
+AliMUONTrackerData::GetManuSparseHisto(Int_t detElemId, Int_t manuId, Int_t dim)
 {
-  /// Create histogram of a given detection element. Note that in order
-  /// to keep memory footprint as low as possible, you should delete
-  /// the returned pointer as soon as possible...
+  /// Get histogram of a given manu. Create it if necessary
   
-  TH1* h(0x0);
+  if (!fHistos) fHistos = new AliMUON2DMap(kTRUE);
   
-  if ( HasDetectionElement(detElemId) && IsHistogrammed(dim) ) 
+  AliMUON1DArray* m = static_cast<AliMUON1DArray*>(fHistos->FindObject(detElemId,manuId));
+  if (!m)
   {
-    h = CreateHisto(Form("DE%04d-%d",detElemId,dim),dim);
-    AddDEHisto(*h,detElemId,dim);
+    m = new AliMUON1DArray(NumberOfDimensions());
+    m->SetUniqueID( ( manuId << 16 ) | detElemId );
+    fHistos->Add(m);
   }
-  
-  return h;
-}
-
-//_____________________________________________________________________________
-void
-AliMUONTrackerData::AddDEHisto(TH1& h, Int_t detElemId, Int_t dim)
-{
-  /// Add data from one detection element to the histogram
-  
-  if ( HasDetectionElement(detElemId) )
+    
+  AliMUONSparseHisto* h = static_cast<AliMUONSparseHisto*>(m->FindObject(dim));
+  if (!h)
   {
-    AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
-    for ( Int_t i = 0; i < de->GetNofBusPatches(); ++ i ) 
-    {
-      Int_t busPatchId = de->GetBusPatchId(i);
-      AddBusPatchHisto(h,busPatchId,dim);
-    }
+    h = new AliMUONSparseHisto(fXmin,fXmax);
+    
+    h->SetUniqueID(dim);
+    
+    m->Add(h);
   }
+  
+   return h;
 }
 
 //_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreateManuHisto(Int_t detElemId, Int_t manuId, Int_t dim)
+AliMUONSparseHisto*
+AliMUONTrackerData::GetChannelSparseHisto(Int_t detElemId, Int_t manuId, 
+                                          Int_t manuChannel, Int_t dim) const
 {
-  /// Create histogram of a given manu. Note that in order
-  /// to keep memory footprint as low as possible, you should delete
-  /// the returned pointer as soon as possible...
+  /// Get histogram of a given channel
   
-  TH1* h(0x0);
+  if (!fHistos) return 0x0;
   
-  if ( HasManu(detElemId, manuId) && IsHistogrammed(dim) ) 
-  {
-    h = CreateHisto(Form("DE%04dMANU%04d_%d",detElemId,manuId,dim),dim);
-    AddManuHisto(*h,detElemId,manuId,dim);
-  }
+  AliMUON1DMap* m = static_cast<AliMUON1DMap*>(fHistos->FindObject(detElemId,manuId));
+  if (!m) return 0x0;
+  
+  UInt_t uid = ( manuChannel << 16 ) | dim;
+  
+  AliMUONSparseHisto* h = static_cast<AliMUONSparseHisto*>(m->FindObject(uid));
   
   return h;
 }
 
 //_____________________________________________________________________________
-void
-AliMUONTrackerData::AddManuHisto(TH1& h, Int_t detElemId, Int_t manuId, Int_t dim)
+AliMUONSparseHisto*
+AliMUONTrackerData::GetChannelSparseHisto(Int_t detElemId, Int_t manuId, 
+                                          Int_t manuChannel, Int_t dim)
 {
-  /// Add data from a given manu to histogram
+  /// Get histogram of a given channel. Create it if necessary
   
-  if ( HasManu(detElemId,manuId) )
+  if (!fHistos) fHistos = new AliMUON2DMap(kTRUE);
+  
+  AliMUON1DMap* m = static_cast<AliMUON1DMap*>(fHistos->FindObject(detElemId,manuId));
+  if (!m)
   {
-    for ( Int_t i = 0; i < AliMpConstants::ManuNofChannels(); ++i ) 
-    {
-      if ( HasChannel(detElemId,manuId,i) )
-      {
-        AliMUONSparseHisto* sh = GetChannelHisto(detElemId,manuId,i,dim);
-      
-        if ( sh ) 
-        {      
-          Add(h,*sh);
-        }
-      }
-    }
+    m = new AliMUON1DMap(AliMpConstants::ManuNofChannels()); // start with only 1 dim
+    m->SetUniqueID( ( manuId << 16 ) | detElemId );
+    fHistos->Add(m);
   }
-}
-
-//_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreatePCBHisto(Int_t /*detElemId*/, Int_t /*pcbIndex*/, Int_t /*dim*/)
-{
-  /// Create histogram of a given PCB. Note that in order
-  /// to keep memory footprint as low as possible, you should delete
-  /// the returned pointer as soon as possible...
-  
- // TH1* h(0x0);
-//  
-//  if ( HasPCB(detElemId, pcbIndex) && IsHistogrammed(dim)) 
-//  {
-//    h = CreateHisto(Form("DE%04dPCB1d_%d",detElemId,pcbIndex,dim),dim);
-//  }
-//  
-//  return h;
-
-  AliWarning("Not implemented (is it needed ?)");
-  return 0x0;
-}
-
-//_____________________________________________________________________________
-TH1*
-AliMUONTrackerData::CreateChamberHisto(Int_t chamberId, Int_t dim)
-{
-  /// Create histogram of a given chamber. Note that in order
-  /// to keep memory footprint as low as possible, you should delete
-  /// the returned pointer as soon as possible...
   
-  TH1* h(0x0);
+  UInt_t uid = ( manuChannel << 16 ) | dim;
   
-  if ( HasChamber(chamberId) && IsHistogrammed(dim))
+  AliMUONSparseHisto* h = static_cast<AliMUONSparseHisto*>(m->FindObject(uid));
+  if (!h)
   {
-    h = CreateHisto(Form("CHAMBER%02d_%d",chamberId,dim),dim);
-    AliMpDEIterator it;
-    it.First(chamberId);
-    while ( !it.IsDone() )
-    {
-      Int_t detElemId = it.CurrentDEId();
-      AddDEHisto(*h,detElemId,dim);
-      it.Next();
-    }
+    h = new AliMUONSparseHisto(fXmin,fXmax);
+    
+    h->SetUniqueID(uid);
+    
+    m->Add(h);
   }
-  
+
   return h;
 }
 
 //_____________________________________________________________________________
-AliMUONSparseHisto*
-AliMUONTrackerData::GetChannelHisto(Int_t detElemId, Int_t manuId, 
-                                    Int_t manuChannel, Int_t dim)
+void
+AliMUONTrackerData::GetDEManu(const AliMUONVCalibParam& param,
+                              Int_t& detElemId, Int_t& manuId) const
 {
-  /// Get histogram of a given channel
-  
-  if (!fChannelHistos) fChannelHistos = new AliMUON2DMap(kTRUE);
+  /// Tries to get (detElemId,manuId) of param
   
-  TObjArray* dimArray = static_cast<TObjArray*>(fChannelHistos->FindObject(detElemId,manuId));
-  if (!dimArray)
-  {
-    dimArray = new TObjArray(fExternalDimension);
-    dimArray->SetUniqueID( ( manuId << 16 ) | detElemId );
-    fChannelHistos->Add(dimArray);
+  // Load mapping manu store
+  if ( ! AliMpCDB::LoadManuStore() ) {
+    AliError("Could not access manu store from OCDB !");
+    return;
   }
-  
-  TObjArray* channels = static_cast<TObjArray*>(dimArray->UncheckedAt(dim));
-  if (!channels)
+
+  if ( param.ID1() <= 0 ) 
   {
-    channels = new TObjArray(AliMpConstants::ManuNofChannels());
-    dimArray->AddAt(channels,dim);
+    // we (probably) get a manu serial number
+    Int_t serial = param.ID0();
+    MpPair_t pair = AliMpManuStore::Instance()->GetDetElemIdManu(serial);
+    detElemId = AliMp::PairFirst(pair);
+    manuId = AliMp::PairSecond(pair);
+    if ( !detElemId ) 
+    {
+      AliDebug(1,Form("DE %d manuId %d from serial %d is not correct !",
+                      detElemId,manuId,serial));
+    }
   }
-  
-  AliMUONSparseHisto* h = static_cast<AliMUONSparseHisto*>(channels->UncheckedAt(manuChannel));
-  if (!h)
+  else
   {
-    h = new AliMUONSparseHisto;
-    h->SetUniqueID(( manuChannel << 16 ) | dim);
-    channels->AddAt(h,manuChannel);
+    // we get a (de,manu) pair
+    detElemId = param.ID0();
+    manuId = param.ID1();
   }
-  
-  return h;
-// below is an alternate implementation, using a 1DMap, which *seems* to be
-// slightly SLOWER.
-//
-//  AliMUON1DMap* m = static_cast<AliMUON1DMap*>(fChannelHistos->FindObject(detElemId,manuId));
-//  if (!m)
-//  {
-//    m = new AliMUON1DMap(kFALSE);
-//    m->SetUniqueID( ( manuId << 16 ) | detElemId );
-//    fChannelHistos->Add(m);
-//  }
-//  
-//  UInt_t uid = ( manuChannel << 16 ) | dim;
-//  
-//  AliMUONSparseHisto* h = static_cast<AliMUONSparseHisto*>(m->FindObject(uid));
-//  if (!h)
-//  {
-//    h = new AliMUONSparseHisto;
-//    
-//    h->SetUniqueID(uid);
-//    
-//    m->Add(h);
-//  }
-
-  return h;
 }
 
+
 //_____________________________________________________________________________
 Int_t
 AliMUONTrackerData::GetParts(AliMUONVCalibParam* external,
@@ -828,29 +1678,54 @@ 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 = external->ID0();
+  Int_t detElemId;
+  Int_t manuId;
   
-  Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
+  GetDEManu(*external,detElemId,manuId);
+  
+  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;
+  }
   
-  Int_t manuId = external->ID1();
+  // explicitely check that de,manu is correct
+  const AliMpVSegmentation* mpseg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId, manuId,kFALSE);
   
-  mpde = ddlStore->GetDetElement(detElemId);
+  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);
   
@@ -917,7 +1792,7 @@ AliMUONTrackerData::Manu(Int_t detElemId, Int_t manuId, Int_t dim) const
   /// Return the value for a given manu and a given dimension
   
   AliMUONVCalibParam* param = ManuParam(detElemId,manuId);
-  return param ? Value(*param,0,dim) : 0.0;
+  return param ? Value(*param,0,dim,DdlIdFromDetElemId(detElemId)) : 0.0;
 }
 
 //_____________________________________________________________________________
@@ -944,7 +1819,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);
   
@@ -957,6 +1832,40 @@ AliMUONTrackerData::CreateManuParam(Int_t detElemId, Int_t manuId) const
   return manu;
 }
 
+//_____________________________________________________________________________
+Long64_t
+AliMUONTrackerData::Merge(TCollection* list)
+{
+  /// Merge this with a list of AliMUONVTrackerData
+
+  if (!list) return 0;
+  
+  if ( list->IsEmpty() ) return NumberOfEvents(-1);
+  
+  TIter next(list);
+  const TObject* o(0x0);
+  
+  while ( ( o = next() ) )
+  {
+    const AliMUONTrackerData* data = dynamic_cast<const AliMUONTrackerData*>(o);
+    if (!data)
+    {
+      AliError(Form("Object named %s is not an AliMUONTrackerData ! Skipping it",
+                    o->GetName()));
+    }
+    else
+    {
+      Bool_t ok = Add(*data);
+      if (!ok)
+      {
+        AliError("Got incompatible objects");
+      }
+    }
+  }
+  
+  return NumberOfEvents(-1);
+}
+
 //_____________________________________________________________________________
 Int_t 
 AliMUONTrackerData::NumberOfDimensions() const
@@ -966,6 +1875,28 @@ AliMUONTrackerData::NumberOfDimensions() const
   return fDimension + fgkVirtualExtraDimension; 
 }
 
+//_____________________________________________________________________________
+Int_t
+AliMUONTrackerData::NumberOfEvents(Int_t ddlNumber) const
+{
+  /// Get the number of events we've seen for a given DDL, or the max
+  /// in case ddlNumber<0
+
+  Int_t n(0);
+  
+  if ( fNofEventsPerDDL && ddlNumber >= 0 && ddlNumber < fNofDDLs )
+  {
+    n = fNofEventsPerDDL[ddlNumber];
+  }
+  else
+  {
+    // get the max number of events
+    return fNevents;
+  }
+  
+  return n;
+}
+
 //_____________________________________________________________________________
 Double_t 
 AliMUONTrackerData::PCB(Int_t detElemId, Int_t pcbIndex, Int_t dim) const
@@ -974,7 +1905,7 @@ AliMUONTrackerData::PCB(Int_t detElemId, Int_t pcbIndex, Int_t dim) const
 
   AliMUONVCalibParam* param = PCBParam(detElemId,pcbIndex);
   
-  return param ? Value(*param,pcbIndex,dim) : 0.0;
+  return param ? Value(*param,0,dim,DdlIdFromDetElemId(detElemId)) : 0.0;
 }
 
 //_____________________________________________________________________________
@@ -1001,9 +1932,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),
@@ -1021,11 +1952,24 @@ AliMUONTrackerData::Print(Option_t* wildcard, Option_t* opt) const
   
   TNamed::Print(opt);
   
-  if ( fIsRunnable ) 
+  if ( !fIsSingleEvent ) 
   {
-    cout << " Nevents=" << fNevents << endl;
+    for ( Int_t i = 0; i < fNofDDLs; ++i ) 
+    {
+      cout << Form("DDL %04d Nevents=%10d",AliDAQ::DdlID("MUONTRK",i),fNofEventsPerDDL[i]) << endl;
+    }
   }
 
+       if ( !fIsChannelLevelEnabled ) 
+       {
+               cout << "Is not storing data at the channel level" << endl;
+       }
+
+  if ( !fIsManuLevelEnabled ) 
+       {
+               cout << "Is not storing data at the manu level" << endl;
+       }
+  
   for ( Int_t i = 0; i <= fExternalDimensionNames->GetLast(); ++i ) 
   {
     TObjString* name = static_cast<TObjString*>(fExternalDimensionNames->At(i));
@@ -1044,14 +1988,28 @@ AliMUONTrackerData::Print(Option_t* wildcard, Option_t* opt) const
   TString sopt(opt);
   sopt.ToUpper();
   
-  if ( sopt.Contains("CHANNEL") && fChannelValues ) 
+  if ( sopt.Contains("CHANNEL") )
   {
-    fChannelValues->Print(wildcard,opt);
+    if ( fIsChannelLevelEnabled ) 
+    {      
+      if ( fChannelValues ) fChannelValues->Print(wildcard,opt);
+    }
+    else
+    {
+      AliWarning("You requested channel values, but they were not stored !");
+    }
   }
 
-  if ( sopt.Contains("MANU") && fManuValues ) 
+  if ( sopt.Contains("MANU") )
   {
-    fManuValues->Print(wildcard,opt);
+    if ( fIsManuLevelEnabled ) 
+    {
+      if ( fManuValues ) fManuValues->Print(wildcard,opt);
+    }
+    else
+    {
+      AliWarning("You requested manu values, but they were not stored !");
+    }
   }
 
   if ( sopt.Contains("BUSPATCH") && fBusPatchValues ) 
@@ -1079,19 +2037,28 @@ 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;
   }
   
   Int_t ix = External2Internal(index);
   
-  const char* prefix[] = { "mean", "sigma" };
-  
-  for ( Int_t i = 0; i < 2; ++i ) 
+  if ( !IsSingleEvent() ) 
   {
-    Int_t j = ix+i;
+    const char* prefix[] = { "mean", "sigma" };
+  
+    for ( Int_t i = 0; i < 2; ++i ) 
+    {
+      Int_t j = ix+i;
     
-    SetInternalDimensionName(j,Form("%s of %s",prefix[i],name));
+      SetInternalDimensionName(j,Form("%s of %s",prefix[i],name));
+    }
+  }
+  else
+  {
+    SetInternalDimensionName(index,name);
   }
   
   SetExternalDimensionName(index,name);
@@ -1099,7 +2066,7 @@ AliMUONTrackerData::SetDimensionName(Int_t index, const char* name)
 
 //_____________________________________________________________________________
 void 
-AliMUONTrackerData::SetHistogramDimension(Int_t index, Bool_t value)
+AliMUONTrackerData::MakeHistogramForDimension(Int_t index, Bool_t value, Double_t xmin, Double_t xmax)
 {
   /// decide to make histos for a given dimension
   if ( index >= ExternalDimension() ) 
@@ -1108,7 +2075,11 @@ AliMUONTrackerData::SetHistogramDimension(Int_t index, Bool_t value)
     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;
 }
 
 //_____________________________________________________________________________
@@ -1155,7 +2126,8 @@ AliMUONTrackerData::SetExternalDimensionName(Int_t index, const char* value)
 
 //_____________________________________________________________________________
 Double_t 
-AliMUONTrackerData::Value(const AliMUONVCalibParam& param, Int_t i, Int_t dim) const
+AliMUONTrackerData::Value(const AliMUONVCalibParam& param, Int_t i, 
+                          Int_t dim, Int_t ddlId) const
 {
   /// Compute the value for a given dim, using the internal information we have
   /// Basically we're converting sum of weights and sum of squares of weights
@@ -1172,26 +2144,146 @@ AliMUONTrackerData::Value(const AliMUONVCalibParam& param, Int_t i, Int_t dim) c
     return occ;
   }
   
-  if ( dim == IndexOfOccupancyDimension() ) return occ/n/NumberOfEvents();
+  if ( dim == IndexOfOccupancyDimension() ) 
+  {
+    if ( ddlId < 0 ) AliError("Got a negative ddl id !");
+    return occ/n/NumberOfEvents(ddlId);
+  }
   
   Double_t value = param.ValueAsDouble(i,dim);
   
   if ( value >= AliMUONVCalibParam::InvalidFloatValue() ) return AliMUONVCalibParam::InvalidFloatValue();
   
-  if ( TMath::Even(dim) ) 
+  if ( TMath::Even(dim) || IsSingleEvent() 
   {
-    return value/occ;
+               Double_t x = value/occ;
+               
+               return ( TMath::Finite(x) ? x : 0.0 ) ;
   }
   else
   {
-    Double_t n = occ;
+    Double_t nn = occ;
     
-    Double_t mean = param.ValueAsDouble(i,dim-1)/n;
+    if ( nn > 1.0 ) 
+    {
+      Double_t mean = param.ValueAsDouble(i,dim-1)/nn;
     
-    return  TMath::Sqrt(TMath::Abs((value-n*mean*mean)/(n-1.0)));
+      return TMath::Sqrt(TMath::Abs((value-nn*mean*mean)/(nn-1.0)));
+    }
+    else
+    {
+      return 0.0;
+    }
   }
   
   AliError("Why am I here ?");
   return 0.0;
 }
 
+//_____________________________________________________________________________
+void 
+AliMUONTrackerData::Streamer(TBuffer &R__b)
+{
+  /// Customized streamer                                                    
+  
+  if (R__b.IsReading()) {
+    AliMUONTrackerData::Class()->ReadBuffer(R__b, this);
+    if ( !fNofDDLs )
+    {
+      // backward compatible mode : we set number of events
+      // per DDL to the total number of events (the only information
+      // we had before version 7 of that class)
+      delete[] fNofEventsPerDDL;
+      fNofDDLs=20;
+      fNofEventsPerDDL = new Int_t[fNofDDLs];
+      for ( Int_t i = 0; i < fNofDDLs; ++i ) 
+      {
+        fNofEventsPerDDL[i] = fNevents;
+      }
+    }
+  } 
+  else {
+    AliMUONTrackerData::Class()->WriteBuffer(R__b, this);
+  }
+}
+
+//_____________________________________________________________________________
+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);
+  
+}