]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrackerData.cxx
Correctly reset the total number of events (for Amore)
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerData.cxx
index 9d26be3629bd29452a71d7088d0e680a2cd8d110..8b54b0408d2225f446d9d73a59d20f0abb6bbf33 100644 (file)
@@ -18,6 +18,7 @@
 #include "AliMUONTrackerData.h"
 
 #include "AliCodeTimer.h"
+#include "AliDAQ.h"
 #include "AliLog.h"
 #include "AliMUON1DArray.h"
 #include "AliMUON1DMap.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 <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
@@ -67,7 +75,7 @@ fDEValues(0x0),
 fChamberValues(0x0),
 fPCBValues(0x0),
 fDimension(External2Internal(dimension)+fgkExtraDimension),
-fNevents(0x0),
+fNevents(0),
 fDimensionNames(new TObjArray(fDimension+fgkVirtualExtraDimension)),
 fExternalDimensionNames(new TObjArray(dimension)),
 fExternalDimension(dimension),
@@ -76,10 +84,12 @@ fHistos(0x0),
 fXmin(0.0),
 fXmax(0.0),
 fIsChannelLevelEnabled(kTRUE),
-fIsManuLevelEnabled(kTRUE)
+fIsManuLevelEnabled(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());
@@ -88,6 +98,154 @@ fIsManuLevelEnabled(kTRUE)
   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),
+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()
 {
@@ -102,14 +260,215 @@ AliMUONTrackerData::~AliMUONTrackerData()
   delete fExternalDimensionNames;
   delete[] fHistogramming;
   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,kFALSE);
+  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.
+  
+  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 (%x vs %x) (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);
+        }
+      }
+    }
+  }
+  
+  fNevents =  TMath::Max(fNevents,data.fNevents);
+  
+  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()));
+    
+    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));
+    }
+  }
 }
 
 //_____________________________________________________________________________
@@ -117,29 +476,58 @@ Bool_t
 AliMUONTrackerData::Replace(const AliMUONVStore& store)
 {
   /// Replace our values by values from the given external store
-  Bool_t rv = InternalAdd(store,kTRUE);
+  Bool_t rv = InternalAdd(store,0x0,kTRUE);
   AliMUONVTrackerData::Replace(store);
   return rv;
 }
 
 //_____________________________________________________________________________
 Bool_t
-AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, 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 (!fNofDDLs)
   {
-    if ( IsSingleEvent() && fNevents == 1 ) 
+    fNofDDLs = AliDAQ::NumberOfDdls("MUONTRK");
+    fNofEventsPerDDL = new Int_t[fNofDDLs];
+    for ( Int_t i = 0; i < fNofDDLs; ++i ) 
     {
-      AliError(Form("%s is supposed to be single event only",GetName()));
+      fNofEventsPerDDL[i] = 0;
+    }
+  }
+  
+  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;
-    }  
-    ++fNevents;  
-    NumberOfEventsChanged();
+    }
+    
+    for ( Int_t i = 0 ; i < fNofDDLs; ++i ) 
+    {
+      fNofEventsPerDDL[i] += nevents->At(i);
+      fNevents = TMath::Max(fNevents,fNofEventsPerDDL[i]);
+    }
+  }
+  else
+  {
+    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)
   {
@@ -159,10 +547,10 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, Bool_t replace)
       deIt.Next();
     }
     
-               if ( fIsChannelLevelEnabled ) 
-               {
-                       fChannelValues = new AliMUON2DMap(kTRUE);
-               }
+    if ( fIsChannelLevelEnabled ) 
+    {
+      fChannelValues = new AliMUON2DMap(kTRUE);
+    }
     if  ( fIsManuLevelEnabled ) 
     {
       fManuValues = new AliMUON2DMap(kTRUE);
@@ -172,6 +560,28 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, Bool_t replace)
     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;
@@ -189,9 +599,13 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, Bool_t replace)
       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);
     
@@ -291,6 +705,8 @@ AliMUONTrackerData::InternalAdd(const AliMUONVStore& store, Bool_t replace)
     }
   }
   
+  NumberOfEventsChanged();
+  
   return kTRUE;
 }
 
@@ -301,7 +717,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;
 }
 
 //_____________________________________________________________________________
@@ -328,7 +744,7 @@ AliMUONTrackerData::CreateBusPatchParam(Int_t busPatchId) const
 {
   /// Create storage for one bus patch
   
-  AliCodeTimerAuto("");
+  AliCodeTimerAuto("",0);
   
   AliMpBusPatch* bp = AliMpDDLStore::Instance()->GetBusPatch(busPatchId);
   
@@ -365,8 +781,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;
 }
 
 //_____________________________________________________________________________
@@ -393,7 +814,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);
   
@@ -437,7 +858,7 @@ 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;
 }
 
 //_____________________________________________________________________________
@@ -471,6 +892,10 @@ AliMUONTrackerData::Clear(Option_t*)
   if ( fDEValues) fDEValues->Clear();
   if ( fChamberValues ) fChamberValues->Clear();
   if ( fHistos ) fHistos->Clear();
+  for ( Int_t i = 0; i < fNofDDLs; ++i ) 
+  {
+    fNofEventsPerDDL[i] = 0;
+  }
   fNevents = 0;
   NumberOfEventsChanged();
 }
@@ -492,7 +917,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(),
@@ -520,7 +945,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;
 
 }
 
@@ -549,7 +974,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);
   
@@ -572,6 +997,61 @@ AliMUONTrackerData::CreateDetectionElementParam(Int_t detElemId) const
   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
+{
+  /// 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
+  
+  AliMpDEIterator it;
+  
+  it.First(chamberid);
+  Int_t n(0);
+  Int_t d(-1);
+  
+  while (!it.IsDone())
+  {
+    Int_t detElemId = it.CurrentDEId();
+    Int_t ddlId = DdlIdFromDetElemId(detElemId);
+    if ( NumberOfEvents(ddlId) > n ) 
+    {
+      n = NumberOfEvents(ddlId);
+      d = ddlId;
+    }
+    it.Next();
+  }
+  
+  return d;
+}
+
 //_____________________________________________________________________________
 TString 
 AliMUONTrackerData::DimensionName(Int_t dim) const
@@ -759,6 +1239,41 @@ AliMUONTrackerData::GetChannelSparseHisto(Int_t detElemId, Int_t manuId,
   return h;
 }
 
+//_____________________________________________________________________________
+void
+AliMUONTrackerData::GetDEManu(const AliMUONVCalibParam& param,
+                              Int_t& detElemId, Int_t& manuId) const
+{
+  /// Tries to get (detElemId,manuId) of param
+  
+  // Load mapping manu store
+  if ( ! AliMpCDB::LoadManuStore() ) {
+    AliError("Could not access manu store from OCDB !");
+    return;
+  }
+
+  if ( param.ID1() <= 0 ) 
+  {
+    // 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));
+    }
+  }
+  else
+  {
+    // we get a (de,manu) pair
+    detElemId = param.ID0();
+    manuId = param.ID1();
+  }
+}
+
+
 //_____________________________________________________________________________
 Int_t
 AliMUONTrackerData::GetParts(AliMUONVCalibParam* external,
@@ -772,49 +1287,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;
   Int_t manuId;
   
-  if ( external->ID1() <= 0 ) 
+  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
   {
-    // we get a manu serial number
-    Int_t serial = external->ID0();
-    AliMpIntPair pair = ddlStore->GetDetElemIdManu(serial);
-    detElemId = pair.GetFirst();
-    manuId = pair.GetSecond();
-    if ( !detElemId ) 
-    {
-      AliError(Form("DE %d manuId %d from serial %d is not correct !",
-                    detElemId,manuId,serial));
-      return -1;
-    }
+    return -1;
   }
-  else
+  
+  // explicitely check that de,manu is correct
+  const AliMpVSegmentation* mpseg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId, manuId,kFALSE);
+  
+  if (!mpseg)
   {
-    // we get a (de,manu) pair
-    detElemId = external->ID0();
-    manuId = external->ID1();
+    return -1;
   }
-
-  mpde = ddlStore->GetDetElement(detElemId);
-
+  
   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);
   
@@ -881,7 +1401,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;
 }
 
 //_____________________________________________________________________________
@@ -908,7 +1428,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);
   
@@ -923,13 +1443,36 @@ AliMUONTrackerData::CreateManuParam(Int_t detElemId, Int_t manuId) const
 
 //_____________________________________________________________________________
 Long64_t
-AliMUONTrackerData::Merge(TCollection*)
+AliMUONTrackerData::Merge(TCollection* list)
 {
-  /// Merge all tracker data objects from li into a single one.
+  /// Merge this with a list of AliMUONVTrackerData
+
+  if (!list) return 0;
+  
+  if ( list->IsEmpty() ) return NumberOfEvents(-1);
   
-  AliError("Not implemented yet");
+  TIter next(list);
+  const TObject* o(0x0);
   
-  return 0;
+  while ( ( o = next() ) )
+  {
+    const AliMUONTrackerData* data = dynamic_cast<const AliMUONTrackerData*>(o);
+    if (!o)
+    {
+      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);
 }
 
 //_____________________________________________________________________________
@@ -941,6 +1484,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
@@ -949,7 +1514,7 @@ AliMUONTrackerData::PCB(Int_t detElemId, Int_t pcbIndex, Int_t dim) const
 
   AliMUONVCalibParam* param = PCBParam(detElemId,pcbIndex);
   
-  return param ? Value(*param,0,dim) : 0.0;
+  return param ? Value(*param,0,dim,DdlIdFromDetElemId(detElemId)) : 0.0;
 }
 
 //_____________________________________________________________________________
@@ -976,9 +1541,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),
@@ -998,7 +1563,10 @@ AliMUONTrackerData::Print(Option_t* wildcard, Option_t* opt) const
   
   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 ) 
@@ -1078,7 +1646,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;
   }
   
@@ -1114,6 +1684,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;
@@ -1163,7 +1735,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
@@ -1180,7 +1753,11 @@ 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);
   
@@ -1212,3 +1789,92 @@ AliMUONTrackerData::Value(const AliMUONVCalibParam& param, Int_t i, Int_t dim) c
   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;
+}
+
+