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"
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 // Normal constructor.
85 //______________________________________________________________________________
86 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
88 fDataReady(cont.fDataReady),
93 fProducer(cont.fProducer),
97 if (cont.fConsumers) {
98 fConsumers = new TObjArray(2);
99 Int_t ncons = cont.fConsumers->GetEntriesFast();
100 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
104 //______________________________________________________________________________
105 AliAnalysisDataContainer::~AliAnalysisDataContainer()
107 // Destructor. Deletes data ! (What happens if data is a container ???)
108 if (fData && fOwnedData) delete fData;
109 if (fConsumers) delete fConsumers;
116 //______________________________________________________________________________
117 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
121 TNamed::operator=(cont);
122 fDataReady = cont.fDataReady;
123 fOwnedData = kFALSE; // !!! Data owned by cont.
127 fProducer = cont.fProducer;
128 if (cont.fConsumers) {
129 fConsumers = new TObjArray(2);
130 Int_t ncons = cont.fConsumers->GetEntriesFast();
131 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
137 //______________________________________________________________________________
138 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
140 // Set the data as READY only if it was published by the producer.
141 // If there is no producer declared, this is a top level container.
142 AliAnalysisTask *task;
143 Bool_t init = kFALSE;
146 if (data != fData) init = kTRUE;
150 nc = fConsumers->GetEntriesFast();
151 for (i=0; i<nc; i++) {
152 task = (AliAnalysisTask*)fConsumers->At(i);
153 task->CheckNotify(init);
158 // Check if it is the producer who published the data
159 if (fProducer->GetPublishedData()==data) {
163 nc = fConsumers->GetEntriesFast();
164 for (i=0; i<nc; i++) {
165 task = (AliAnalysisTask*)fConsumers->At(i);
171 cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
172 //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));
177 //______________________________________________________________________________
178 void AliAnalysisDataContainer::OpenFile(const char *name, Option_t *option)
180 // Data will be written to this file at the end of processing.
181 // Option represent the way the file is accessed: NEW, APPEND, ...
186 fFile = new TFile(name, option);
187 if (fFile->IsZombie()) {
188 cout<<"Cannot open file "<<name<<" with option "<<option<<endl;
189 //AliError(Form("Cannot open file %s with option %s",name,option));
194 //______________________________________________________________________________
195 void AliAnalysisDataContainer::WriteData()
197 // Write data to the file.
199 TDirectory *cursav = gDirectory;
203 if (cursav) cursav->cd();
207 //______________________________________________________________________________
208 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
210 // If data is ready and derives from TTree or from TBranch, this will get the
211 // requested entry in memory if not already loaded.
212 if (!fDataReady) return;
213 Bool_t istree = fType->InheritsFrom(TTree::Class());
215 TTree *tree = (TTree*)fData;
216 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
219 Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
221 TBranch *branch = (TBranch*)fData;
222 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
227 //______________________________________________________________________________
228 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
230 // Set the producer of data. The slot number is required for data type checking.
232 cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
233 //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
236 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
237 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
240 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
242 cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
243 //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
246 if (!slot->GetType()->InheritsFrom(fType)) {
247 cout<<"Data type "<<slot->GetType()->GetName()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<fType->GetName()<<endl;
248 //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()));
253 // Add all consumers as daughter tasks
254 TIter next(fConsumers);
255 AliAnalysisTask *cons;
256 while ((cons=(AliAnalysisTask*)next())) {
257 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
261 //______________________________________________________________________________
262 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
264 // Add a consumer for contained data;
265 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
267 cout<<"Consumer task "<< consumer->GetName()<<" does not have an input #"<<islot<<endl;
268 //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
271 if (!slot->GetType()->InheritsFrom(fType)) {
272 cout<<"Data type "<<slot->GetType()->GetName()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<fType->GetName()<<endl;
273 //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()));
277 if (!fConsumers) fConsumers = new TObjArray(2);
278 fConsumers->Add(consumer);
279 // Add the consumer task to the list of task of the producer
280 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
281 fProducer->Add(consumer);
284 //______________________________________________________________________________
285 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
287 // Check if all client tasks have executed.
288 TIter next(fConsumers);
289 AliAnalysisTask *task;
290 while ((task=(AliAnalysisTask*)next())) {
291 if (!task->HasExecuted()) return kFALSE;
296 //______________________________________________________________________________
297 void AliAnalysisDataContainer::DeleteData()
299 // Delete data if not needed anymore.
300 if (!fDataReady || !ClientsExecuted()) {
301 cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
302 //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
306 cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
307 //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
315 //______________________________________________________________________________
316 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
318 // Print info about this container.
320 for (Int_t i=0; i<indent; i++) ind += " ";
323 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
325 printf("%s\n", Form("%sContainer: %s type: %s", ind.Data(), GetName(), fType->GetName()));
327 printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName()));
329 printf("%s\n", Form("%s= No data producer"));
330 printf("%s", Form("%s = Consumer tasks: "));
331 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
334 TIter next(fConsumers);
335 AliAnalysisTask *task;
336 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);