]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliAnalysisDataContainer.cxx
- Renaming fEventNumber (and the associated getters/setters) to
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataContainer.cxx
CommitLineData
d3106602 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
c52c2132 45#include <Riostream.h>
46#include <TMethodCall.h>
11026a80 47
c52c2132 48#include <TClass.h>
49#include <TTree.h>
50#include <TROOT.h>
d3106602 51
52#include "AliAnalysisDataContainer.h"
53#include "AliAnalysisDataSlot.h"
54#include "AliAnalysisTask.h"
55
56ClassImp(AliAnalysisDataContainer)
57
58//______________________________________________________________________________
37a26056 59AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
60 fDataReady(kFALSE),
61 fOwnedData(kFALSE),
c52c2132 62 fFileName(),
37a26056 63 fData(NULL),
64 fType(NULL),
65 fProducer(NULL),
66 fConsumers(NULL)
d3106602 67{
c52c2132 68// Dummy ctor.
d3106602 69}
37a26056 70
d3106602 71//______________________________________________________________________________
72AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
37a26056 73 :TNamed(name,""),
74 fDataReady(kFALSE),
75 fOwnedData(kTRUE),
c52c2132 76 fFileName(),
37a26056 77 fData(NULL),
78 fType(type),
79 fProducer(NULL),
80 fConsumers(NULL)
d3106602 81{
c52c2132 82// Default constructor.
83 SetTitle(fType->GetName());
37a26056 84}
85
86//______________________________________________________________________________
87AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
88 :TNamed(cont),
89 fDataReady(cont.fDataReady),
90 fOwnedData(kFALSE),
c52c2132 91 fFileName(cont.fFileName),
37a26056 92 fData(cont.fData),
c52c2132 93 fType(NULL),
37a26056 94 fProducer(cont.fProducer),
95 fConsumers(NULL)
96{
97// Copy ctor.
c52c2132 98 GetType();
37a26056 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 }
d3106602 104}
105
106//______________________________________________________________________________
107AliAnalysisDataContainer::~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
37a26056 114//______________________________________________________________________________
115AliAnalysisDataContainer &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.
c52c2132 122 fFileName = cont.fFileName;
37a26056 123 fData = cont.fData;
c52c2132 124 GetType();
37a26056 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
c52c2132 135//______________________________________________________________________________
136void 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//______________________________________________________________________________
159Bool_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//______________________________________________________________________________
171void 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//______________________________________________________________________________
190TClass *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//______________________________________________________________________________
200void 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//______________________________________________________________________________
220Long64_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//______________________________________________________________________________
260void 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
d3106602 283//______________________________________________________________________________
327eaf46 284Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
d3106602 285{
286// Set the data as READY only if it was published by the producer.
d3106602 287 // If there is no producer declared, this is a top level container.
288 AliAnalysisTask *task;
327eaf46 289 Bool_t init = kFALSE;
d3106602 290 Int_t i, nc;
291 if (!fProducer) {
327eaf46 292 if (data != fData) init = kTRUE;
d3106602 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);
327eaf46 299 task->CheckNotify(init);
d3106602 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;
d3106602 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 {
11026a80 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;
d3106602 320 }
321}
322
d3106602 323//______________________________________________________________________________
324void 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) {
11026a80 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()));
d3106602 330 }
331 if (fDataReady) {
11026a80 332 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
333 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
d3106602 334 return;
335 }
336 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
337 if (!slot) {
11026a80 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));
d3106602 340 return;
341 }
c52c2132 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;
11026a80 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()));
d3106602 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}
d3106602 356