]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisDataContainer.cxx
Made valid calibrations w/range 0-999999 - note oversampling is 4
[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 <TTree.h>
50 #include <TROOT.h>
51
52 #include "AliAnalysisDataContainer.h"
53 #include "AliAnalysisDataSlot.h"
54 #include "AliAnalysisTask.h"
55
56 ClassImp(AliAnalysisDataContainer)
57
58 //______________________________________________________________________________
59 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
60                           fDataReady(kFALSE),
61                           fOwnedData(kFALSE),
62                           fFileName(),
63                           fData(NULL),
64                           fType(NULL),
65                           fProducer(NULL),
66                           fConsumers(NULL)
67 {
68 // Dummy ctor.
69 }
70
71 //______________________________________________________________________________
72 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
73                          :TNamed(name,""),
74                           fDataReady(kFALSE),
75                           fOwnedData(kTRUE),
76                           fFileName(),
77                           fData(NULL),
78                           fType(type),
79                           fProducer(NULL),
80                           fConsumers(NULL)
81 {
82 // Default constructor.
83    SetTitle(fType->GetName());
84 }
85
86 //______________________________________________________________________________
87 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
88                          :TNamed(cont),
89                           fDataReady(cont.fDataReady),
90                           fOwnedData(kFALSE),
91                           fFileName(cont.fFileName),
92                           fData(cont.fData),
93                           fType(NULL),
94                           fProducer(cont.fProducer),
95                           fConsumers(NULL)
96 {
97 // Copy ctor.
98    GetType();
99    if (cont.fConsumers) {
100       fConsumers = new TObjArray(2);
101       Int_t ncons = cont.fConsumers->GetEntriesFast();
102       for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
103    }   
104 }
105
106 //______________________________________________________________________________
107 AliAnalysisDataContainer::~AliAnalysisDataContainer()
108 {
109 // Destructor. Deletes data ! (What happens if data is a container ???)
110    if (fData && fOwnedData) delete fData;
111    if (fConsumers) delete fConsumers;
112 }
113
114 //______________________________________________________________________________
115 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
116 {
117 // Assignment.
118    if (&cont != this) {
119       TNamed::operator=(cont);
120       fDataReady = cont.fDataReady;
121       fOwnedData = kFALSE;  // !!! Data owned by cont.
122       fFileName = cont.fFileName;
123       fData = cont.fData;
124       GetType();
125       fProducer = cont.fProducer;
126       if (cont.fConsumers) {
127          fConsumers = new TObjArray(2);
128          Int_t ncons = cont.fConsumers->GetEntriesFast();
129          for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
130       }   
131    }   
132    return *this;
133 }      
134
135 //______________________________________________________________________________
136 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
137 {
138 // Add a consumer for contained data;
139    AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
140    if (!slot || !slot->GetType()) {
141      cout<<"Consumer task "<< consumer->GetName()<<" does not have an input/type #"<<islot<<endl;
142      //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
143       return;
144    }
145    if (!slot->GetType()->InheritsFrom(GetType())) {
146      cout<<"Data type "<<slot->GetTitle()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<GetTitle()<<endl;  
147      //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()));
148       return;
149    }   
150
151    if (!fConsumers) fConsumers = new TObjArray(2);   
152    fConsumers->Add(consumer);
153    // Add the consumer task to the list of task of the producer
154    if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer)) 
155       fProducer->Add(consumer);
156 }      
157
158 //______________________________________________________________________________
159 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
160 {
161 // Check if all client tasks have executed.
162    TIter next(fConsumers);
163    AliAnalysisTask *task;
164    while ((task=(AliAnalysisTask*)next())) {
165       if (!task->HasExecuted()) return kFALSE;
166    }
167    return kTRUE;
168 }   
169
170 //______________________________________________________________________________
171 void AliAnalysisDataContainer::DeleteData()
172 {
173 // Delete data if not needed anymore.
174    if (!fDataReady || !ClientsExecuted()) {
175      cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
176      //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
177       return;
178    }
179    if (!fOwnedData) {
180      cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
181      //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
182       return;
183    }
184    delete fData;
185    fData = 0;
186    fDataReady = kFALSE;
187 }   
188
189 //______________________________________________________________________________
190 TClass *AliAnalysisDataContainer::GetType() const
191 {
192 // Get class type for this slot.
193    AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)this;
194    if (!fType) cont->SetType(gROOT->GetClass(fTitle.Data()));
195    if (!fType) printf("AliAnalysisDataContainer: Unknown class: %s\n", GetTitle());
196    return fType;
197 }
198
199 //______________________________________________________________________________
200 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
201 {
202 // If data is ready and derives from TTree or from TBranch, this will get the
203 // requested entry in memory if not already loaded.
204    if (!fDataReady || !GetType()) return;
205    Bool_t istree = fType->InheritsFrom(TTree::Class());
206    if (istree) {
207       TTree *tree = (TTree*)fData;
208       if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
209       return;
210    }   
211    Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
212    if (isbranch) {
213       TBranch *branch = (TBranch*)fData;
214       if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
215       return;
216    }   
217 }   
218
219 //______________________________________________________________________________
220 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
221 {
222 // Merge a list of containers with this one. Containers in the list must have
223 // data of the same type.
224    if (!list || !fData) return 0;
225    printf("Merging %d containers %s\n", list->GetSize()+1, GetName());
226    TMethodCall callEnv;
227    if (fData->IsA())
228       callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
229    if (!callEnv.IsValid() && !list->IsEmpty()) {
230       cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
231       return 1;
232    }
233
234    if (list->IsEmpty()) return 1;
235
236    TIter next(list);
237    AliAnalysisDataContainer *cont;
238    // Make a list where to temporary store the data to be merged.
239    TList *collectionData = new TList();
240    Int_t count = 0; // object counter
241    while ((cont=(AliAnalysisDataContainer*)next())) {
242       TObject *data = cont->GetData();
243       if (!data) continue;
244       if (strcmp(cont->GetName(), GetName())) {
245          cout << "Not merging containers with different names !" << endl;
246          continue;
247       }
248       printf(" ... merging object %s\n", data->GetName());
249       collectionData->Add(data);
250       count++;
251    }
252    callEnv.SetParam((Long_t) collectionData);
253    callEnv.Execute(fData);
254    delete collectionData;
255
256    return count+1;
257 }
258
259 //______________________________________________________________________________
260 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
261 {
262 // Print info about this container.
263    TString ind;
264    for (Int_t i=0; i<indent; i++) ind += " ";
265    TString opt(option);
266    opt.ToLower();
267    Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
268    if (!dep) {
269       printf("%sContainer: %s  type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
270       if (fProducer) 
271          printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
272       else
273          printf("%s= No data producer",ind.Data());
274       printf("%s = Consumer tasks: ", ind.Data());
275       if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
276       else printf("\n");
277    }
278    printf("Filename: %s\n", fFileName.Data());
279    TIter next(fConsumers);
280    AliAnalysisTask *task;
281    while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
282 }   
283
284 //______________________________________________________________________________
285 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
286 {
287 // Set the data as READY only if it was published by the producer.
288    // If there is no producer declared, this is a top level container.
289    AliAnalysisTask *task;
290    Bool_t init = kFALSE;
291    Int_t i, nc;
292    if (!fProducer) {
293       if (data != fData) init = kTRUE;
294       fData = data;
295       fDataReady = kTRUE;
296       if (fConsumers) {
297          nc = fConsumers->GetEntriesFast();
298          for (i=0; i<nc; i++) {
299             task = (AliAnalysisTask*)fConsumers->At(i);
300             task->CheckNotify(init);
301          }
302       }      
303       return kTRUE;
304    }
305    // Check if it is the producer who published the data     
306    if (fProducer->GetPublishedData()==data) {
307       fData = data;
308       fDataReady = kTRUE;
309       if (fConsumers) {
310          nc = fConsumers->GetEntriesFast();
311          for (i=0; i<nc; i++) {
312             task = (AliAnalysisTask*)fConsumers->At(i);
313             task->CheckNotify();
314          }
315       }      
316       return kTRUE;   
317    } else {
318      cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
319      //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));   
320      return kFALSE;           
321    }              
322 }
323
324 //______________________________________________________________________________
325 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
326 {
327 // Set the producer of data. The slot number is required for data type checking.
328    if (fProducer) {
329      cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
330      //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
331    } 
332    if (fDataReady) {
333      cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
334      //AliError(Form("%s container contains data - cannot change producer!", GetName()));
335       return;
336    }   
337    AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
338    if (!slot) {
339      cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
340      //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
341       return;
342    }   
343    if (!slot->GetType()->InheritsFrom(GetType())) {
344      cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
345      //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()));
346       return;
347    }   
348    
349    fProducer = prod;
350    // Add all consumers as daughter tasks
351    TIter next(fConsumers);
352    AliAnalysisTask *cons;
353    while ((cons=(AliAnalysisTask*)next())) {
354       if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
355    }   
356 }   
357
358 //______________________________________________________________________________
359 AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
360 {
361 // Wraps data for sending it through the net.
362    AliAnalysisDataWrapper *pack = 0;
363    if (!fData) return pack;
364    pack = new AliAnalysisDataWrapper(fData);
365    pack->SetName(fName.Data());
366    return pack;
367 }
368
369 //______________________________________________________________________________
370 void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
371 {
372 // Unwraps data from a data wrapper.
373    if (pack) {
374       fData = pack->Data();
375       fDataReady = kTRUE;
376       // Imported wrappers do not own data anymore (AG 13-11-07)
377       pack->SetDeleteData(kFALSE);
378    }   
379 }      
380       
381 ClassImp (AliAnalysisDataWrapper)
382
383 //______________________________________________________________________________
384 AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
385                        :TNamed(),
386                         fData(data)
387 {
388 // Ctor.
389    if (data) SetName(data->GetName());
390 }
391
392 //______________________________________________________________________________
393 AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
394 {
395 // Dtor.
396    if (fData && TObject::TestBit(kDeleteData)) delete fData;
397 }   
398
399 //______________________________________________________________________________
400 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
401 {
402 // Assignment.
403    if (&other != this) {
404       TNamed::operator=(other);
405       fData = other.fData;
406    }   
407    return *this;
408 }
409
410 //______________________________________________________________________________
411 Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
412 {
413 // Merge a list of containers with this one. Containers in the list must have
414 // data of the same type.
415    if (!fData) return 0;
416    if (!list || list->IsEmpty()) return 1;
417
418    TMethodCall callEnv;
419    if (fData->InheritsFrom(TSeqCollection::Class())) {
420 //      printf("Collection wrapper %s 0x%lx (owner=%d) merge with:\n", GetName(), (ULong_t)this, TObject::TestBit(kDeleteData));
421       TSeqCollection *coll = (TSeqCollection*)fData;
422       if (coll->IsEmpty()) return 0;
423       Int_t nentries = coll->GetEntries();
424       AliAnalysisDataWrapper *top, *crt;
425       TIter next(list);
426       TSeqCollection *collcrt = 0;
427       TList *list1 = 0;
428       // Loop entries of the collection attached to this wrapper.
429       for (Int_t i=0; i<nentries; i++) {
430          list1 = new TList();
431          top = new AliAnalysisDataWrapper(coll->At(i));
432          next.Reset();
433          // Loop wrappers coming in the 'to merge with' list
434          while ((crt=(AliAnalysisDataWrapper*)next())) {
435 //            printf("  %s 0x%lx (owner=%d)\n", crt->GetName(), (ULong_t)crt, crt->TestBit(AliAnalysisDataWrapper::kDeleteData));
436             collcrt = (TSeqCollection*)crt->Data();
437             list1->Add(new AliAnalysisDataWrapper(collcrt->At(i)));
438          }
439          // Now merge 'top' wrapper with 'list1'. This may go recursively.
440          top->Merge(list1);
441          delete top;
442          list1->Delete();
443          delete list1;
444       }
445       return nentries;
446    }   
447    
448    if (fData->IsA())
449       callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
450    if (!callEnv.IsValid()) {
451       cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
452       return 1;
453    }
454
455    TIter next1(list);
456    AliAnalysisDataWrapper *cont;
457    // Make a list where to temporary store the data to be merged.
458    TList *collectionData = new TList();
459    Int_t count = 0; // object counter
460 //   printf("Wrapper %s 0x%lx (owner=%d) merged with:\n", GetName(), (ULong_t)this, TObject::TestBit(kDeleteData));
461    while ((cont=(AliAnalysisDataWrapper*)next1())) {
462 //      printf("   %s 0x%lx (owner=%d)\n", cont->GetName(), (ULong_t)cont, cont->TestBit(AliAnalysisDataWrapper::kDeleteData));
463       TObject *data = cont->Data();
464       if (!data) continue;
465       collectionData->Add(data);
466       count++;
467    }
468    callEnv.SetParam((Long_t) collectionData);
469    callEnv.Execute(fData);
470    delete collectionData;
471
472    return count+1;
473 }