]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisDataContainer.cxx
Modifying the GetChainFromCollection function based on the additions of the TEntryList
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataContainer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /* $Id$ */
17 // Author: Andrei Gheata, 31/05/2006
18
19 //==============================================================================
20 //   AliAnalysysDataContainer - Container of data of arbitrary type deriving
21 //      from TObject used for analysis. A container must be connected to the 
22 //      output data slot of a single analysis task (producer) , but also as 
23 //      input slot for possibly several other tasks (consumers). The connected 
24 //      slots must enforce the same data type as the container (or a derived type).
25 //      A container becomes the owner of the contained data once this was produced.
26 //
27 // Containers should be defined by the analysis module using:
28 //
29 //   AliAnalysisModule::AddContainer(const char *name, TClass *type);
30 //
31 // A container should be connected to a producer:
32
33 //   AliAnalysisModule::ConnectOutput(AliAnalysisTask *task,
34 //                                    AliAnalysisDataContainer *cont)
35 // and to its consumers:
36 //
37 //   AliAnalysisModule::ConnectInput(AliAnalysisTask *task, Int_t islot,
38 //                                   AliAnalysisDataContainer *cont)
39 //
40 // The container will create an implicit connection between the producer task 
41 // and all consumers, which will become sub-tasks of the producer.
42 //
43 //==============================================================================
44
45 #include <Riostream.h>
46 #include <TMethodCall.h>
47
48 #include <TClass.h>
49 #include <TTree.h>
50 #include <TROOT.h>
51
52 #include "AliAnalysisDataContainer.h"
53 #include "AliAnalysisDataSlot.h"
54 #include "AliAnalysisTask.h"
55
56 ClassImp(AliAnalysisDataContainer)
57
58 //______________________________________________________________________________
59 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
60                           fDataReady(kFALSE),
61                           fOwnedData(kFALSE),
62                           fFileName(),
63                           fData(NULL),
64                           fType(NULL),
65                           fProducer(NULL),
66                           fConsumers(NULL)
67 {
68 // Dummy ctor.
69 }
70
71 //______________________________________________________________________________
72 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
73                          :TNamed(name,""),
74                           fDataReady(kFALSE),
75                           fOwnedData(kTRUE),
76                           fFileName(),
77                           fData(NULL),
78                           fType(type),
79                           fProducer(NULL),
80                           fConsumers(NULL)
81 {
82 // Default constructor.
83    SetTitle(fType->GetName());
84 }
85
86 //______________________________________________________________________________
87 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
88                          :TNamed(cont),
89                           fDataReady(cont.fDataReady),
90                           fOwnedData(kFALSE),
91                           fFileName(cont.fFileName),
92                           fData(cont.fData),
93                           fType(NULL),
94                           fProducer(cont.fProducer),
95                           fConsumers(NULL)
96 {
97 // Copy ctor.
98    GetType();
99    if (cont.fConsumers) {
100       fConsumers = new TObjArray(2);
101       Int_t ncons = cont.fConsumers->GetEntriesFast();
102       for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
103    }   
104 }
105
106 //______________________________________________________________________________
107 AliAnalysisDataContainer::~AliAnalysisDataContainer()
108 {
109 // Destructor. Deletes data ! (What happens if data is a container ???)
110    if (fData && fOwnedData) delete fData;
111    if (fConsumers) delete fConsumers;
112 }
113
114 //______________________________________________________________________________
115 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
116 {
117 // Assignment.
118    if (&cont != this) {
119       TNamed::operator=(cont);
120       fDataReady = cont.fDataReady;
121       fOwnedData = kFALSE;  // !!! Data owned by cont.
122       fFileName = cont.fFileName;
123       fData = cont.fData;
124       GetType();
125       fProducer = cont.fProducer;
126       if (cont.fConsumers) {
127          fConsumers = new TObjArray(2);
128          Int_t ncons = cont.fConsumers->GetEntriesFast();
129          for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
130       }   
131    }   
132    return *this;
133 }      
134
135 //______________________________________________________________________________
136 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
137 {
138 // Add a consumer for contained data;
139    AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
140    if (!slot || !slot->GetType()) {
141      cout<<"Consumer task "<< consumer->GetName()<<" does not have an input/type #"<<islot<<endl;
142      //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
143       return;
144    }
145    if (!slot->GetType()->InheritsFrom(GetType())) {
146      cout<<"Data type "<<slot->GetTitle()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<GetTitle()<<endl;  
147      //AliError(Form("Data type %s for input slot %i of task %s does not match container type %s", slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
148       return;
149    }   
150
151    if (!fConsumers) fConsumers = new TObjArray(2);   
152    fConsumers->Add(consumer);
153    // Add the consumer task to the list of task of the producer
154    if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer)) 
155       fProducer->Add(consumer);
156 }      
157
158 //______________________________________________________________________________
159 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
160 {
161 // Check if all client tasks have executed.
162    TIter next(fConsumers);
163    AliAnalysisTask *task;
164    while ((task=(AliAnalysisTask*)next())) {
165       if (!task->HasExecuted()) return kFALSE;
166    }
167    return kTRUE;
168 }   
169
170 //______________________________________________________________________________
171 void AliAnalysisDataContainer::DeleteData()
172 {
173 // Delete data if not needed anymore.
174    if (!fDataReady || !ClientsExecuted()) {
175      cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
176      //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
177       return;
178    }
179    if (!fOwnedData) {
180      cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
181      //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
182       return;
183    }
184    delete fData;
185    fData = 0;
186    fDataReady = kFALSE;
187 }   
188
189 //______________________________________________________________________________
190 TClass *AliAnalysisDataContainer::GetType() const
191 {
192 // Get class type for this slot.
193    AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)this;
194    if (!fType) cont->SetType(gROOT->GetClass(fTitle.Data()));
195    if (!fType) printf("AliAnalysisDataContainer: Unknown class: %s\n", GetTitle());
196    return fType;
197 }
198
199 //______________________________________________________________________________
200 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
201 {
202 // If data is ready and derives from TTree or from TBranch, this will get the
203 // requested entry in memory if not already loaded.
204    if (!fDataReady || !GetType()) return;
205    Bool_t istree = fType->InheritsFrom(TTree::Class());
206    if (istree) {
207       TTree *tree = (TTree*)fData;
208       if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
209       return;
210    }   
211    Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
212    if (isbranch) {
213       TBranch *branch = (TBranch*)fData;
214       if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
215       return;
216    }   
217 }   
218
219 //______________________________________________________________________________
220 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
221 {
222 // Merge a list of containers with this one. Containers in the list must have
223 // data of the same type.
224    if (!list || !fData) return 0;
225    printf("Merging %d containers %s\n", list->GetSize()+1, GetName());
226    TMethodCall callEnv;
227    if (fData->IsA())
228       callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
229    if (!callEnv.IsValid() && !list->IsEmpty()) {
230       cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
231       return 1;
232    }   
233    
234    if (list->IsEmpty()) return 1;
235
236    TIter next(list);
237    AliAnalysisDataContainer *cont;   
238    // Make a list where to temporary store the data to be merged.
239    TList *collectionData = new TList();
240    Int_t count = 0; // object counter
241    while ((cont=(AliAnalysisDataContainer*)next())) {
242       TObject *data = cont->GetData();
243       if (!data) continue;
244       if (strcmp(cont->GetName(), GetName())) {
245          cout << "Not merging containers with different names !" << endl;
246          continue;
247       }
248       printf(" ... merging object %s\n", data->GetName());
249       collectionData->Add(data);
250       count++;
251    }   
252    callEnv.SetParam((Long_t) collectionData);
253    callEnv.Execute(fData);
254    delete collectionData;
255    
256    return count+1;   
257 }
258
259 //______________________________________________________________________________
260 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
261 {
262 // Print info about this container.
263    TString ind;
264    for (Int_t i=0; i<indent; i++) ind += " ";
265    TString opt(option);
266    opt.ToLower();
267    Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
268    if (!dep) {
269       printf("%s\n", Form("%sContainer: %s  type: %s", ind.Data(), GetName(), GetTitle()));
270       if (fProducer) 
271          printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName()));
272       else
273          printf("%s\n", Form("%s= No data producer"));
274       printf("%s", Form("%s = Consumer tasks: "));
275       if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
276       else printf("\n");
277    }   
278    TIter next(fConsumers);
279    AliAnalysisTask *task;
280    while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
281 }   
282
283 //______________________________________________________________________________
284 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
285 {
286 // Set the data as READY only if it was published by the producer.
287    // If there is no producer declared, this is a top level container.
288    AliAnalysisTask *task;
289    Bool_t init = kFALSE;
290    Int_t i, nc;
291    if (!fProducer) {
292       if (data != fData) init = kTRUE;
293       fData = data;
294       fDataReady = kTRUE;
295       if (fConsumers) {
296          nc = fConsumers->GetEntriesFast();
297          for (i=0; i<nc; i++) {
298             task = (AliAnalysisTask*)fConsumers->At(i);
299             task->CheckNotify(init);
300          }
301       }      
302       return kTRUE;
303    } 
304    // Check if it is the producer who published the data     
305    if (fProducer->GetPublishedData()==data) {
306       fData = data;
307       fDataReady = kTRUE;
308       if (fConsumers) {
309          nc = fConsumers->GetEntriesFast();
310          for (i=0; i<nc; i++) {
311             task = (AliAnalysisTask*)fConsumers->At(i);
312             task->CheckNotify();
313          }
314       }      
315       return kTRUE;   
316    } else {
317      cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
318      //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));   
319      return kFALSE;           
320    }              
321 }
322
323 //______________________________________________________________________________
324 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
325 {
326 // Set the producer of data. The slot number is required for data type checking.
327    if (fProducer) {
328      cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
329      //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
330    } 
331    if (fDataReady) {
332      cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
333      //AliError(Form("%s container contains data - cannot change producer!", GetName()));
334       return;
335    }   
336    AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
337    if (!slot) {
338      cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
339      //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
340       return;
341    }   
342    if (!slot->GetType()->InheritsFrom(GetType())) {
343      cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
344      //AliError(Form("Data type %s for output slot %i of task %s does not match container type %s", slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
345       return;
346    }   
347    
348    fProducer = prod;
349    // Add all consumers as daughter tasks
350    TIter next(fConsumers);
351    AliAnalysisTask *cons;
352    while ((cons=(AliAnalysisTask*)next())) {
353       if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
354    }   
355 }   
356