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>
54 #include "AliAnalysisManager.h"
55 #include "AliAnalysisDataContainer.h"
56 #include "AliAnalysisDataSlot.h"
57 #include "AliAnalysisTask.h"
61 ClassImp(AliAnalysisDataContainer)
63 //______________________________________________________________________________
64 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
78 //______________________________________________________________________________
79 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
91 // Default constructor.
92 SetTitle(fType->GetName());
95 //______________________________________________________________________________
96 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
98 fDataReady(cont.fDataReady),
100 fFileName(cont.fFileName),
101 fFolderName(cont.fFolderName),
105 fProducer(cont.fProducer),
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));
117 //______________________________________________________________________________
118 AliAnalysisDataContainer::~AliAnalysisDataContainer()
120 // Destructor. Deletes data ! (What happens if data is a container ???)
121 if (fData && fOwnedData) delete fData;
122 if (fConsumers) delete fConsumers;
125 //______________________________________________________________________________
126 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
130 TNamed::operator=(cont);
131 fDataReady = cont.fDataReady;
133 fFileName = cont.fFileName;
134 fFolderName = cont.fFolderName;
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));
148 //______________________________________________________________________________
149 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
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));
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()));
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);
171 //______________________________________________________________________________
172 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
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;
183 //______________________________________________________________________________
184 void AliAnalysisDataContainer::DeleteData()
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()));
193 cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
194 //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
202 //______________________________________________________________________________
203 TClass *AliAnalysisDataContainer::GetType() const
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());
212 //______________________________________________________________________________
213 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
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());
220 TTree *tree = (TTree*)fData;
221 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
224 Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
226 TBranch *branch = (TBranch*)fData;
227 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
232 //______________________________________________________________________________
233 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
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());
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;
247 if (list->IsEmpty()) return 1;
250 AliAnalysisDataContainer *cont;
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();
257 if (strcmp(cont->GetName(), GetName())) {
258 cout << "Not merging containers with different names !" << endl;
261 printf(" ... merging object %s\n", data->GetName());
262 collectionData->Add(data);
265 callEnv.SetParam((Long_t) collectionData);
266 callEnv.Execute(fData);
267 delete collectionData;
272 //______________________________________________________________________________
273 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
275 // Print info about this container.
277 for (Int_t i=0; i<indent; i++) ind += " ";
280 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
282 printf("%sContainer: %s type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
284 printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
286 printf("%s= No data producer",ind.Data());
287 printf("%s = Consumer tasks: ", ind.Data());
288 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
291 if (fFolderName.Length())
292 printf("Filename: %s folder: %s\n", fFileName.Data(), fFolderName.Data());
294 printf("Filename: %s\n", fFileName.Data());
295 TIter next(fConsumers);
296 AliAnalysisTask *task;
297 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
300 //______________________________________________________________________________
301 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
303 // Set the data as READY only if it was published by the producer.
304 // If there is no producer declared, this is a top level container.
305 AliAnalysisTask *task;
306 Bool_t init = kFALSE;
309 if (data != fData) init = kTRUE;
313 nc = fConsumers->GetEntriesFast();
314 for (i=0; i<nc; i++) {
315 task = (AliAnalysisTask*)fConsumers->At(i);
316 task->CheckNotify(init);
321 // Check if it is the producer who published the data
322 if (fProducer->GetPublishedData()==data) {
326 nc = fConsumers->GetEntriesFast();
327 for (i=0; i<nc; i++) {
328 task = (AliAnalysisTask*)fConsumers->At(i);
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()));
341 //______________________________________________________________________________
342 void AliAnalysisDataContainer::SetFileName(const char *filename)
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;
350 Int_t index = fFileName.Index(":");
351 // Fill the folder name
353 fFolderName = fFileName(index+1, fFileName.Length()-index);
354 fFileName.Remove(index);
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");
362 //______________________________________________________________________________
363 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
365 // Set the producer of data. The slot number is required for data type checking.
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()));
371 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
372 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
375 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
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));
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;
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()));
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);
396 //______________________________________________________________________________
397 AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
399 // Wraps data for sending it through the net.
400 AliAnalysisDataWrapper *pack = 0;
402 Error("ExportData", "Container %s - No data to be wrapped !", GetName());
405 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
406 if (mgr->GetDebugLevel() > 1) printf(" ExportData: Wrapping data %s for container %s\n", fData->GetName(),GetName());
407 pack = new AliAnalysisDataWrapper(fData);
408 pack->SetName(fName.Data());
412 //______________________________________________________________________________
413 void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
415 // Unwraps data from a data wrapper.
417 fData = pack->Data();
419 Error("ImportData", "No data was wrapped for container %s", GetName());
422 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
423 if (mgr->GetDebugLevel() > 1) printf(" ImportData: Unwrapping data %s for container %s\n", fData->GetName(),GetName());
425 // Imported wrappers do not own data anymore (AG 13-11-07)
426 pack->SetDeleteData(kFALSE);
430 ClassImp (AliAnalysisDataWrapper)
432 //______________________________________________________________________________
433 AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
438 if (data) SetName(data->GetName());
441 //______________________________________________________________________________
442 AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
445 if (fData && TObject::TestBit(kDeleteData)) delete fData;
448 //______________________________________________________________________________
449 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
452 if (&other != this) {
453 TNamed::operator=(other);
459 //______________________________________________________________________________
460 Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
462 // Merge a list of containers with this one. Containers in the list must have
463 // data of the same type.
464 if (TH1::AddDirectoryStatus()) TH1::AddDirectory(kFALSE);
465 if (!fData) return 0;
466 if (!list || list->IsEmpty()) return 1;
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;
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
483 // printf("Wrapper %s 0x%lx (data=%s) merged with:\n", GetName(), (ULong_t)this, fData->ClassName());
484 while ((cont=(AliAnalysisDataWrapper*)next1())) {
485 cont->SetDeleteData();
486 TObject *data = cont->Data();
488 // printf(" - %s 0x%lx (data=%s)\n", cont->GetName(), (ULong_t)cont, data->ClassName());
489 collectionData->Add(data);
492 callEnv.SetParam((Long_t) collectionData);
493 callEnv.Execute(fData);
494 delete collectionData;