1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // Author: Andrei Gheata, 31/05/2006
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.
27 // Containers should be defined by the analysis module using:
29 // AliAnalysisModule::AddContainer(const char *name, TClass *type);
31 // A container should be connected to a producer:
33 // AliAnalysisModule::ConnectOutput(AliAnalysisTask *task,
34 // AliAnalysisDataContainer *cont)
35 // and to its consumers:
37 // AliAnalysisModule::ConnectInput(AliAnalysisTask *task, Int_t islot,
38 // AliAnalysisDataContainer *cont)
40 // The container will create an implicit connection between the producer task
41 // and all consumers, which will become sub-tasks of the producer.
43 //==============================================================================
45 #include <Riostream.h>
46 #include <TMethodCall.h>
52 #include "AliAnalysisDataContainer.h"
53 #include "AliAnalysisDataSlot.h"
54 #include "AliAnalysisTask.h"
56 ClassImp(AliAnalysisDataContainer)
58 //______________________________________________________________________________
59 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
71 //______________________________________________________________________________
72 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
82 // Default constructor.
83 SetTitle(fType->GetName());
86 //______________________________________________________________________________
87 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
89 fDataReady(cont.fDataReady),
91 fFileName(cont.fFileName),
94 fProducer(cont.fProducer),
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));
106 //______________________________________________________________________________
107 AliAnalysisDataContainer::~AliAnalysisDataContainer()
109 // Destructor. Deletes data ! (What happens if data is a container ???)
110 if (fData && fOwnedData) delete fData;
111 if (fConsumers) delete fConsumers;
114 //______________________________________________________________________________
115 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
119 TNamed::operator=(cont);
120 fDataReady = cont.fDataReady;
121 fOwnedData = kFALSE; // !!! Data owned by cont.
122 fFileName = cont.fFileName;
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));
135 //______________________________________________________________________________
136 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
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));
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()));
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);
158 //______________________________________________________________________________
159 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
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;
170 //______________________________________________________________________________
171 void AliAnalysisDataContainer::DeleteData()
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()));
180 cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
181 //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
189 //______________________________________________________________________________
190 TClass *AliAnalysisDataContainer::GetType() const
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());
199 //______________________________________________________________________________
200 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
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());
207 TTree *tree = (TTree*)fData;
208 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
211 Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
213 TBranch *branch = (TBranch*)fData;
214 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
219 //______________________________________________________________________________
220 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
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());
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;
234 if (list->IsEmpty()) return 1;
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();
244 if (strcmp(cont->GetName(), GetName())) {
245 cout << "Not merging containers with different names !" << endl;
248 printf(" ... merging object %s\n", data->GetName());
249 collectionData->Add(data);
252 callEnv.SetParam((Long_t) collectionData);
253 callEnv.Execute(fData);
254 delete collectionData;
259 //______________________________________________________________________________
260 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
262 // Print info about this container.
264 for (Int_t i=0; i<indent; i++) ind += " ";
267 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
269 printf("%sContainer: %s type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
271 printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
273 printf("%s= No data producer",ind.Data());
274 printf("%s = Consumer tasks: ", ind.Data());
275 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
278 printf("Filename: %s\n", fFileName.Data());
279 TIter next(fConsumers);
280 AliAnalysisTask *task;
281 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
284 //______________________________________________________________________________
285 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
287 // Set the data as READY only if it was published by the producer.
288 // If there is no producer declared, this is a top level container.
289 AliAnalysisTask *task;
290 Bool_t init = kFALSE;
293 if (data != fData) init = kTRUE;
297 nc = fConsumers->GetEntriesFast();
298 for (i=0; i<nc; i++) {
299 task = (AliAnalysisTask*)fConsumers->At(i);
300 task->CheckNotify(init);
305 // Check if it is the producer who published the data
306 if (fProducer->GetPublishedData()==data) {
310 nc = fConsumers->GetEntriesFast();
311 for (i=0; i<nc; i++) {
312 task = (AliAnalysisTask*)fConsumers->At(i);
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()));
324 //______________________________________________________________________________
325 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
327 // Set the producer of data. The slot number is required for data type checking.
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()));
333 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
334 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
337 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
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));
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;
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()));
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);
358 //______________________________________________________________________________
359 AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
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());
369 //______________________________________________________________________________
370 void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
372 // Unwraps data from a data wrapper.
374 fData = pack->Data();
376 // Imported wrappers do not own data anymore (AG 13-11-07)
377 pack->SetDeleteData(kFALSE);
381 ClassImp (AliAnalysisDataWrapper)
383 //______________________________________________________________________________
384 AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
389 if (data) SetName(data->GetName());
392 //______________________________________________________________________________
393 AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
396 if (fData && TObject::TestBit(kDeleteData)) delete fData;
399 //______________________________________________________________________________
400 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
403 if (&other != this) {
404 TNamed::operator=(other);
410 //______________________________________________________________________________
411 Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
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;
419 if (fData->InheritsFrom(TSeqCollection::Class())) {
420 // printf("Collection wrapper %s 0x%lx (owner=%d) merge with:\n", GetName(), (ULong_t)this, TObject::TestBit(kDeleteData));
421 TSeqCollection *coll = (TSeqCollection*)fData;
422 if (coll->IsEmpty()) return 0;
423 Int_t nentries = coll->GetEntries();
424 AliAnalysisDataWrapper *top, *crt;
426 TSeqCollection *collcrt = 0;
428 // Loop entries of the collection attached to this wrapper.
429 for (Int_t i=0; i<nentries; i++) {
431 top = new AliAnalysisDataWrapper(coll->At(i));
433 // Loop wrappers coming in the 'to merge with' list
434 while ((crt=(AliAnalysisDataWrapper*)next())) {
435 // printf(" %s 0x%lx (owner=%d)\n", crt->GetName(), (ULong_t)crt, crt->TestBit(AliAnalysisDataWrapper::kDeleteData));
436 collcrt = (TSeqCollection*)crt->Data();
437 list1->Add(new AliAnalysisDataWrapper(collcrt->At(i)));
439 // Now merge 'top' wrapper with 'list1'. This may go recursively.
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;
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
460 // printf("Wrapper %s 0x%lx (owner=%d) merged with:\n", GetName(), (ULong_t)this, TObject::TestBit(kDeleteData));
461 while ((cont=(AliAnalysisDataWrapper*)next1())) {
462 // printf(" %s 0x%lx (owner=%d)\n", cont->GetName(), (ULong_t)cont, cont->TestBit(AliAnalysisDataWrapper::kDeleteData));
463 TObject *data = cont->Data();
465 collectionData->Add(data);
468 callEnv.SetParam((Long_t) collectionData);
469 callEnv.Execute(fData);
470 delete collectionData;