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