]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliAnalysisDataContainer.cxx
Updating macro to check the various methods of the AliMUONMCDataInterface
[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;
37153431 232 }
233
c52c2132 234 if (list->IsEmpty()) return 1;
235
236 TIter next(list);
37153431 237 AliAnalysisDataContainer *cont;
c52c2132 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++;
37153431 251 }
c52c2132 252 callEnv.SetParam((Long_t) collectionData);
253 callEnv.Execute(fData);
254 delete collectionData;
37153431 255
256 return count+1;
c52c2132 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) {
b1310ef5 269 printf("%sContainer: %s type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
c52c2132 270 if (fProducer)
12856ea6 271 printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
c52c2132 272 else
12856ea6 273 printf("%s= No data producer",ind.Data());
274 printf("%s = Consumer tasks: ", ind.Data());
c52c2132 275 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
276 else printf("\n");
37153431 277 }
278 printf("Filename: %s\n", fFileName.Data());
c52c2132 279 TIter next(fConsumers);
280 AliAnalysisTask *task;
281 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
282}
283
d3106602 284//______________________________________________________________________________
327eaf46 285Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
d3106602 286{
287// Set the data as READY only if it was published by the producer.
d3106602 288 // If there is no producer declared, this is a top level container.
289 AliAnalysisTask *task;
327eaf46 290 Bool_t init = kFALSE;
d3106602 291 Int_t i, nc;
292 if (!fProducer) {
327eaf46 293 if (data != fData) init = kTRUE;
d3106602 294 fData = data;
295 fDataReady = kTRUE;
296 if (fConsumers) {
297 nc = fConsumers->GetEntriesFast();
298 for (i=0; i<nc; i++) {
299 task = (AliAnalysisTask*)fConsumers->At(i);
327eaf46 300 task->CheckNotify(init);
d3106602 301 }
302 }
303 return kTRUE;
37153431 304 }
d3106602 305 // Check if it is the producer who published the data
306 if (fProducer->GetPublishedData()==data) {
307 fData = data;
308 fDataReady = kTRUE;
d3106602 309 if (fConsumers) {
310 nc = fConsumers->GetEntriesFast();
311 for (i=0; i<nc; i++) {
312 task = (AliAnalysisTask*)fConsumers->At(i);
313 task->CheckNotify();
314 }
315 }
316 return kTRUE;
317 } else {
11026a80 318 cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
319 //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));
320 return kFALSE;
d3106602 321 }
322}
323
d3106602 324//______________________________________________________________________________
325void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
326{
327// Set the producer of data. The slot number is required for data type checking.
328 if (fProducer) {
11026a80 329 cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
330 //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
d3106602 331 }
332 if (fDataReady) {
11026a80 333 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
334 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
d3106602 335 return;
336 }
337 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
338 if (!slot) {
11026a80 339 cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
340 //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
d3106602 341 return;
342 }
c52c2132 343 if (!slot->GetType()->InheritsFrom(GetType())) {
344 cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
11026a80 345 //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 346 return;
347 }
348
349 fProducer = prod;
350 // Add all consumers as daughter tasks
351 TIter next(fConsumers);
352 AliAnalysisTask *cons;
353 while ((cons=(AliAnalysisTask*)next())) {
354 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
355 }
356}
981f2614 357
358//______________________________________________________________________________
359AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
360{
361// Wraps data for sending it through the net.
362 AliAnalysisDataWrapper *pack = 0;
363 if (!fData) return pack;
364 pack = new AliAnalysisDataWrapper(fData);
365 pack->SetName(fName.Data());
366 return pack;
367}
368
369//______________________________________________________________________________
370void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
371{
372// Unwraps data from a data wrapper.
373 if (pack) {
374 fData = pack->Data();
375 fDataReady = kTRUE;
8167b1d0 376 // Imported wrappers do not own data anymore (AG 13-11-07)
377 pack->SetDeleteData(kFALSE);
981f2614 378 }
379}
d3106602 380
981f2614 381ClassImp (AliAnalysisDataWrapper)
382
8167b1d0 383//______________________________________________________________________________
384AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
385 :TNamed(),
386 fData(data)
387{
388// Ctor.
389 if (data) SetName(data->GetName());
390}
391
392//______________________________________________________________________________
393AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
394{
395// Dtor.
396 if (fData && TObject::TestBit(kDeleteData)) delete fData;
397}
398
981f2614 399//______________________________________________________________________________
400AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
401{
402// Assignment.
403 if (&other != this) {
404 TNamed::operator=(other);
405 fData = other.fData;
406 }
407 return *this;
408}
409
410//______________________________________________________________________________
411Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
412{
413// Merge a list of containers with this one. Containers in the list must have
414// data of the same type.
415 if (!fData) return 0;
416 if (!list || list->IsEmpty()) return 1;
417
981f2614 418 TMethodCall callEnv;
419 if (fData->InheritsFrom(TSeqCollection::Class())) {
8167b1d0 420// printf("Collection wrapper %s 0x%lx (owner=%d) merge with:\n", GetName(), (ULong_t)this, TObject::TestBit(kDeleteData));
981f2614 421 TSeqCollection *coll = (TSeqCollection*)fData;
422 if (coll->IsEmpty()) return 0;
423 Int_t nentries = coll->GetEntries();
8eedd442 424 AliAnalysisDataWrapper *top, *crt;
981f2614 425 TIter next(list);
426 TSeqCollection *collcrt = 0;
427 TList *list1 = 0;
8eedd442 428 // Loop entries of the collection attached to this wrapper.
981f2614 429 for (Int_t i=0; i<nentries; i++) {
430 list1 = new TList();
431 top = new AliAnalysisDataWrapper(coll->At(i));
432 next.Reset();
8eedd442 433 // Loop wrappers coming in the 'to merge with' list
434 while ((crt=(AliAnalysisDataWrapper*)next())) {
8167b1d0 435// printf(" %s 0x%lx (owner=%d)\n", crt->GetName(), (ULong_t)crt, crt->TestBit(AliAnalysisDataWrapper::kDeleteData));
8eedd442 436 collcrt = (TSeqCollection*)crt->Data();
981f2614 437 list1->Add(new AliAnalysisDataWrapper(collcrt->At(i)));
8eedd442 438 }
439 // Now merge 'top' wrapper with 'list1'. This may go recursively.
440 top->Merge(list1);
441 delete top;
981f2614 442 list1->Delete();
443 delete list1;
444 }
445 return nentries;
446 }
447
448 if (fData->IsA())
449 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
450 if (!callEnv.IsValid()) {
451 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
452 return 1;
453 }
454
8eedd442 455 TIter next1(list);
981f2614 456 AliAnalysisDataWrapper *cont;
457 // Make a list where to temporary store the data to be merged.
458 TList *collectionData = new TList();
459 Int_t count = 0; // object counter
8167b1d0 460// printf("Wrapper %s 0x%lx (owner=%d) merged with:\n", GetName(), (ULong_t)this, TObject::TestBit(kDeleteData));
8eedd442 461 while ((cont=(AliAnalysisDataWrapper*)next1())) {
8167b1d0 462// printf(" %s 0x%lx (owner=%d)\n", cont->GetName(), (ULong_t)cont, cont->TestBit(AliAnalysisDataWrapper::kDeleteData));
981f2614 463 TObject *data = cont->Data();
464 if (!data) continue;
981f2614 465 collectionData->Add(data);
466 count++;
467 }
468 callEnv.SetParam((Long_t) collectionData);
469 callEnv.Execute(fData);
470 delete collectionData;
471
472 return count+1;
473}