]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/background/AliHistoListWrapper.cxx
Geometry for MFT (Brigitte)
[u/mrichter/AliRoot.git] / PWGPP / background / AliHistoListWrapper.cxx
1 #include "AliHistoListWrapper.h"
2 #include "TH2F.h"
3 #include "TList.h"
4 #include "AliLog.h"
5 #include "TString.h"
6 #include "AliESDInputHandlerRP.h"
7 #include "AliAnalysisManager.h"
8 #include "TTree.h"
9 #include "AliMultiplicity.h"
10
11 ClassImp(AliHistoListWrapper)
12
13 AliHistoListWrapper::AliHistoListWrapper():
14   TNamed(), fList(0)
15 {
16   
17   // constructor
18   fList = new TList();
19   fList->SetOwner();
20
21 }
22
23 AliHistoListWrapper::AliHistoListWrapper(const char* name, const char* title):
24   TNamed(name,title), fList(0)
25 {
26   // constructor
27
28   fList = new TList();
29   fList->SetOwner();
30
31 }
32
33 AliHistoListWrapper::AliHistoListWrapper(const AliHistoListWrapper& obj) : 
34   TNamed(obj),
35   fList(0)
36 {
37   // Copy ctor
38   fList  = obj.fList;
39 }
40
41 AliHistoListWrapper::~AliHistoListWrapper() {
42   // Destructor
43   if(fList) {
44     delete fList;
45     fList = 0;
46   }
47
48 }
49
50 Long64_t AliHistoListWrapper::Merge(TCollection* list)
51 {
52   // Merge a list of AliHistoListWrapper objects with this.
53   // Returns the number of merged objects (including this).
54
55   // We have to make sure that all the list contain the same histos in
56   // the same order. We thus also have to sort the list (sorting is
57   // done by name in TList).
58
59   //  AliInfo("Merging");
60
61   if (!list)
62     return 0;
63
64   if (list->IsEmpty())
65     return 1;
66
67   TIterator* iter = list->MakeIterator();
68   TObject* obj;
69
70   // collections of all histograms
71   TList collections;
72
73   Int_t count = 0;
74
75   while ((obj = iter->Next())) {
76     Bool_t foundDiffinThisIterStep = kFALSE;
77
78     //    Printf("%d - %s",count, obj->GetName());
79     AliHistoListWrapper* entry = dynamic_cast<AliHistoListWrapper*> (obj);
80     if (entry == 0) 
81       continue;
82
83     TList * hlist = entry->fList;
84
85     // Check if all histos in this fList are also in the one from entry and viceversa
86     // Use getters to automatically book non defined histos    
87
88     Bool_t areListsDifferent=kTRUE;
89     Int_t iloop = 0;
90     Int_t max_loops = hlist->GetSize() + fList->GetSize(); // In the worst case all of the histos will be different...
91     while(areListsDifferent) {
92       if(iloop>max_loops) AliFatal("Infinite Loop?");
93       iloop++;
94       // sort
95       hlist->Sort();
96       fList->Sort();
97       // loop over the largest 
98       TObject * hist =0;
99       TIterator * iterlist = 0;
100       TList * thislist  = 0; // the list over which I'm iterating
101       TList * otherlist = 0; // the other
102
103       if (hlist->GetSize() >= fList->GetSize()) { 
104         thislist  = hlist;
105         otherlist = fList;
106       }
107       else{
108         thislist  = fList;
109         otherlist = hlist;      
110       }
111       iterlist = thislist->MakeIterator();
112
113       while ((hist= iterlist->Next())){ 
114         if(!otherlist->FindObject(hist->GetName())){
115           AliInfo(Form("Adding object %s",hist->GetName()));      
116           TH1 * hclone =  (TH1*) hist->Clone();
117           if (!hclone->InheritsFrom("TH1")) AliFatal(Form("Found a %s. This class only supports objects inheriting from TH1",hclone->ClassName()));
118           hclone->Reset();
119           otherlist->Add(hclone);
120           foundDiffinThisIterStep=kTRUE;
121         }
122       }
123
124       // re-sort before checking
125       hlist->Sort();
126       fList->Sort();
127
128       // check if everything is fine    
129       areListsDifferent=kFALSE;
130       if (hlist->GetSize() == fList->GetSize()) {       
131         Int_t nhist =  fList->GetSize();
132         for(Int_t ihist = 0; ihist < nhist; ihist++){
133           if(strcmp(fList->At(ihist)->GetName(),hlist->At(ihist)->GetName())) areListsDifferent = kTRUE;
134         }
135       } else {
136         areListsDifferent=kTRUE;
137       }
138     }
139
140     // last check: if something is not ok die loudly 
141     if (hlist->GetSize() != fList->GetSize()) {
142       AliFatal("Mismatching size!");
143     }
144     Int_t nhist =  fList->GetSize();
145     for(Int_t ihist = 0; ihist < nhist; ihist++){
146       if(strcmp(fList->At(ihist)->GetName(),hlist->At(ihist)->GetName())){
147         AliFatal(Form("Mismatching histos: %s -> %s", fList->At(ihist)->GetName(),hlist->At(ihist)->GetName()));
148       }
149     }
150     
151     if (foundDiffinThisIterStep){
152       iter->Reset(); // We found a difference: previous lists could
153                      // also be affected... We start from scratch
154       collections.Clear();
155       count = 0;
156     }
157     else {
158       
159       collections.Add(hlist);
160       
161       count++;
162     }
163   }
164
165   fList->Merge(&collections);
166   
167   delete iter;
168
169   return count+1;
170 }
171
172
173 AliHistoListWrapper& AliHistoListWrapper::operator=(const AliHistoListWrapper& wrap) {
174
175   // Assignment operator
176   if(this!=&wrap) {
177     
178     fList = new TList();
179     fList->SetOwner();
180     TIterator* iter = wrap.fList->MakeIterator();
181     TObject* obj;
182
183     while ((obj = iter->Next())) {
184       fList->Add(obj->Clone());
185     }
186
187   }
188   return *this;
189 }
190