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()
68 //______________________________________________________________________________
69 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
72 // Normal constructor.
82 //______________________________________________________________________________
83 AliAnalysisDataContainer::~AliAnalysisDataContainer()
85 // Destructor. Deletes data ! (What happens if data is a container ???)
86 if (fData && fOwnedData) delete fData;
87 if (fConsumers) delete fConsumers;
90 //______________________________________________________________________________
91 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *option)
93 // Set the data as READY only if it was published by the producer.
94 // If option is not empty the data will be saved in the file fFileName and option
95 // describes the method to opent the file: NEW/CREATE, RECREATE, UPDATE
96 // If there is no producer declared, this is a top level container.
97 AliAnalysisTask *task;
103 nc = fConsumers->GetEntriesFast();
104 for (i=0; i<nc; i++) {
105 task = (AliAnalysisTask*)fConsumers->At(i);
111 // Check if it is the producer who published the data
112 if (fProducer->GetPublishedData()==data) {
115 if (strlen(option)) {
116 if (!fFileName.Length()) {
117 AliWarning(Form("Cannot write data since file name for container %s was not set", GetName()));
120 TFile *f = new TFile(fFileName.Data(), option);
121 if (!f->IsZombie()) {
127 nc = fConsumers->GetEntriesFast();
128 for (i=0; i<nc; i++) {
129 task = (AliAnalysisTask*)fConsumers->At(i);
135 AliWarning(Form("Data for container %s can be published only by producer task %s",
136 GetName(), fProducer->GetName()));
141 //______________________________________________________________________________
142 void AliAnalysisDataContainer::SetFileName(const char *name)
144 // Data will be written to this file if it is set using SetData(data, option)
145 // Option represent the way the file is accessed: NEW, APPEND, ...
149 //______________________________________________________________________________
150 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
152 // If data is ready and derives from TTree or from TBranch, this will get the
153 // requested entry in memory if not already loaded.
154 if (!fDataReady) return;
155 Bool_t is_tree = fType->InheritsFrom(TTree::Class());
157 TTree *tree = (TTree*)fData;
158 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
161 Bool_t is_branch = fType->InheritsFrom(TBranch::Class());
163 TBranch *branch = (TBranch*)fData;
164 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
169 //______________________________________________________________________________
170 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
172 // Set the producer of data. The slot number is required for data type checking.
174 AliWarning(Form("Data container %s already has a producer: %s",
175 GetName(),fProducer->GetName()));
178 AliError(Form("%s container contains data - cannot change producer!", GetName()));
181 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
183 AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
186 if (!slot->GetType()->InheritsFrom(fType)) {
187 AliError(Form("Data type %s for output slot %i of task %s does not match container type %s",
188 slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
193 // Add all consumers as daughter tasks
194 TIter next(fConsumers);
195 AliAnalysisTask *cons;
196 while ((cons=(AliAnalysisTask*)next())) {
197 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
201 //______________________________________________________________________________
202 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
204 // Add a consumer for contained data;
205 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
207 AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
210 if (!slot->GetType()->InheritsFrom(fType)) {
211 AliError(Form("Data type %s for input slot %i of task %s does not match container type %s",
212 slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
216 if (!fConsumers) fConsumers = new TObjArray(2);
217 fConsumers->Add(consumer);
218 // Add the consumer task to the list of task of the producer
219 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
220 fProducer->Add(consumer);
223 //______________________________________________________________________________
224 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
226 // Check if all client tasks have executed.
227 TIter next(fConsumers);
228 AliAnalysisTask *task;
229 while ((task=(AliAnalysisTask*)next())) {
230 if (!task->HasExecuted()) return kFALSE;
235 //______________________________________________________________________________
236 void AliAnalysisDataContainer::DeleteData()
238 // Delete data if not needed anymore.
239 if (!fDataReady || !ClientsExecuted()) {
240 AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
244 AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
252 //______________________________________________________________________________
253 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
255 // Print info about this container.
257 for (Int_t i=0; i<indent; i++) ind += " ";
260 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
262 printf("%s\n", Form("%sContainer: %s type: %s", ind.Data(), GetName(), fType->GetName()));
264 printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName()));
266 printf("%s\n", Form("%s= No data producer"));
267 printf("%s", Form("%s = Consumer tasks: "));
268 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
271 TIter next(fConsumers);
272 AliAnalysisTask *task;
273 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);