]> git.uio.no Git - u/mrichter/AliRoot.git/commitdiff
adding the Attach and Prune methods (Laurent)
authorpcrochet <pcrochet@f7af4fe6-9843-0410-8265-dc069ae4e863>
Fri, 14 Jun 2013 19:27:31 +0000 (19:27 +0000)
committerpcrochet <pcrochet@f7af4fe6-9843-0410-8265-dc069ae4e863>
Fri, 14 Jun 2013 19:27:31 +0000 (19:27 +0000)
PWG/muon/AliMergeableCollection.cxx
PWG/muon/AliMergeableCollection.h

index 7ca36cf57cfcf425f731d0d5f02a2c54653aab7e..31e15217498de07c2737f4fcb36b3062f7a0342a 100644 (file)
@@ -33,6 +33,7 @@ ClassImp(AliMergeableCollection)
 #include "AliLog.h"
 #include "Riostream.h"
 #include "TError.h"
+#include "TGraph.h"
 #include "THashList.h"
 #include "TKey.h"
 #include "TMap.h"
@@ -86,6 +87,49 @@ AliMergeableCollection::Adopt(const char* identifier, TObject* obj)
   return InternalAdopt(sidentifier.Data(),obj);
 }
 
+//_____________________________________________________________________________
+Bool_t AliMergeableCollection::Attach(AliMergeableCollection* mc, const char* identifier, Bool_t pruneFirstIfAlreadyExists)
+{
+  /// Attach an already existing mergeable collection to this one.
+  /// It is attached at level identifier/
+  /// We take ownership of mc
+  /// If identifier is already existing we kill it if pruneFirstIfAlreadyExists is kTRUE
+  /// (and attach mc) otherwise we return kFALSE (and do *not* attach mc)
+  
+  THashList* hlist = dynamic_cast<THashList*>(Map()->GetValue(identifier));
+  
+  if (hlist)
+  {
+    if (!pruneFirstIfAlreadyExists)
+    {
+      AliError(Form("%s already exist. Will not overwrite it.",identifier));
+      return kFALSE;
+    }
+    else
+    {
+      Int_t n = Prune(identifier);
+      if (!n)
+      {
+        AliError(Form("Could not prune pre-existing %s",identifier));
+        return kFALSE;        
+      }
+    }
+  }
+
+  TIter next(mc->fMap);
+  TObjString* str;
+  
+  while ( ( str = static_cast<TObjString*>(next())) )
+  {
+    THashList* hl = dynamic_cast<THashList*>(mc->Map()->GetValue(str->String()));
+    TString newid(Form("/%s%s",identifier,str->String().Data()));
+    newid.ReplaceAll("//","/");                  
+    Map()->Add(new TObjString(newid.Data()),hl);
+  }
+  
+  return kTRUE;
+}
+
 //_____________________________________________________________________________
 void AliMergeableCollection::ClearMessages()
 {
@@ -901,17 +945,43 @@ AliMergeableCollection::Print(Option_t* option) const
       {
         obj = list->FindObject(objName.Data());
         if ( IsEmptyObject(obj) && ! fMustShowEmptyObject ) continue;
+        
         if (!identifierPrinted)
         {
           cout << identifier.Data() << endl;
           identifierPrinted = kTRUE;
         }
-        cout << Form("    (%s) %s", obj->ClassName(), obj->GetName());
-        if ( obj->IsA()->InheritsFrom(TH1::Class()) ) {
+        
+        TString extra;
+        TString warning("   ");
+        
+        if ( obj->IsA()->InheritsFrom(TH1::Class()) )
+        {
+          
           TH1* histo = static_cast<TH1*> (obj);
-          cout << Form(" %s Entries=%d Sum=%g",histo->GetTitle(),Int_t(histo->GetEntries()),histo->GetSumOfWeights());
+          extra.Form("%s | Entries=%d Sum=%g",histo->GetTitle(),Int_t(histo->GetEntries()),histo->GetSumOfWeights());
+        }
+        else if ( obj->IsA()->InheritsFrom(TGraph::Class()) )
+        {
+          TGraph* graph = static_cast<TGraph*> (obj);
+          if ( ! TMath::Finite(graph->GetMean(2) ) )
+          {
+            warning = " ! ";
+          }
+          extra.Form("%s | Npts=%d Mean=%g RMS=%g",graph->GetTitle(),graph->GetN(),
+                       graph->GetMean(2),graph->GetRMS(2));
+          
         }
-        cout << endl;
+        
+        std::cout << Form("    (%s) %s %s", obj->ClassName(),
+                     warning.Data(),
+                          obj->GetName());
+        
+        if ( extra.Length() )
+        {
+          std::cout << " | " << extra.Data();
+        }
+        std::cout << std::endl;
       }
     }
     if (!identifierPrinted && sreObjectName=="-" )
@@ -1028,6 +1098,30 @@ AliMergeableCollection::EstimateSize(Bool_t show) const
   return size;
 }
 
+//_____________________________________________________________________________
+Int_t AliMergeableCollection::Prune(const char* identifier)
+{
+  // Delete all objects which match the beginning of the identifier
+  // returns the number of entries removed from the Map()
+  // (not to be confused with the number of leaf objects removed)
+  //
+  
+  TIter next(Map());
+  TObjString* key;
+  Int_t ndeleted(0);
+  
+  while ( ( key = static_cast<TObjString*>(next())) )
+  {
+      if (key->String().BeginsWith(identifier))
+      {
+        Bool_t ok = Map()->DeleteEntry(key);
+        if (ok) ++ndeleted;
+      }
+  }
+  
+  return ndeleted;
+}
+
 //_____________________________________________________________________________
 void AliMergeableCollection::PruneEmptyObjects()
 {
index a5566588fa876b8b59dea77dce0cc6b4f190e805..04ad57716b9ff466995864a28dfa0c5d6714e55a 100644 (file)
@@ -46,6 +46,8 @@ public:
 
   virtual AliMergeableCollection* Clone(const char* name="") const;
   
+  Bool_t Attach(AliMergeableCollection* mc, const char* identifier, Bool_t pruneFirstIfAlreadyExists=kFALSE);
+  
   Bool_t Adopt(TObject* obj);
   Bool_t Adopt(const char* identifier, TObject* obj);
     
@@ -99,6 +101,8 @@ public:
   
   void PruneEmptyObjects();
   
+  Int_t Prune(const char* identifier);
+  
   static Bool_t MergeObject(TObject* baseObject, TObject* objToAdd);
   
   TObject* GetSum(const char* idPattern);