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