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