Next step towards supporting multiple primary vertices in ESD. Every primary vertex...
[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>
8d7d3b59 49#include <TFile.h>
c52c2132 50#include <TTree.h>
ca78991b 51#include <TH1.h>
c52c2132 52#include <TROOT.h>
d3106602 53
8d7d3b59 54#include "AliAnalysisManager.h"
d3106602 55#include "AliAnalysisDataContainer.h"
56#include "AliAnalysisDataSlot.h"
57#include "AliAnalysisTask.h"
58
59ClassImp(AliAnalysisDataContainer)
60
61//______________________________________________________________________________
37a26056 62AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
63 fDataReady(kFALSE),
64 fOwnedData(kFALSE),
c52c2132 65 fFileName(),
8d7d3b59 66 fFile(NULL),
37a26056 67 fData(NULL),
68 fType(NULL),
69 fProducer(NULL),
70 fConsumers(NULL)
d3106602 71{
c52c2132 72// Dummy ctor.
d3106602 73}
37a26056 74
d3106602 75//______________________________________________________________________________
76AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
37a26056 77 :TNamed(name,""),
78 fDataReady(kFALSE),
0355fc48 79 fOwnedData(kFALSE),
c52c2132 80 fFileName(),
8d7d3b59 81 fFile(NULL),
37a26056 82 fData(NULL),
83 fType(type),
84 fProducer(NULL),
85 fConsumers(NULL)
d3106602 86{
c52c2132 87// Default constructor.
88 SetTitle(fType->GetName());
37a26056 89}
90
91//______________________________________________________________________________
92AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
93 :TNamed(cont),
94 fDataReady(cont.fDataReady),
95 fOwnedData(kFALSE),
c52c2132 96 fFileName(cont.fFileName),
8d7d3b59 97 fFile(NULL),
37a26056 98 fData(cont.fData),
c52c2132 99 fType(NULL),
37a26056 100 fProducer(cont.fProducer),
101 fConsumers(NULL)
102{
103// Copy ctor.
c52c2132 104 GetType();
37a26056 105 if (cont.fConsumers) {
106 fConsumers = new TObjArray(2);
107 Int_t ncons = cont.fConsumers->GetEntriesFast();
108 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
109 }
d3106602 110}
111
112//______________________________________________________________________________
113AliAnalysisDataContainer::~AliAnalysisDataContainer()
114{
115// Destructor. Deletes data ! (What happens if data is a container ???)
116 if (fData && fOwnedData) delete fData;
117 if (fConsumers) delete fConsumers;
118}
119
120//______________________________________________________________________________
37a26056 121AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
122{
123// Assignment.
124 if (&cont != this) {
125 TNamed::operator=(cont);
126 fDataReady = cont.fDataReady;
127 fOwnedData = kFALSE; // !!! Data owned by cont.
c52c2132 128 fFileName = cont.fFileName;
8d7d3b59 129 fFile = NULL;
37a26056 130 fData = cont.fData;
c52c2132 131 GetType();
37a26056 132 fProducer = cont.fProducer;
133 if (cont.fConsumers) {
134 fConsumers = new TObjArray(2);
135 Int_t ncons = cont.fConsumers->GetEntriesFast();
136 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
137 }
138 }
139 return *this;
140}
141
142//______________________________________________________________________________
c52c2132 143void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
144{
145// Add a consumer for contained data;
146 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
147 if (!slot || !slot->GetType()) {
148 cout<<"Consumer task "<< consumer->GetName()<<" does not have an input/type #"<<islot<<endl;
149 //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
150 return;
151 }
152 if (!slot->GetType()->InheritsFrom(GetType())) {
153 cout<<"Data type "<<slot->GetTitle()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<GetTitle()<<endl;
154 //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()));
155 return;
156 }
157
158 if (!fConsumers) fConsumers = new TObjArray(2);
159 fConsumers->Add(consumer);
160 // Add the consumer task to the list of task of the producer
161 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
162 fProducer->Add(consumer);
163}
164
165//______________________________________________________________________________
166Bool_t AliAnalysisDataContainer::ClientsExecuted() const
167{
168// Check if all client tasks have executed.
169 TIter next(fConsumers);
170 AliAnalysisTask *task;
171 while ((task=(AliAnalysisTask*)next())) {
172 if (!task->HasExecuted()) return kFALSE;
173 }
174 return kTRUE;
175}
176
177//______________________________________________________________________________
178void AliAnalysisDataContainer::DeleteData()
179{
180// Delete data if not needed anymore.
181 if (!fDataReady || !ClientsExecuted()) {
182 cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
183 //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
184 return;
185 }
186 if (!fOwnedData) {
187 cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
188 //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
189 return;
190 }
191 delete fData;
192 fData = 0;
193 fDataReady = kFALSE;
194}
195
196//______________________________________________________________________________
197TClass *AliAnalysisDataContainer::GetType() const
198{
199// Get class type for this slot.
200 AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)this;
201 if (!fType) cont->SetType(gROOT->GetClass(fTitle.Data()));
202 if (!fType) printf("AliAnalysisDataContainer: Unknown class: %s\n", GetTitle());
203 return fType;
204}
205
206//______________________________________________________________________________
207void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
208{
209// If data is ready and derives from TTree or from TBranch, this will get the
210// requested entry in memory if not already loaded.
211 if (!fDataReady || !GetType()) return;
212 Bool_t istree = fType->InheritsFrom(TTree::Class());
213 if (istree) {
214 TTree *tree = (TTree*)fData;
215 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
216 return;
217 }
218 Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
219 if (isbranch) {
220 TBranch *branch = (TBranch*)fData;
221 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
222 return;
223 }
224}
225
226//______________________________________________________________________________
227Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
228{
229// Merge a list of containers with this one. Containers in the list must have
230// data of the same type.
231 if (!list || !fData) return 0;
232 printf("Merging %d containers %s\n", list->GetSize()+1, GetName());
233 TMethodCall callEnv;
234 if (fData->IsA())
235 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
236 if (!callEnv.IsValid() && !list->IsEmpty()) {
237 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
238 return 1;
37153431 239 }
240
c52c2132 241 if (list->IsEmpty()) return 1;
242
243 TIter next(list);
37153431 244 AliAnalysisDataContainer *cont;
c52c2132 245 // Make a list where to temporary store the data to be merged.
246 TList *collectionData = new TList();
247 Int_t count = 0; // object counter
248 while ((cont=(AliAnalysisDataContainer*)next())) {
249 TObject *data = cont->GetData();
250 if (!data) continue;
251 if (strcmp(cont->GetName(), GetName())) {
252 cout << "Not merging containers with different names !" << endl;
253 continue;
254 }
255 printf(" ... merging object %s\n", data->GetName());
256 collectionData->Add(data);
257 count++;
37153431 258 }
c52c2132 259 callEnv.SetParam((Long_t) collectionData);
260 callEnv.Execute(fData);
261 delete collectionData;
37153431 262
263 return count+1;
c52c2132 264}
265
266//______________________________________________________________________________
267void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
268{
269// Print info about this container.
270 TString ind;
271 for (Int_t i=0; i<indent; i++) ind += " ";
272 TString opt(option);
273 opt.ToLower();
274 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
275 if (!dep) {
b1310ef5 276 printf("%sContainer: %s type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
c52c2132 277 if (fProducer)
12856ea6 278 printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
c52c2132 279 else
12856ea6 280 printf("%s= No data producer",ind.Data());
281 printf("%s = Consumer tasks: ", ind.Data());
c52c2132 282 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
283 else printf("\n");
37153431 284 }
285 printf("Filename: %s\n", fFileName.Data());
c52c2132 286 TIter next(fConsumers);
287 AliAnalysisTask *task;
288 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
289}
290
291//______________________________________________________________________________
327eaf46 292Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
d3106602 293{
294// Set the data as READY only if it was published by the producer.
d3106602 295 // If there is no producer declared, this is a top level container.
296 AliAnalysisTask *task;
327eaf46 297 Bool_t init = kFALSE;
d3106602 298 Int_t i, nc;
299 if (!fProducer) {
327eaf46 300 if (data != fData) init = kTRUE;
d3106602 301 fData = data;
302 fDataReady = kTRUE;
303 if (fConsumers) {
304 nc = fConsumers->GetEntriesFast();
305 for (i=0; i<nc; i++) {
306 task = (AliAnalysisTask*)fConsumers->At(i);
327eaf46 307 task->CheckNotify(init);
d3106602 308 }
309 }
310 return kTRUE;
37153431 311 }
d3106602 312 // Check if it is the producer who published the data
313 if (fProducer->GetPublishedData()==data) {
314 fData = data;
315 fDataReady = kTRUE;
d3106602 316 if (fConsumers) {
317 nc = fConsumers->GetEntriesFast();
318 for (i=0; i<nc; i++) {
319 task = (AliAnalysisTask*)fConsumers->At(i);
320 task->CheckNotify();
321 }
322 }
323 return kTRUE;
324 } else {
11026a80 325 cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
326 //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));
327 return kFALSE;
d3106602 328 }
329}
330
331//______________________________________________________________________________
d3106602 332void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
333{
334// Set the producer of data. The slot number is required for data type checking.
335 if (fProducer) {
11026a80 336 cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
337 //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
d3106602 338 }
339 if (fDataReady) {
11026a80 340 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
341 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
d3106602 342 return;
343 }
344 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
345 if (!slot) {
11026a80 346 cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
347 //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
d3106602 348 return;
349 }
c52c2132 350 if (!slot->GetType()->InheritsFrom(GetType())) {
351 cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
11026a80 352 //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 353 return;
354 }
355
356 fProducer = prod;
357 // Add all consumers as daughter tasks
358 TIter next(fConsumers);
359 AliAnalysisTask *cons;
360 while ((cons=(AliAnalysisTask*)next())) {
361 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
362 }
363}
981f2614 364
365//______________________________________________________________________________
366AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
367{
368// Wraps data for sending it through the net.
369 AliAnalysisDataWrapper *pack = 0;
8d7d3b59 370 if (!fData) {
371 Error("ExportData", "Container %s - No data to be wrapped !", GetName());
372 return pack;
373 }
374 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
375 if (mgr->GetDebugLevel() > 1) printf(" ExportData: Wrapping data %s for container %s\n", fData->GetName(),GetName());
981f2614 376 pack = new AliAnalysisDataWrapper(fData);
377 pack->SetName(fName.Data());
378 return pack;
379}
380
381//______________________________________________________________________________
382void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
383{
384// Unwraps data from a data wrapper.
385 if (pack) {
386 fData = pack->Data();
8d7d3b59 387 if (!fData) {
388 Error("ImportData", "No data was wrapped for container %s", GetName());
389 return;
390 }
391 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
392 if (mgr->GetDebugLevel() > 1) printf(" ImportData: Unwrapping data %s for container %s\n", fData->GetName(),GetName());
981f2614 393 fDataReady = kTRUE;
8167b1d0 394 // Imported wrappers do not own data anymore (AG 13-11-07)
395 pack->SetDeleteData(kFALSE);
981f2614 396 }
397}
d3106602 398
981f2614 399ClassImp (AliAnalysisDataWrapper)
400
401//______________________________________________________________________________
8167b1d0 402AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
403 :TNamed(),
404 fData(data)
405{
406// Ctor.
407 if (data) SetName(data->GetName());
408}
409
410//______________________________________________________________________________
411AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
412{
413// Dtor.
414 if (fData && TObject::TestBit(kDeleteData)) delete fData;
415}
416
417//______________________________________________________________________________
981f2614 418AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
419{
420// Assignment.
421 if (&other != this) {
422 TNamed::operator=(other);
423 fData = other.fData;
424 }
425 return *this;
426}
427
428//______________________________________________________________________________
429Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
430{
431// Merge a list of containers with this one. Containers in the list must have
432// data of the same type.
ca78991b 433 if (TH1::AddDirectoryStatus()) TH1::AddDirectory(kFALSE);
981f2614 434 if (!fData) return 0;
435 if (!list || list->IsEmpty()) return 1;
436
b3694163 437 SetDeleteData();
438
981f2614 439 TMethodCall callEnv;
981f2614 440 if (fData->IsA())
441 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
442 if (!callEnv.IsValid()) {
443 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
444 return 1;
445 }
446
8eedd442 447 TIter next1(list);
981f2614 448 AliAnalysisDataWrapper *cont;
449 // Make a list where to temporary store the data to be merged.
450 TList *collectionData = new TList();
451 Int_t count = 0; // object counter
ca78991b 452 // printf("Wrapper %s 0x%lx (data=%s) merged with:\n", GetName(), (ULong_t)this, fData->ClassName());
8eedd442 453 while ((cont=(AliAnalysisDataWrapper*)next1())) {
b3694163 454 cont->SetDeleteData();
981f2614 455 TObject *data = cont->Data();
456 if (!data) continue;
ca78991b 457 // printf(" - %s 0x%lx (data=%s)\n", cont->GetName(), (ULong_t)cont, data->ClassName());
981f2614 458 collectionData->Add(data);
459 count++;
460 }
461 callEnv.SetParam((Long_t) collectionData);
462 callEnv.Execute(fData);
463 delete collectionData;
464
465 return count+1;
466}