]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisDataContainer.cxx
-updates marcel
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataContainer.cxx
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
45 #include <Riostream.h>
46 #include <TMethodCall.h>
47
48 #include <TClass.h>
49 #include <TFile.h>
50 #include <TTree.h>
51 #include <TH1.h>
52 #include <TROOT.h>
53
54 #include "AliAnalysisManager.h"
55 #include "AliAnalysisDataContainer.h"
56 #include "AliAnalysisDataSlot.h"
57 #include "AliAnalysisTask.h"
58
59 using std::endl;
60 using std::cout;
61 ClassImp(AliAnalysisDataContainer)
62
63 //______________________________________________________________________________
64 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
65                           fDataReady(kFALSE),
66                           fOwnedData(kFALSE),
67                           fFileName(),
68                           fFolderName(),
69                           fFile(NULL),
70                           fData(NULL),
71                           fType(NULL),
72                           fProducer(NULL),
73                           fConsumers(NULL)
74 {
75 // Dummy ctor.
76 }
77
78 //______________________________________________________________________________
79 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
80                          :TNamed(name,""),
81                           fDataReady(kFALSE),
82                           fOwnedData(kFALSE),
83                           fFileName(),
84                           fFolderName(),
85                           fFile(NULL),
86                           fData(NULL),
87                           fType(type),
88                           fProducer(NULL),
89                           fConsumers(NULL)
90 {
91 // Default constructor.
92    SetTitle(fType->GetName());
93 }
94
95 //______________________________________________________________________________
96 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
97                          :TNamed(cont),
98                           fDataReady(cont.fDataReady),
99                           fOwnedData(kFALSE),
100                           fFileName(cont.fFileName),
101                           fFolderName(cont.fFolderName),
102                           fFile(NULL),
103                           fData(cont.fData),
104                           fType(NULL),
105                           fProducer(cont.fProducer),
106                           fConsumers(NULL)
107 {
108 // Copy ctor.
109    GetType();
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));
114    }   
115 }
116
117 //______________________________________________________________________________
118 AliAnalysisDataContainer::~AliAnalysisDataContainer()
119 {
120 // Destructor. Deletes data ! (What happens if data is a container ???)
121    if (fData && fOwnedData) delete fData;
122    if (fConsumers) delete fConsumers;
123 }
124
125 //______________________________________________________________________________
126 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
127 {
128 // Assignment.
129    if (&cont != this) {
130       TNamed::operator=(cont);
131       fDataReady = cont.fDataReady;
132       fOwnedData = kFALSE;
133       fFileName = cont.fFileName;
134       fFolderName = cont.fFolderName;
135       fFile = NULL;
136       fData = cont.fData;
137       GetType();
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));
143       }   
144    }   
145    return *this;
146 }      
147
148 //______________________________________________________________________________
149 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
150 {
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));
156       return;
157    }
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()));
161       return;
162    }   
163
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);
169 }      
170
171 //______________________________________________________________________________
172 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
173 {
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;
179    }
180    return kTRUE;
181 }   
182
183 //______________________________________________________________________________
184 void AliAnalysisDataContainer::DeleteData()
185 {
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()));
190       return;
191    }
192    if (!fOwnedData) {
193      cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
194      //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
195       return;
196    }
197    delete fData;
198    fData = 0;
199    fDataReady = kFALSE;
200 }   
201
202 //______________________________________________________________________________
203 TClass *AliAnalysisDataContainer::GetType() const
204 {
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());
209    return fType;
210 }
211
212 //______________________________________________________________________________
213 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
214 {
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());
219    if (istree) {
220       TTree *tree = (TTree*)fData;
221       if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
222       return;
223    }   
224    Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
225    if (isbranch) {
226       TBranch *branch = (TBranch*)fData;
227       if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
228       return;
229    }   
230 }   
231
232 //______________________________________________________________________________
233 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
234 {
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());
239    TMethodCall callEnv;
240    if (fData->IsA())
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;
244       return 1;
245    }
246
247    if (list->IsEmpty()) return 1;
248
249    TIter next(list);
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();
256       if (!data) continue;
257       if (strcmp(cont->GetName(), GetName())) {
258          cout << "Not merging containers with different names !" << endl;
259          continue;
260       }
261       printf(" ... merging object %s\n", data->GetName());
262       collectionData->Add(data);
263       count++;
264    }
265    callEnv.SetParam((Long_t) collectionData);
266    callEnv.Execute(fData);
267    delete collectionData;
268
269    return count+1;
270 }
271
272 //______________________________________________________________________________
273 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
274 {
275 // Print info about this container.
276    TString ind;
277    for (Int_t i=0; i<indent; i++) ind += " ";
278    TString opt(option);
279    opt.ToLower();
280    Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
281    if (!dep) {
282       printf("%sContainer: %s  type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
283       if (fProducer) 
284          printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
285       else
286          printf("%s= No data producer",ind.Data());
287       printf("%s = Consumer tasks: ", ind.Data());
288       if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
289       else printf("\n");
290    }
291    if (fFolderName.Length())
292      printf("Filename: %s  folder: %s\n", fFileName.Data(), fFolderName.Data());
293    else
294      printf("Filename: %s\n", fFileName.Data());
295    TIter next(fConsumers);
296    AliAnalysisTask *task;
297    while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
298 }   
299
300 //______________________________________________________________________________
301 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
302 {
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;
307    Int_t i, nc;
308    if (!fProducer) {
309       if (data != fData) init = kTRUE;
310       fData = data;
311       fDataReady = kTRUE;
312       if (fConsumers) {
313          nc = fConsumers->GetEntriesFast();
314          for (i=0; i<nc; i++) {
315             task = (AliAnalysisTask*)fConsumers->At(i);
316             task->CheckNotify(init);
317          }
318       }      
319       return kTRUE;
320    }
321    // Check if it is the producer who published the data     
322    if (fProducer->GetPublishedData()==data) {
323       fData = data;
324       fDataReady = kTRUE;
325       if (fConsumers) {
326          nc = fConsumers->GetEntriesFast();
327          for (i=0; i<nc; i++) {
328             task = (AliAnalysisTask*)fConsumers->At(i);
329             task->CheckNotify();
330          }
331       }      
332       return kTRUE;   
333    } else {
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()));   
337       return kFALSE;           
338    }              
339 }
340
341 //______________________________________________________________________________
342 void AliAnalysisDataContainer::SetFileName(const char *filename)
343 {
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;
349   fFolderName = "";
350   Int_t index = fFileName.Index(":");
351   // Fill the folder name
352   if (index >= 0) {
353     fFolderName = fFileName(index+1, fFileName.Length()-index);
354     fFileName.Remove(index);
355   }  
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");   
360 }
361
362 //______________________________________________________________________________
363 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
364 {
365 // Set the producer of data. The slot number is required for data type checking.
366    if (fProducer) {
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()));
369    } 
370    if (fDataReady) {
371      cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
372      //AliError(Form("%s container contains data - cannot change producer!", GetName()));
373       return;
374    }   
375    AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
376    if (!slot) {
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));
379       return;
380    }   
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()));
384       return;
385    }   
386    
387    fProducer = prod;
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);
393    }   
394 }   
395
396 //______________________________________________________________________________
397 AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
398 {
399 // Wraps data for sending it through the net.
400    AliAnalysisDataWrapper *pack = 0;
401    if (!fData) {
402       Error("ExportData", "Container %s - No data to be wrapped !", GetName());
403       return pack;
404    } 
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());
409    return pack;
410 }
411
412 //______________________________________________________________________________
413 void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
414 {
415 // Unwraps data from a data wrapper.
416    if (pack) {
417       fData = pack->Data();
418       if (!fData) {
419          Error("ImportData", "No data was wrapped for container %s", GetName());
420          return;
421       }   
422       AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
423       if (mgr->GetDebugLevel() > 1) printf("   ImportData: Unwrapping data %s for container %s\n", fData->GetName(),GetName());
424       fDataReady = kTRUE;
425       // Imported wrappers do not own data anymore (AG 13-11-07)
426       pack->SetDeleteData(kFALSE);
427    }   
428 }      
429       
430 ClassImp (AliAnalysisDataWrapper)
431
432 //______________________________________________________________________________
433 AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
434                        :TNamed(),
435                         fData(data)
436 {
437 // Ctor.
438    if (data) SetName(data->GetName());
439 }
440
441 //______________________________________________________________________________
442 AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
443 {
444 // Dtor.
445    if (fData && TObject::TestBit(kDeleteData)) delete fData;
446 }   
447
448 //______________________________________________________________________________
449 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
450 {
451 // Assignment.
452    if (&other != this) {
453       TNamed::operator=(other);
454       fData = other.fData;
455    }   
456    return *this;
457 }
458
459 //______________________________________________________________________________
460 Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
461 {
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;
467
468    SetDeleteData();
469
470    TMethodCall callEnv;
471    if (fData->IsA())
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;
475       return 1;
476    }
477
478    TIter next1(list);
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();
487       if (!data) continue;
488       // printf("   - %s 0x%lx (data=%s)\n", cont->GetName(), (ULong_t)cont, data->ClassName());
489       collectionData->Add(data);
490       count++;
491    }
492    callEnv.SetParam((Long_t) collectionData);
493    callEnv.Execute(fData);
494    delete collectionData;
495
496    return count+1;
497 }