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 //==============================================================================
50 #include "AliAnalysisDataContainer.h"
51 #include "AliAnalysisDataSlot.h"
52 #include "AliAnalysisTask.h"
54 ClassImp(AliAnalysisDataContainer)
56 //______________________________________________________________________________
57 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
69 //______________________________________________________________________________
70 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
80 // Normal constructor.
83 //______________________________________________________________________________
84 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
86 fDataReady(cont.fDataReady),
88 fFileName(cont.fFileName),
91 fProducer(cont.fProducer),
95 if (cont.fConsumers) {
96 fConsumers = new TObjArray(2);
97 Int_t ncons = cont.fConsumers->GetEntriesFast();
98 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
102 //______________________________________________________________________________
103 AliAnalysisDataContainer::~AliAnalysisDataContainer()
105 // Destructor. Deletes data ! (What happens if data is a container ???)
106 if (fData && fOwnedData) delete fData;
107 if (fConsumers) delete fConsumers;
110 //______________________________________________________________________________
111 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
115 TNamed::operator=(cont);
116 fDataReady = cont.fDataReady;
117 fOwnedData = kFALSE; // !!! Data owned by cont.
118 fFileName = cont.fFileName;
121 fProducer = cont.fProducer;
122 if (cont.fConsumers) {
123 fConsumers = new TObjArray(2);
124 Int_t ncons = cont.fConsumers->GetEntriesFast();
125 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
131 //______________________________________________________________________________
132 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *option)
134 // Set the data as READY only if it was published by the producer.
135 // If option is not empty the data will be saved in the file fFileName and option
136 // describes the method to opent the file: NEW/CREATE, RECREATE, UPDATE
137 // If there is no producer declared, this is a top level container.
138 AliAnalysisTask *task;
144 nc = fConsumers->GetEntriesFast();
145 for (i=0; i<nc; i++) {
146 task = (AliAnalysisTask*)fConsumers->At(i);
152 // Check if it is the producer who published the data
153 if (fProducer->GetPublishedData()==data) {
156 if (strlen(option)) {
157 if (!fFileName.Length()) {
158 AliWarning(Form("Cannot write data since file name for container %s was not set", GetName()));
161 TFile *f = new TFile(fFileName.Data(), option);
162 if (!f->IsZombie()) {
168 nc = fConsumers->GetEntriesFast();
169 for (i=0; i<nc; i++) {
170 task = (AliAnalysisTask*)fConsumers->At(i);
176 AliWarning(Form("Data for container %s can be published only by producer task %s",
177 GetName(), fProducer->GetName()));
182 //______________________________________________________________________________
183 void AliAnalysisDataContainer::SetFileName(const char *name)
185 // Data will be written to this file if it is set using SetData(data, option)
186 // Option represent the way the file is accessed: NEW, APPEND, ...
190 //______________________________________________________________________________
191 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
193 // If data is ready and derives from TTree or from TBranch, this will get the
194 // requested entry in memory if not already loaded.
195 if (!fDataReady) return;
196 Bool_t is_tree = fType->InheritsFrom(TTree::Class());
198 TTree *tree = (TTree*)fData;
199 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
202 Bool_t is_branch = fType->InheritsFrom(TBranch::Class());
204 TBranch *branch = (TBranch*)fData;
205 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
210 //______________________________________________________________________________
211 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
213 // Set the producer of data. The slot number is required for data type checking.
215 AliWarning(Form("Data container %s already has a producer: %s",
216 GetName(),fProducer->GetName()));
219 AliError(Form("%s container contains data - cannot change producer!", GetName()));
222 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
224 AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
227 if (!slot->GetType()->InheritsFrom(fType)) {
228 AliError(Form("Data type %s for output slot %i of task %s does not match container type %s",
229 slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
234 // Add all consumers as daughter tasks
235 TIter next(fConsumers);
236 AliAnalysisTask *cons;
237 while ((cons=(AliAnalysisTask*)next())) {
238 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
242 //______________________________________________________________________________
243 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
245 // Add a consumer for contained data;
246 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
248 AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
251 if (!slot->GetType()->InheritsFrom(fType)) {
252 AliError(Form("Data type %s for input slot %i of task %s does not match container type %s",
253 slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
257 if (!fConsumers) fConsumers = new TObjArray(2);
258 fConsumers->Add(consumer);
259 // Add the consumer task to the list of task of the producer
260 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
261 fProducer->Add(consumer);
264 //______________________________________________________________________________
265 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
267 // Check if all client tasks have executed.
268 TIter next(fConsumers);
269 AliAnalysisTask *task;
270 while ((task=(AliAnalysisTask*)next())) {
271 if (!task->HasExecuted()) return kFALSE;
276 //______________________________________________________________________________
277 void AliAnalysisDataContainer::DeleteData()
279 // Delete data if not needed anymore.
280 if (!fDataReady || !ClientsExecuted()) {
281 AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
285 AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
293 //______________________________________________________________________________
294 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
296 // Print info about this container.
298 for (Int_t i=0; i<indent; i++) ind += " ";
301 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
303 printf("%s\n", Form("%sContainer: %s type: %s", ind.Data(), GetName(), fType->GetName()));
305 printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName()));
307 printf("%s\n", Form("%s= No data producer"));
308 printf("%s", Form("%s = Consumer tasks: "));
309 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
312 TIter next(fConsumers);
313 AliAnalysisTask *task;
314 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);