1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 // $Id: AliMergeableCollection.cxx 50593 2011-07-14 17:42:28Z martinez $
19 /// A mergeable object container.
21 /// For each tuple (key1,key2,..,keyN) a (hash)list of mergeable objects is associated.
22 /// Note that key1, key2 (optional), ..., keyN (optional) are strings.
23 /// Those strings should not contain "/" themselves.
25 /// More helper functions might be added in the future (e.g. Project, etc...)
27 #include "AliMergeableCollection.h"
31 ClassImp(AliMergeableCollection)
34 #include "Riostream.h"
40 #include "THashList.h"
41 #include "THnSparse.h"
44 #include "TObjArray.h"
45 #include "TObjString.h"
54 //_____________________________________________________________________________
55 AliMergeableCollection::AliMergeableCollection(const char* name, const char* title)
56 : TFolder(name,title), fMap(0x0), fMustShowEmptyObject(0), fMapVersion(0), fMessages()
61 //_____________________________________________________________________________
62 AliMergeableCollection::~AliMergeableCollection()
64 /// dtor. Note that the map is owner
68 //_____________________________________________________________________________
70 AliMergeableCollection::Adopt(TObject* obj)
72 /// Adopt a given object at top level (i.e. no key)
73 return InternalAdopt("",obj);
76 //_____________________________________________________________________________
78 AliMergeableCollection::Adopt(const char* identifier, TObject* obj)
80 /// Adopt a given object, and associate it with pair key
81 TString sidentifier(identifier);
82 if ( ! sidentifier.IsNull() ){
83 if ( ! sidentifier.EndsWith("/") ) sidentifier.Append("/");
84 if ( ! sidentifier.BeginsWith("/") ) sidentifier.Prepend("/");
86 return InternalAdopt(sidentifier.Data(),obj);
89 //_____________________________________________________________________________
90 Bool_t AliMergeableCollection::Attach(AliMergeableCollection* mc, const char* identifier, Bool_t pruneFirstIfAlreadyExists)
92 /// Attach an already existing mergeable collection to this one.
93 /// It is attached at level identifier/
94 /// We take ownership of mc
95 /// If identifier is already existing we kill it if pruneFirstIfAlreadyExists is kTRUE
96 /// (and attach mc) otherwise we return kFALSE (and do *not* attach mc)
98 THashList* hlist = dynamic_cast<THashList*>(Map()->GetValue(identifier));
102 if (!pruneFirstIfAlreadyExists)
104 AliError(Form("%s already exist. Will not overwrite it.",identifier));
109 Int_t n = Prune(identifier);
112 AliError(Form("Could not prune pre-existing %s",identifier));
118 TIter next(mc->fMap);
121 while ( ( str = static_cast<TObjString*>(next())) )
123 THashList* hl = dynamic_cast<THashList*>(mc->Map()->GetValue(str->String()));
124 TString newid(Form("/%s%s",identifier,str->String().Data()));
125 newid.ReplaceAll("//","/");
126 Map()->Add(new TObjString(newid.Data()),hl);
132 //_____________________________________________________________________________
133 void AliMergeableCollection::Browse(TBrowser* b)
135 /// Create a TFolder structure pointing to our objects, so we
138 if ( !fFolders ) return;
140 TObjArray* ids = SortAllIdentifiers();
141 TIter nextIdentifier(ids);
144 while ( ( str = static_cast<TObjString*>(nextIdentifier()) ) )
146 TObjArray* parts = str->String().Tokenize("/");
148 TIter nextPart(parts);
149 TFolder* base = this;
151 while ( ( s = static_cast<TObjString*>(nextPart())))
153 TFolder* f = static_cast<TFolder*>(base->TFolder::FindObject(s->String()));
156 f = new TFolder(s->String(),"");
163 TList* list = CreateListOfObjectNames(str->String());
167 TIter nextObject(list);
168 while ( ( oname = static_cast<TObjString*>(nextObject())) )
170 TObject* o = GetObject(str->String(),oname->String());
176 AliError("got list=0x0");
181 TList* top = CreateListOfKeys(0);
185 while ( ( stop = static_cast<TObjString*>(nextTop())) )
187 b->Add(TFolder::FindObject(stop->String()));
195 //_____________________________________________________________________________
196 void AliMergeableCollection::ClearMessages()
198 /// clear pending messages
202 //_____________________________________________________________________________
204 AliMergeableCollection::CreateIterator(Bool_t direction) const
206 /// Create an iterator (must be deleted by the client)
207 return fMap ? new AliMergeableCollectionIterator(this,direction) : 0x0;
210 //_____________________________________________________________________________
211 AliMergeableCollection*
212 AliMergeableCollection::Clone(const char* name) const
214 /// Clone this collection.
215 /// We loose the messages.
217 AliMergeableCollection* newone = new AliMergeableCollection(name,GetTitle());
219 newone->fMap = static_cast<TMap*>(fMap->Clone());
220 newone->fMustShowEmptyObject = fMustShowEmptyObject;
221 newone->fMapVersion = fMapVersion;
226 //_____________________________________________________________________________
228 AliMergeableCollection::Delete(Option_t*)
230 /// Delete all the objects
236 //_____________________________________________________________________________
238 AliMergeableCollection::FindObject(const char* fullIdentifier) const
240 /// Find an object by its full identifier.
242 return GetObject(fullIdentifier);
245 //_____________________________________________________________________________
247 AliMergeableCollection::FindObject(const TObject *object) const
250 AliWarning("This method is awfully inefficient. Please improve it or use FindObject(const char*)");
251 TIter next(CreateIterator());
253 while ( ( obj=next() ) )
255 if ( obj->IsEqual(object) ) return obj;
261 //_____________________________________________________________________________
263 AliMergeableCollection::CreateListOfKeys(Int_t index) const
265 /// Create the list of keys at level index
267 TList* list = new TList;
268 list->SetOwner(kTRUE);
270 TObjArray* ids = SortAllIdentifiers();
274 while ( ( str = static_cast<TObjString*>(next()) ) )
276 TString oneid = GetKey(str->String().Data(),index,kFALSE);
277 if (oneid.Length()>0 && !list->Contains(oneid))
279 list->Add(new TObjString(oneid));
287 //_____________________________________________________________________________
289 AliMergeableCollection::CreateListOfObjectNames(const char* identifier) const
291 /// Create list of object names for /key1/key2/key...
292 /// Returned list must be deleted by client
294 TList* listOfNames = new TList;
295 listOfNames->SetOwner(kTRUE);
300 while ( ( str = static_cast<TObjString*>(next()) ) )
302 TString currIdentifier = str->String();
303 if ( currIdentifier.CompareTo(identifier) ) continue;
305 THashList* list = static_cast<THashList*>(Map()->GetValue(identifier));
307 TIter nextObject(list);
310 while ( ( obj = nextObject() ) )
312 listOfNames->Add(new TObjString(obj->GetName()));
320 //_____________________________________________________________________________
322 AliMergeableCollection::GetIdentifier(const char* fullIdentifier) const
324 /// Extract the identifier from the fullIdentifier
328 Int_t n = TString(fullIdentifier).CountChar('/')-1;
330 for (Int_t i=0; i < n; ++i)
333 identifier += InternalDecode(fullIdentifier,i);
339 //_____________________________________________________________________________
341 AliMergeableCollection::GetKey(const char* identifier, Int_t index, Bool_t idContainsObjName) const
343 /// Extract the index element of the key pair from the fullIdentifier
345 if ( ! idContainsObjName )
347 TString sidentifier(identifier);
348 sidentifier.Append("/dummy");
349 return InternalDecode(sidentifier.Data(),index);
352 return InternalDecode(identifier,index);
355 //_____________________________________________________________________________
357 AliMergeableCollection::GetObjectName(const char* fullIdentifier) const
359 /// Extract the object name from an identifier
361 return InternalDecode(fullIdentifier,-1);
364 //_____________________________________________________________________________
366 AliMergeableCollection::Histo(const char* fullIdentifier) const
368 /// Get histogram key1/key2/.../objectName:action
369 /// action is used for 2D histograms :
370 /// might be px for projection along x-axis
371 /// py for projection along y-axis
372 /// pfx for profile along x-axis
373 /// pfy for profile along y-axis
375 TString sfullIdentifier(fullIdentifier);
377 TString fullIdWithoutAction(fullIdentifier);
380 if ( sfullIdentifier.First(':') != kNPOS )
382 TObjArray* arr = sfullIdentifier.Tokenize(":");
384 fullIdWithoutAction = static_cast<TObjString*>(arr->At(0))->String();
386 if ( arr->GetLast() > 0 )
388 action = static_cast<TObjString*>(arr->At(1))->String();
395 Int_t nslashes = sfullIdentifier.CountChar('/');
401 o = GetObject("", fullIdWithoutAction);
405 o = GetObject(GetIdentifier(fullIdWithoutAction).Data(), GetObjectName(fullIdWithoutAction));
408 return HistoWithAction(fullIdWithoutAction.Data(),o,action);
411 //_____________________________________________________________________________
413 AliMergeableCollection::Histo(const char* identifier,
414 const char* objectName) const
416 /// Get histogram key1/key2/.../objectName:action
417 /// action is used for 2D histograms :
418 /// might be px for projection along x-axis
419 /// py for projection along y-axis
420 /// pfx for profile along x-axis
421 /// pfy for profile along y-axis
423 TObject* o = GetObject(identifier,objectName);
425 TObjArray* arr = TString(objectName).Tokenize(":");
428 if ( arr->GetLast() > 0 )
430 action = static_cast<TObjString*>(arr->At(1))->String();
436 return HistoWithAction(identifier,o,action);
440 //_____________________________________________________________________________
442 AliMergeableCollection::HistoWithAction(const char* identifier, TObject* o, const TString& action) const
444 /// Convert o to an histogram if possible, applying a given action if there
448 if (!o->InheritsFrom("TH1"))
450 AliError(Form("%s is not an histogram",o->GetName()));
454 TH2* h2 = dynamic_cast<TH2*>(o);
458 if ( action == "PX" )
460 return h2->ProjectionX(NormalizeName(Form("%s/%s",identifier,o->GetName()),action.Data()).Data());
462 if ( action == "PY" )
464 return h2->ProjectionY(NormalizeName(Form("%s/%s",identifier,o->GetName()),action.Data()).Data());
466 if ( action == "PFX" )
468 return h2->ProfileX(NormalizeName(Form("%s/%s",identifier,o->GetName()),action.Data()).Data());
470 if ( action == "PFY" )
472 return h2->ProfileY(NormalizeName(Form("%s/%s",identifier,o->GetName()),action.Data()).Data());
476 return static_cast<TH1*>(o);
479 //_____________________________________________________________________________
481 AliMergeableCollection::H2(const char* fullIdentifier) const
483 /// Short-cut method to grab a 2D histogram
484 /// Will return 0x0 if the object if not a TH2xxx
486 TObject* o = GetObject(fullIdentifier);
488 if (o->IsA()->InheritsFrom(TH2::Class()))
490 return static_cast<TH2*>(o);
495 //_____________________________________________________________________________
497 AliMergeableCollection::H2(const char* identifier,
498 const char* objectName) const
500 /// Short-cut method to grab a 2D histogram
501 /// Will return 0x0 if the object if not a TH2xxx
503 TObject* o = GetObject(identifier,objectName);
505 if (o->IsA()->InheritsFrom(TH2::Class()))
507 return static_cast<TH2*>(o);
512 //_____________________________________________________________________________
514 AliMergeableCollection::Prof(const char* fullIdentifier) const
516 /// Short-cut method to grab a TProfile histogram
517 /// Will return 0x0 if the object if not a TProfile
519 TObject* o = GetObject(fullIdentifier);
521 if (o->IsA()->InheritsFrom(TProfile::Class()))
523 return static_cast<TProfile*>(o);
528 //_____________________________________________________________________________
530 AliMergeableCollection::Prof(const char* identifier,
531 const char* objectName) const
533 /// Short-cut method to grab a TProfile histogram
534 /// Will return 0x0 if the object if not a TProfile
536 TObject* o = GetObject(identifier,objectName);
538 if (o->IsA()->InheritsFrom(TProfile::Class()))
540 return static_cast<TProfile*>(o);
545 //_____________________________________________________________________________
547 AliMergeableCollection::GetObject(const char* fullIdentifier) const
549 /// Get object key1/key2/.../objectName
550 /// Note that no action is allowed for generic objects (only for histograms,
551 /// see Histo() methods)
553 TString sfullIdentifier(fullIdentifier);
555 Int_t nslashes = sfullIdentifier.CountChar('/');
559 return GetObject("", sfullIdentifier);
563 return GetObject(GetIdentifier(fullIdentifier).Data(), GetObjectName(fullIdentifier));
567 //_____________________________________________________________________________
569 AliMergeableCollection::GetObject(const char* identifier,
570 const char* objectName) const
572 /// Get object for (identifier,objectName) triplet
574 TString sidentifier(identifier);
575 if ( ! sidentifier.IsNull() ) {
576 if ( ! sidentifier.BeginsWith("/") ) sidentifier.Prepend("/");
577 if ( ! sidentifier.EndsWith("/") ) sidentifier.Append("/");
579 return InternalObject(sidentifier.Data(),objectName);
582 //_____________________________________________________________________________
583 TObject* AliMergeableCollection::GetSum(const char* idPattern) const
586 /// The pattern must be in the form:
587 /// /key1_1,key1_2,.../key2_1,key2_2,.../.../objectName_1,objectName_2...
588 /// The logical or between patterns separated by commas is taken
589 /// Exact match is required for keys and objectNames
591 TObject* sumObject = 0x0;
593 // Build array of lists of pattern
594 TString idPatternString(idPattern);
595 TObjArray* keyList = idPatternString.Tokenize("/");
596 TObjArray keyMatrix(keyList->GetEntries());
597 keyMatrix.SetOwner();
598 for ( Int_t ikey=0; ikey<keyList->GetEntries(); ikey++ ) {
599 TObjArray* subKeyList = ((TObjString*)keyList->At(ikey))->GetString().Tokenize(",");
600 keyMatrix.AddAt(subKeyList, ikey);
604 TString debugMsg = "Adding objects:";
607 // First handle the keys
611 while ( ( str = static_cast<TObjString*>(next()) ) )
613 TString identifier = str->String();
615 Bool_t listMatchPattern = kTRUE;
616 for ( Int_t ikey=0; ikey<keyMatrix.GetEntries()-1; ikey++ ) {
617 TString currKey = GetKey(identifier, ikey, kFALSE);
618 Bool_t matchKey = kFALSE;
619 TObjArray* subKeyList = static_cast<TObjArray*> ( keyMatrix.At(ikey) );
620 for ( Int_t isub=0; isub<subKeyList->GetEntries(); isub++ ) {
621 TString subKeyString = static_cast<TObjString*> (subKeyList->At(isub))->GetString();
622 if ( currKey == subKeyString ) {
626 } // loop on the list of patterns of each key
628 listMatchPattern = kFALSE;
631 } // loop on keys in the idPattern
632 if ( ! listMatchPattern ) continue;
636 // Then handle the object name
638 THashList* list = static_cast<THashList*>(Map()->GetValue(identifier.Data()));
643 while ( ( obj = nextObj()) )
645 TString currKey = obj->GetName();
646 Bool_t matchKey = kFALSE;
647 TObjArray* subKeyList = static_cast<TObjArray*> ( keyMatrix.Last() );
648 for ( Int_t isub=0; isub<subKeyList->GetEntries(); isub++ ) {
649 TString subKeyString = static_cast<TObjString*> (subKeyList->At(isub))->GetString();
650 if ( currKey == subKeyString ) {
655 if ( ! matchKey ) continue;
656 if ( ! sumObject ) sumObject = obj->Clone();
657 else MergeObject(sumObject, obj);
658 debugMsg += Form(" %s%s",identifier.Data(),obj->GetName());
659 } // loop on objects in list
660 } // loop on identifiers in map
662 AliDebug(1,debugMsg.Data());
667 //_____________________________________________________________________________
668 Bool_t AliMergeableCollection::InternalAdopt(const char* identifier, TObject* obj)
674 Error("Adopt","Cannot adopt a null object");
678 if ( ! obj->IsA()->InheritsFrom(TObject::Class()) ||
679 ! obj->IsA()->GetMethodWithPrototype("Merge", "TCollection*") ) {
680 Error("Adopt","Cannot adopt an object which is not mergeable!");
683 THashList* hlist = 0x0;
685 hlist = static_cast<THashList*>(Map()->GetValue(identifier));
689 hlist = new THashList;
690 hlist->SetOwner(kTRUE);
691 Map()->Add(new TObjString(identifier),hlist);
692 hlist->SetName(identifier);
695 TObject* existingObj = hlist->FindObject(obj->GetName());
699 AliError(Form("Cannot adopt an already existing object : %s -> %s",identifier,existingObj->GetName()));
703 if ( obj->IsA()->InheritsFrom(TH1::Class()) ) (static_cast<TH1*> ( obj ))->SetDirectory(0);
711 //_____________________________________________________________________________
713 AliMergeableCollection::InternalDecode(const char* identifier, Int_t index) const
715 /// Extract the index-th element of the identifier (/key1/key2/.../keyN/objectName)
716 /// object is index=-1 (i.e. last)
718 if ( strlen(identifier) > 0 && identifier[0] != '/' )
720 AliError(Form("identifier %s is malformed (should start with /)",identifier));
724 std::vector<Int_t> splitIndex;
727 TString sidentifier(identifier);
729 while (start < sidentifier.Length())
731 Int_t pos = sidentifier.Index('/', start);
732 if (pos == kNPOS) break;
733 splitIndex.push_back(pos);
737 Int_t nkeys = splitIndex.size() - 1;
739 if ( index >= nkeys )
741 AliError(Form("Requiring index %i of identifier %s which only have %i",index, identifier, nkeys));
747 return sidentifier(splitIndex.back()+1,sidentifier.Length()-splitIndex.back()-1);
750 return sidentifier(splitIndex[index]+1,splitIndex[index+1]-splitIndex[index]-1);
753 //_____________________________________________________________________________
755 AliMergeableCollection::InternalObject(const char* identifier,
756 const char* objectName) const
758 /// Get object for (identifier,objectName)
765 THashList* hlist = static_cast<THashList*>(Map()->GetValue(identifier));
768 TString msg(Form("Did not find hashlist for identifier=%s dir=%s",identifier,gDirectory ? gDirectory->GetName() : "" ));
769 fMessages[msg.Data()]++;
773 TObject* obj = hlist->FindObject(objectName);
776 TString msg(Form("Did not find objectName=%s in %s",objectName,identifier));
777 fMessages[msg.Data()]++;
783 //_____________________________________________________________________________
784 Bool_t AliMergeableCollection::IsEmptyObject(TObject* obj) const
786 /// Check if object is empty
787 /// (done only for TH1, so far)
789 if ( obj->IsA()->InheritsFrom(TH1::Class()) ) {
790 TH1* histo = static_cast<TH1*> (obj);
791 if ( histo->GetEntries() == 0 ) return kTRUE;
799 //_____________________________________________________________________________
800 TMap* AliMergeableCollection::Map() const
802 /// Wrapper to insure proper key formats (i.e. new vs old)
807 fMap->SetOwnerKeyValue(kTRUE,kTRUE);
812 if ( fMapVersion < 1 )
814 AliInfo("Remapping");
819 while ( ( str = static_cast<TObjString*>(next()) ) )
821 if ( str->String().Contains("./") )
823 TString newkey(str->String());
825 newkey.ReplaceAll("./","");
827 TObject* o = fMap->GetValue(str);
829 TPair* p = fMap->RemoveEntry(str);
832 AliError("oups oups oups");
836 fMap->Add(new TObjString(newkey.Data()),o);
849 //_____________________________________________________________________________
851 AliMergeableCollection::Merge(TCollection* list)
853 // Merge a list of AliMergeableCollection objects with this
854 // Returns the number of merged objects (including this).
858 if (list->IsEmpty()) return 1;
865 while ( ( currObj = next() ) )
867 AliMergeableCollection* mergeCol = dynamic_cast<AliMergeableCollection*>(currObj);
869 AliFatal(Form("object named \"%s\" is a %s instead of an AliMergeableCollection!", currObj->GetName(), currObj->ClassName()));
875 if ( mergeCol->fMap ) mergeCol->Map(); // to insure keys in the new format
877 TIter nextIdentifier(mergeCol->fMap);
878 TObjString* identifier;
880 while ( ( identifier = static_cast<TObjString*>(nextIdentifier()) ) )
882 THashList* otherList = static_cast<THashList*>(mergeCol->fMap->GetValue(identifier->String().Data()));
884 TIter nextObject(otherList);
887 while ( ( obj = nextObject() ) )
889 TString newid(Form("%s%s",identifier->String().Data(),obj->GetName()));
891 TObject* thisObject = GetObject(newid.Data());
895 AliDebug(1,Form("Adopting a new object = %s%s",identifier->String().Data(),obj->GetName()));
897 Bool_t ok = Adopt(identifier->String(), obj->Clone());
901 AliError(Form("Adoption of object %s failed",obj->GetName()));
907 AliDebug(1,Form("Merging object = %s%s",
908 identifier->String().Data(),
911 MergeObject(thisObject, obj);
913 } // loop on objects in map
914 } // loop on identifiers
915 } // loop on collections in list
917 AliDebug(1,Form("count=%d",count));
922 //_____________________________________________________________________________
923 Bool_t AliMergeableCollection::MergeObject(TObject* baseObject, TObject* objToAdd)
925 /// Add objToAdd to baseObject
927 if ( baseObject->IsA()->Class() != objToAdd->IsA()->Class() ) {
928 printf("MergeObject: Cannot add %s to %s", objToAdd->ClassName(), baseObject->ClassName());
931 if ( ! baseObject->IsA()->InheritsFrom(TObject::Class()) ||
932 ! baseObject->IsA()->GetMethodWithPrototype("Merge", "TCollection*") ) {
933 printf("MergeObject: Objects are not mergeable!");
940 TString listArgs = Form("((TCollection*)0x%lx)", (ULong_t)&list);
942 baseObject->Execute("Merge", listArgs.Data(), &error);
946 //_____________________________________________________________________________
947 TString AliMergeableCollection::NormalizeName(const char* identifier,const char* action) const
949 /// Replace / by _ to build a root-compliant histo name
950 TString name(GetName());
956 name.ReplaceAll("/","_");
957 name.ReplaceAll("-","_");
961 //_____________________________________________________________________________
963 AliMergeableCollection::NumberOfObjects() const
965 /// Get the number of objects we hold
966 TIter next(CreateIterator());
968 while ( next() ) ++count;
972 //_____________________________________________________________________________
974 AliMergeableCollection::NumberOfKeys() const
976 /// Get the number of keys we have
977 return fMap ? fMap->GetSize() : 0;
980 //_____________________________________________________________________________
982 AliMergeableCollection::Print(Option_t* option) const
984 /// Print all the objects we hold, in a hopefully visually pleasing
987 /// Option can be used to select given part only, using the schema :
989 /// Where the stars are wilcards for /key1/key2/.../objectName
991 /// if * is used it is assumed to be a wildcard for objectName
993 /// For other selections the full syntax /*/*/*/*/* must be used.
995 /// Use "-" as objectName to disable object's name output
997 /// One might also use /*/*/*/*/:classname syntax to restrict
998 /// output to only those objects matching a given classname pattern
1001 cout << Form("AliMergeableCollection(%s,%s)[%p] : %d keys and %d objects",
1002 GetName(),GetTitle(),this,
1003 NumberOfKeys(), NumberOfObjects()) << endl;
1005 if (!strlen(option)) return;
1007 TString soption(option);
1009 TObjArray* classes = soption.Tokenize(":");
1011 TRegexp* classPattern(0x0);
1013 if ( classes->GetLast() > 0 )
1015 TString pat = static_cast<TObjString*>(classes->At(1))->String();
1016 classPattern = new TRegexp(pat,kTRUE);
1017 soption = static_cast<TObjString*>(classes->At(0))->String();
1022 TObjArray* select = soption.Tokenize("/");
1024 TString sreObjectName(select->Last()->GetName());
1025 TRegexp reObjectName(sreObjectName.Data(),kTRUE);
1027 TObjArray* identifiers = SortAllIdentifiers();
1029 std::cout << Form("Number of identifiers %d", identifiers->GetEntries()) << std::endl;
1031 TIter nextIdentifier(identifiers);
1033 TObjString* sid(0x0);
1035 while ( ( sid = static_cast<TObjString*>(nextIdentifier()) ) )
1037 Bool_t identifierPrinted(kFALSE);
1039 TString identifier(sid->String());
1041 Bool_t matchPattern = kTRUE;
1042 for ( Int_t isel=0; isel<select->GetLast(); isel++ ) {
1043 if ( ! GetKey(identifier.Data(), isel, kFALSE).Contains(TRegexp(select->At(isel)->GetName(),kTRUE)) ) {
1044 matchPattern = kFALSE;
1048 if ( ! matchPattern ) continue;
1050 if ( sreObjectName == "*" && !classPattern)
1052 identifierPrinted = kTRUE;
1053 cout << identifier.Data() << endl;
1056 THashList * list = static_cast<THashList*>(Map()->GetValue(sid->String().Data()));
1059 names.SetOwner(kTRUE);
1060 TIter nextUnsortedObj(list);
1062 while ( ( obj = nextUnsortedObj() ) )
1064 TString cname(obj->ClassName());
1065 if ( classPattern && !cname.Contains((*classPattern)) )
1069 names.Add(new TObjString(obj->GetName()));
1072 TIter nextObjName(&names);
1074 while ( ( oname = static_cast<TObjString*>(nextObjName()) ) )
1076 TString objName(oname->String());
1077 if (objName.Contains(reObjectName) )
1079 obj = list->FindObject(objName.Data());
1080 if ( IsEmptyObject(obj) && ! fMustShowEmptyObject ) continue;
1082 if (!identifierPrinted)
1084 cout << identifier.Data() << endl;
1085 identifierPrinted = kTRUE;
1089 TString warning(" ");
1091 if ( obj->IsA()->InheritsFrom(TH1::Class()) )
1094 TH1* histo = static_cast<TH1*> (obj);
1095 extra.Form("%s | Entries=%d Sum=%g",histo->GetTitle(),Int_t(histo->GetEntries()),histo->GetSumOfWeights());
1097 else if ( obj->IsA()->InheritsFrom(TGraph::Class()) )
1099 TGraph* graph = static_cast<TGraph*> (obj);
1100 if ( ! TMath::Finite(graph->GetMean(2) ) )
1104 extra.Form("%s | Npts=%d Mean=%g RMS=%g",graph->GetTitle(),graph->GetN(),
1105 graph->GetMean(2),graph->GetRMS(2));
1109 std::cout << Form(" (%s) %s %s", obj->ClassName(),
1113 if ( extra.Length() )
1115 std::cout << " | " << extra.Data();
1117 std::cout << std::endl;
1120 if (!identifierPrinted && sreObjectName=="-" )
1122 // to handle the case where we used objectName="-" to disable showing the objectNames,
1123 // but we still want to see the matching keys maybe...
1124 cout << identifier.Data() << endl;
1133 //_____________________________________________________________________________
1135 AliMergeableCollection::PrintMessages(const char* prefix) const
1137 /// Print pending messages
1139 std::map<std::string,int>::const_iterator it;
1141 for ( it = fMessages.begin(); it != fMessages.end(); ++it )
1143 cout << Form("%s : message %s appeared %5d times",prefix,it->first.c_str(),it->second) << endl;
1148 //_____________________________________________________________________________
1150 AliMergeableCollection::EstimateSize(Bool_t show) const
1152 /// Estimate the memory (in kilobytes) used by some objects
1155 // sizeof(TH1) + (nbins+2)*(nbytes_per_bin) +name+title_sizes
1156 // if you have errors add (nbins+2)*8
1158 TIter next(CreateIterator());
1163 while ( ( obj = next() ) )
1166 if ( obj->IsA()->InheritsFrom(TH1::Class()) || obj->IsA()->InheritsFrom(TProfile::Class()) )
1168 TH1* histo = static_cast<TH1*> (obj);
1169 Int_t nbins = (histo->GetNbinsX()+2);
1171 if (histo->GetNbinsY()>1)
1173 nbins *= (histo->GetNbinsY()+2);
1176 if (histo->GetNbinsZ()>1)
1178 nbins *= (histo->GetNbinsZ()+2);
1181 Bool_t hasErrors = ( histo->GetSumw2N() > 0 );
1183 TString cname(histo->ClassName());
1185 Int_t nbytesPerBin(0);
1187 if (cname.Contains(TRegexp("C$")) ) nbytesPerBin = sizeof(Char_t);
1188 if (cname.Contains(TRegexp("S$")) ) nbytesPerBin = sizeof(Short_t);
1189 if (cname.Contains(TRegexp("I$")) ) nbytesPerBin = sizeof(Int_t);
1190 if (cname.Contains(TRegexp("F$")) ) nbytesPerBin = sizeof(Float_t);
1191 if (cname.Contains(TRegexp("D$")) ) nbytesPerBin = sizeof(Double_t);
1192 if (cname=="TProfile") nbytesPerBin = sizeof(Double_t);
1196 AliError(Form("Could not get the number of bytes per bin for histo %s of class %s. Thus the size estimate will be wrong !",
1197 histo->GetName(),histo->ClassName()));
1201 thissize = sizeof(histo) + nbins*(nbytesPerBin) + strlen(histo->GetName())
1202 + strlen(histo->GetTitle());
1204 if ( hasErrors) thissize += nbins*8;
1206 if ( obj->IsA()->InheritsFrom(TProfile::Class()) )
1208 TProfile* prof = static_cast<TProfile*>(obj);
1209 TArrayD* d = prof->GetBinSumw2();
1210 thissize += d->GetSize()*8*2; // 2 TArrayD
1211 thissize += sizeof(prof) - sizeof(histo);
1214 else if ( obj->IsA()->InheritsFrom(THnSparse::Class()) ) {
1215 THnSparse* sparse = static_cast<THnSparse*> (obj);
1216 thissize = sizeof(Float_t) * (UInt_t)sparse->GetNbins();
1218 // else if ( obj->IsA() == AliCFGridSparse::Class() ) {
1219 // AliCFGridSparse* sparse = static_cast<AliCFGridSparse*> (obj);
1220 // thissize = sizeof(Float_t) * (UInt_t)sparse->GetNFilledBins();
1222 // else if ( obj->IsA() == AliCFContainer::Class() ) {
1223 // AliCFContainer* cont = static_cast<AliCFContainer*> (obj);
1224 // for ( Int_t istep=0; istep<cont->GetNStep(); istep++ ) {
1225 // thissize += sizeof(Float_t) * (UInt_t)cont->GetGrid(istep)->GetNFilledBins();
1229 AliWarning(Form("Cannot estimate size of %s\n", obj->ClassName()));
1237 AliInfo(Form("Size of %30s is %20d bytes",obj->GetName(),thissize));
1239 } // loop on objects
1244 //_____________________________________________________________________________
1245 Int_t AliMergeableCollection::Prune(const char* identifier)
1247 // Delete all objects which match the beginning of the identifier
1248 // returns the number of entries removed from the Map()
1249 // (not to be confused with the number of leaf objects removed)
1256 while ( ( key = static_cast<TObjString*>(next())) )
1258 if (key->String().BeginsWith(identifier))
1260 Bool_t ok = Map()->DeleteEntry(key);
1268 //_____________________________________________________________________________
1269 void AliMergeableCollection::PruneEmptyObjects()
1271 /// Delete the empty objects
1272 /// (Implemented for TH1 only)
1277 toBeRemoved.SetOwner(kTRUE);
1279 while ( ( key = static_cast<TObjString*>(next()) ) )
1281 TString identifier(key->String());
1282 THashList* hlist = static_cast<THashList*>(Map()->GetValue(identifier.Data()));
1283 TIter nextObject(hlist);
1285 while ( ( obj = nextObject() ) )
1287 if ( IsEmptyObject(obj) ) toBeRemoved.Add(new TObjString(Form("%s%s",identifier.Data(),obj->GetName())));
1291 TIter nextTBR(&toBeRemoved);
1292 while ( ( key = static_cast<TObjString*>(nextTBR()) ) )
1294 Remove(key->GetString().Data());
1295 AliDebug(2,Form("Removing %s", key->GetString().Data()));
1299 //_____________________________________________________________________________
1300 AliMergeableCollection*
1301 AliMergeableCollection::Project(const char* identifier) const
1303 /// To be implemented : would create a new collection starting at /key1/key2/...
1305 if (!fMap) return 0x0;
1307 AliMergeableCollection* mergCol = new AliMergeableCollection(Form("%s %s",GetName(),identifier),
1313 while ( ( str = static_cast<TObjString*>(next()) ) )
1315 TString currIdentifier = str->String();
1316 if ( ! currIdentifier.Contains(identifier) ) continue;
1318 THashList* list = static_cast<THashList*>(Map()->GetValue(identifier));
1320 TIter nextObj(list);
1323 while ( ( obj = nextObj()) )
1325 TObject* clone = obj->Clone();
1327 TString newkey(currIdentifier.Data());
1328 newkey.ReplaceAll(identifier,"");
1330 if (newkey=="/") newkey="";
1332 mergCol->InternalAdopt(newkey.Data(),clone);
1339 //_____________________________________________________________________________
1341 AliMergeableCollection::Remove(const char* fullIdentifier)
1344 /// Remove a given object (given its fullIdentifier=/key1/key2/.../objectName)
1346 /// Note that we do *not* remove the /key1/key2/... entry even if there's no
1347 /// more object for this triplet.
1349 /// Not very efficient. Could be improved ?
1352 TString identifier = GetIdentifier(fullIdentifier);
1354 THashList* hlist = dynamic_cast<THashList*>(Map()->GetValue(identifier.Data()));
1358 AliWarning(Form("Could not get hlist for key=%s",identifier.Data()));
1362 TObject* obj = GetObject(fullIdentifier);
1365 AliError(Form("Could not find object %s",fullIdentifier));
1369 TObject* rmObj = hlist->Remove(obj);
1372 AliError("Remove failed");
1379 //_____________________________________________________________________________
1380 Int_t AliMergeableCollection::RemoveByType(const char* typeName)
1382 /// Remove all the objects in this collection that are of a given type
1383 TIter nextIdentifier(Map());
1384 TObjString* identifier;
1387 while ( (identifier = static_cast<TObjString*>(nextIdentifier()) ) )
1389 THashList* list = static_cast<THashList*>(Map()->GetValue(identifier->String()));
1393 while ( ( o = next() ) )
1395 if ( strcmp(o->ClassName(),typeName) == 0 )
1406 //_____________________________________________________________________________
1408 AliMergeableCollection::SortAllIdentifiers() const
1410 /// Sort our internal identifiers. Returned array must be deleted.
1411 TObjArray* identifiers = new TObjArray;
1412 identifiers->SetOwner(kFALSE);
1416 while ( ( sid = static_cast<TObjString*>(next()) ) )
1418 if ( !identifiers->FindObject(sid->String().Data()) )
1420 identifiers->Add(sid);
1423 identifiers->Sort();
1428 ///////////////////////////////////////////////////////////////////////////////
1430 // AliMergeableCollectionIterator
1432 ///////////////////////////////////////////////////////////////////////////////
1434 class AliMergeableCollectionIterator;
1436 //_____________________________________________________________________________
1437 AliMergeableCollectionIterator::AliMergeableCollectionIterator(const AliMergeableCollection* mcol, Bool_t dir)
1438 : fkMergeableCollection(mcol), fMapIterator(0x0), fHashListIterator(0x0), fDirection(dir)
1443 //_____________________________________________________________________________
1444 AliMergeableCollectionIterator&
1445 AliMergeableCollectionIterator::operator=(const TIterator&)
1447 /// Overriden operator= (imposed by Root's declaration of TIterator ?)
1448 Fatal("TIterator::operator=","Not implementeable"); // because there's no clone in TIterator :-(
1452 //_____________________________________________________________________________
1453 AliMergeableCollectionIterator::~AliMergeableCollectionIterator()
1459 //_____________________________________________________________________________
1460 TObject* AliMergeableCollectionIterator::Next()
1462 /// Advance to next object in the collection
1464 if (!fHashListIterator)
1466 if ( !fMapIterator )
1468 fMapIterator = fkMergeableCollection->fMap->MakeIterator(fDirection);
1470 TObjString* key = static_cast<TObjString*>(fMapIterator->Next());
1476 THashList* list = static_cast<THashList*>(fkMergeableCollection->Map()->GetValue(key->String().Data()));
1477 if (!list) return 0x0;
1478 fHashListIterator = list->MakeIterator(fDirection);
1481 TObject* obj = fHashListIterator->Next();
1485 delete fHashListIterator;
1486 fHashListIterator = 0x0;
1493 //_____________________________________________________________________________
1494 void AliMergeableCollectionIterator::Reset()
1496 /// Reset the iterator
1497 delete fHashListIterator;
1498 delete fMapIterator;