]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisManager.cxx
VEventPool moved to STEERBase
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisManager.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 //   AliAnalysysManager - Manager analysis class. Allows creation of several
21 // analysis tasks and data containers storing their input/output. Allows
22 // connecting/chaining tasks via shared data containers. Serializes the current
23 // event for all tasks depending only on initial input data.
24 //==============================================================================
25 //
26 //==============================================================================
27
28 #include <Riostream.h>
29
30 #include <TClass.h>
31 #include <TFile.h>
32 #include <TMethodCall.h>
33 #include <TChain.h>
34 #include <TSystem.h>
35 #include <TROOT.h>
36 #include <TCanvas.h>
37
38 #include "AliAnalysisSelector.h"
39 #include "AliAnalysisTask.h"
40 #include "AliAnalysisDataContainer.h"
41 #include "AliAnalysisDataSlot.h"
42 #include "AliVEventHandler.h"
43 #include "AliVEventPool.h"
44 #include "AliSysInfo.h"
45 #include "AliAnalysisManager.h"
46
47 ClassImp(AliAnalysisManager)
48
49 AliAnalysisManager *AliAnalysisManager::fgAnalysisManager = NULL;
50
51 //______________________________________________________________________________
52 AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
53                    :TNamed(name,title),
54                     fTree(NULL),
55                     fInputEventHandler(NULL),
56                     fOutputEventHandler(NULL),
57                     fMCtruthEventHandler(NULL),
58                     fEventPool(NULL),
59                     fCurrentEntry(-1),
60                     fNSysInfo(0),
61                     fMode(kLocalAnalysis),
62                     fInitOK(kFALSE),
63                     fDebug(0),
64                     fSpecialOutputLocation(""), 
65                     fTasks(NULL),
66                     fTopTasks(NULL),
67                     fZombies(NULL),
68                     fContainers(NULL),
69                     fInputs(NULL),
70                     fOutputs(NULL),
71                     fSelector(NULL)
72 {
73 // Default constructor.
74    fgAnalysisManager = this;
75    fTasks      = new TObjArray();
76    fTopTasks   = new TObjArray();
77    fZombies    = new TObjArray();
78    fContainers = new TObjArray();
79    fInputs     = new TObjArray();
80    fOutputs    = new TObjArray();
81    SetEventLoop(kTRUE);
82 }
83
84 //______________________________________________________________________________
85 AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
86                    :TNamed(other),
87                     fTree(NULL),
88                     fInputEventHandler(NULL),
89                     fOutputEventHandler(NULL),
90                     fMCtruthEventHandler(NULL),
91                     fEventPool(NULL),
92                     fCurrentEntry(-1),
93                     fNSysInfo(0),
94                     fMode(other.fMode),
95                     fInitOK(other.fInitOK),
96                     fDebug(other.fDebug),
97                     fSpecialOutputLocation(""), 
98                     fTasks(NULL),
99                     fTopTasks(NULL),
100                     fZombies(NULL),
101                     fContainers(NULL),
102                     fInputs(NULL),
103                     fOutputs(NULL),
104                     fSelector(NULL)
105 {
106 // Copy constructor.
107    fTasks      = new TObjArray(*other.fTasks);
108    fTopTasks   = new TObjArray(*other.fTopTasks);
109    fZombies    = new TObjArray(*other.fZombies);
110    fContainers = new TObjArray(*other.fContainers);
111    fInputs     = new TObjArray(*other.fInputs);
112    fOutputs    = new TObjArray(*other.fOutputs);
113    fgAnalysisManager = this;
114 }
115    
116 //______________________________________________________________________________
117 AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& other)
118 {
119 // Assignment
120    if (&other != this) {
121       TNamed::operator=(other);
122       fInputEventHandler   = other.fInputEventHandler;
123       fOutputEventHandler  = other.fOutputEventHandler;
124       fMCtruthEventHandler = other.fMCtruthEventHandler;
125       fEventPool           = other.fEventPool;
126       fTree       = NULL;
127       fCurrentEntry = -1;
128       fNSysInfo   = other.fNSysInfo;
129       fMode       = other.fMode;
130       fInitOK     = other.fInitOK;
131       fDebug      = other.fDebug;
132       fTasks      = new TObjArray(*other.fTasks);
133       fTopTasks   = new TObjArray(*other.fTopTasks);
134       fZombies    = new TObjArray(*other.fZombies);
135       fContainers = new TObjArray(*other.fContainers);
136       fInputs     = new TObjArray(*other.fInputs);
137       fOutputs    = new TObjArray(*other.fOutputs);
138       fSelector   = NULL;
139       fgAnalysisManager = this;
140    }
141    return *this;
142 }
143
144 //______________________________________________________________________________
145 AliAnalysisManager::~AliAnalysisManager()
146 {
147 // Destructor.
148    if (fTasks) {fTasks->Delete(); delete fTasks;}
149    if (fTopTasks) delete fTopTasks;
150    if (fZombies) delete fZombies;
151    if (fContainers) {fContainers->Delete(); delete fContainers;}
152    if (fInputs) delete fInputs;
153    if (fOutputs) delete fOutputs;
154    if (fgAnalysisManager==this) fgAnalysisManager = NULL;
155 }
156
157 //______________________________________________________________________________
158 Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
159 {
160 // Read one entry of the tree or a whole branch.
161    if (fDebug > 0) printf("== AliAnalysisManager::GetEntry(%lld)\n", entry);
162    fCurrentEntry = entry;
163    return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
164 }
165    
166 //______________________________________________________________________________
167 void AliAnalysisManager::Init(TTree *tree)
168 {
169   // The Init() function is called when the selector needs to initialize
170   // a new tree or chain. Typically here the branch addresses of the tree
171   // will be set. It is normaly not necessary to make changes to the
172   // generated code, but the routine can be extended by the user if needed.
173   // Init() will be called many times when running with PROOF.
174    if (!tree) return;
175    if (fDebug > 0) {
176       printf("->AliAnalysisManager::Init(%s)\n", tree->GetName());
177    }
178
179    // Call InitTree of EventHandler
180    if (fOutputEventHandler) {
181       if (fMode == kProofAnalysis) {
182          fOutputEventHandler->Init(0x0, "proof");
183       } else {
184          fOutputEventHandler->Init(0x0, "local");
185       }
186    }
187
188    if (fInputEventHandler) {
189       if (fMode == kProofAnalysis) {
190          fInputEventHandler->Init(tree, "proof");
191       } else {
192          fInputEventHandler->Init(tree, "local");
193       }
194    } else {
195       // If no input event handler we need to get the tree once
196       // for the chain
197       if(!tree->GetTree()) tree->LoadTree(0);
198    }
199    
200
201    if (fMCtruthEventHandler) {
202       if (fMode == kProofAnalysis) {
203          fMCtruthEventHandler->Init(0x0, "proof");
204       } else {
205          fMCtruthEventHandler->Init(0x0, "local");
206       }
207    }
208
209    if (!fInitOK) InitAnalysis();
210    if (!fInitOK) return;
211    fTree = tree;
212    AliAnalysisDataContainer *top = (AliAnalysisDataContainer*)fInputs->At(0);
213    if (!top) {
214       Error("Init","No top input container !");
215       return;
216    }
217    top->SetData(tree);
218    if (fDebug > 0) {
219       printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
220    }
221 }
222
223 //______________________________________________________________________________
224 void AliAnalysisManager::SlaveBegin(TTree *tree)
225 {
226   // The SlaveBegin() function is called after the Begin() function.
227   // When running with PROOF SlaveBegin() is called on each slave server.
228   // The tree argument is deprecated (on PROOF 0 is passed).
229    if (fDebug > 0) printf("->AliAnalysisManager::SlaveBegin()\n");
230    static Bool_t isCalled = kFALSE;
231    TDirectory *curdir = gDirectory;
232    // Call SlaveBegin only once in case of mixing
233    if (isCalled && fMode==kMixingAnalysis) return;
234    // Call Init of EventHandler
235    if (fOutputEventHandler) {
236       if (fMode == kProofAnalysis) {
237          TIter nextout(fOutputs);
238          AliAnalysisDataContainer *c_aod;
239          while ((c_aod=(AliAnalysisDataContainer*)nextout())) if (!strcmp(c_aod->GetFileName(),"default")) break;
240          if (c_aod && c_aod->IsSpecialOutput()) {
241             // Merging via files
242             if (fDebug > 1) printf("   Initializing special output file %s...\n", fOutputEventHandler->GetOutputFileName());
243             OpenProofFile(fOutputEventHandler->GetOutputFileName(), "RECREATE");
244             c_aod->SetFile(gFile);
245             fOutputEventHandler->Init("proofspecial");
246          } else {
247             // Merging in memory
248             fOutputEventHandler->Init("proof");
249          }   
250       } else {
251          fOutputEventHandler->Init("local");
252       }
253    }
254
255    if (fInputEventHandler) {
256       fInputEventHandler->SetInputTree(tree);
257       if (fMode == kProofAnalysis) {
258          fInputEventHandler->Init("proof");
259       } else {
260          fInputEventHandler->Init("local");
261       }
262    }
263
264    if (fMCtruthEventHandler) {
265       if (fMode == kProofAnalysis) {
266          fMCtruthEventHandler->Init("proof");
267       } else {
268          fMCtruthEventHandler->Init("local");
269       }
270    }
271    if (curdir) curdir->cd();
272    
273    TIter next(fTasks);
274    AliAnalysisTask *task;
275    // Call CreateOutputObjects for all tasks
276    while ((task=(AliAnalysisTask*)next())) {
277       curdir = gDirectory;
278       task->CreateOutputObjects();
279       if (curdir) curdir->cd();
280    }
281    isCalled = kTRUE;
282
283    if (fDebug > 0) printf("<-AliAnalysisManager::SlaveBegin()\n");
284 }
285
286 //______________________________________________________________________________
287 Bool_t AliAnalysisManager::Notify()
288 {
289    // The Notify() function is called when a new file is opened. This
290    // can be either for a new TTree in a TChain or when when a new TTree
291    // is started when using PROOF. It is normaly not necessary to make changes
292    // to the generated code, but the routine can be extended by the
293    // user if needed. The return value is currently not used.
294    if (!fTree) {
295       Error("Notify","No current tree.");
296       return kFALSE;
297    }   
298    TFile *curfile = fTree->GetCurrentFile();
299    if (!curfile) {
300       Error("Notify","No current file");
301       return kFALSE;
302    }   
303    
304    if (fDebug > 0) printf("->AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
305    TIter next(fTasks);
306    AliAnalysisTask *task;
307    // Call Notify for all tasks
308    while ((task=(AliAnalysisTask*)next())) 
309       task->Notify();
310         
311    // Call Notify of the event handlers
312    if (fInputEventHandler) {
313        fInputEventHandler->Notify(curfile->GetName());
314    }
315
316    if (fOutputEventHandler) {
317        fOutputEventHandler->Notify(curfile->GetName());
318    }
319
320    if (fMCtruthEventHandler) {
321        fMCtruthEventHandler->Notify(curfile->GetName());
322    }
323    if (fDebug > 0) printf("<-AliAnalysisManager::Notify()\n");
324    return kTRUE;
325 }    
326
327 //______________________________________________________________________________
328 Bool_t AliAnalysisManager::Process(Long64_t entry)
329 {
330   // The Process() function is called for each entry in the tree (or possibly
331   // keyed object in the case of PROOF) to be processed. The entry argument
332   // specifies which entry in the currently loaded tree is to be processed.
333   // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
334   // to read either all or the required parts of the data. When processing
335   // keyed objects with PROOF, the object is already loaded and is available
336   // via the fObject pointer.
337   //
338   // This function should contain the "body" of the analysis. It can contain
339   // simple or elaborate selection criteria, run algorithms on the data
340   // of the event and typically fill histograms.
341
342   // WARNING when a selector is used with a TChain, you must use
343   //  the pointer to the current TTree to call GetEntry(entry).
344   //  The entry is always the local entry number in the current tree.
345   //  Assuming that fChain is the pointer to the TChain being processed,
346   //  use fChain->GetTree()->GetEntry(entry).
347    if (fDebug > 0) printf("->AliAnalysisManager::Process(%lld)\n", entry);
348
349    if (fInputEventHandler)   fInputEventHandler  ->BeginEvent(entry);
350    if (fOutputEventHandler)  fOutputEventHandler ->BeginEvent(entry);
351    if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
352    
353    GetEntry(entry);
354    ExecAnalysis();
355    if (fDebug > 0) printf("<-AliAnalysisManager::Process()\n");
356    return kTRUE;
357 }
358
359 //______________________________________________________________________________
360 void AliAnalysisManager::PackOutput(TList *target)
361 {
362   // Pack all output data containers in the output list. Called at SlaveTerminate
363   // stage in PROOF case for each slave.
364    if (fDebug > 0) printf("->AliAnalysisManager::PackOutput()\n");
365    if (!target) {
366       Error("PackOutput", "No target. Aborting.");
367       return;
368    }
369    if (fInputEventHandler)   fInputEventHandler  ->Terminate();
370    if (fOutputEventHandler)  fOutputEventHandler ->Terminate();
371    if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
372
373    // Call FinishTaskOutput() for each event loop task (not called for 
374    // post-event loop tasks - use Terminate() fo those)
375    TIter nexttask(fTasks);
376    AliAnalysisTask *task;
377    while ((task=(AliAnalysisTask*)nexttask())) {
378       if (!task->IsPostEventLoop()) {
379          if (fDebug > 0) printf("->FinishTaskOutput: task %s\n", task->GetName());
380          task->FinishTaskOutput();
381          if (fDebug > 0) printf("<-FinishTaskOutput: task %s\n", task->GetName());
382       }
383    }      
384    
385    if (fMode == kProofAnalysis) {
386       TIter next(fOutputs);
387       AliAnalysisDataContainer *output;
388       Bool_t isManagedByHandler = kFALSE;
389       while ((output=(AliAnalysisDataContainer*)next())) {
390          // Do not consider outputs of post event loop tasks
391          isManagedByHandler = kFALSE;
392          if (output->GetProducer()->IsPostEventLoop()) continue;
393          const char *filename = output->GetFileName();
394          if (!(strcmp(filename, "default")) && fOutputEventHandler) {
395             isManagedByHandler = kTRUE;
396             filename = fOutputEventHandler->GetOutputFileName();
397          }
398          // Check if data was posted to this container. If not, issue an error.
399          if (!output->GetData() && !isManagedByHandler) {
400             Error("PackOutput", "No data for output container %s. Forgot to PostData ?\n", output->GetName());
401             continue;
402          }   
403          if (!output->IsSpecialOutput()) {
404             // Normal outputs
405             if (strlen(filename) && !isManagedByHandler) {
406                // File resident outputs
407                TFile *file = output->GetFile();
408                // Backup current folder
409                TDirectory *opwd = gDirectory;
410                // Create file if not existing and register to container.
411                if (file) file->cd();
412                else      file = new TFile(filename, "RECREATE"); 
413                if (file->IsZombie()) {
414                   Fatal("PackOutput", "Could not recreate file %s\n", filename);
415                   return;
416                }   
417                output->SetFile(file);
418                // Clear file list to release object ownership to user.
419                file->Clear();
420                // Save data to file, then close.
421                if (output->GetData()->InheritsFrom(TCollection::Class())) {
422                   // If data is a collection, we set the name of the collection 
423                   // as the one of the container and we save as a single key.
424                   TCollection *coll = (TCollection*)output->GetData();
425                   coll->SetName(output->GetName());
426                   coll->Write(output->GetName(), TObject::kSingleKey);
427                } else {
428                   if (output->GetData()->InheritsFrom(TTree::Class())) {
429                      TTree *tree = (TTree*)output->GetData();
430                      tree->SetDirectory(file);
431                      tree->AutoSave();
432                   } else {
433                      output->GetData()->Write();
434                   }   
435                }      
436                if (fDebug > 1) printf("PackOutput %s: memory merge, file resident output\n", output->GetName());
437                if (fDebug > 2) {
438                   printf("   file %s listing content:\n", filename);
439                   file->ls();
440                }   
441                file->Close();
442                // Restore current directory
443                if (opwd) opwd->cd();
444             } else {
445                // Memory-resident outputs   
446                if (fDebug > 1) printf("PackOutput %s: memory merge memory resident output\n", filename);
447             }   
448             AliAnalysisDataWrapper *wrap = 0;
449             if (isManagedByHandler) {
450                wrap = new AliAnalysisDataWrapper(fOutputEventHandler->GetTree());
451                wrap->SetName(output->GetName());
452             }   
453             else                    wrap =output->ExportData();
454             // Output wrappers must NOT delete data after merging - the user owns them
455             wrap->SetDeleteData(kFALSE);
456             target->Add(wrap);
457          } else {
458          // Special outputs
459             TDirectory *opwd = gDirectory;
460             TFile *file = output->GetFile();
461             if (fDebug > 1 && file) printf("PackOutput %s: file merge, special output\n", output->GetName());
462             if (isManagedByHandler) {
463                // Terminate IO for files managed by the output handler
464                if (file) file->Write();
465                if (file && fDebug > 2) {
466                   printf("   handled file %s listing content:\n", file->GetName());
467                   file->ls();
468                }   
469                fOutputEventHandler->TerminateIO();
470                continue;
471             }   
472             
473             if (!file) {
474                AliAnalysisTask *producer = output->GetProducer();
475                Error("PackOutput", 
476                      "File %s for special container %s was NOT opened in %s::CreateOutputObjects !!!",
477                      output->GetFileName(), output->GetName(), producer->ClassName());
478                continue;
479             }   
480             file->cd();
481             // Release object ownership to users after writing data to file
482             if (output->GetData()->InheritsFrom(TCollection::Class())) {
483                // If data is a collection, we set the name of the collection 
484                // as the one of the container and we save as a single key.
485                TCollection *coll = (TCollection*)output->GetData();
486                coll->SetName(output->GetName());
487                coll->Write(output->GetName(), TObject::kSingleKey);
488             } else {
489                if (output->GetData()->InheritsFrom(TTree::Class())) {
490                   TTree *tree = (TTree*)output->GetData();
491                   tree->SetDirectory(file);
492                   tree->AutoSave();
493                } else {
494                   output->GetData()->Write();
495                }   
496             }      
497             file->Clear();
498             if (fDebug > 2) {
499                printf("   file %s listing content:\n", output->GetFileName());
500                file->ls();
501             }   
502             file->Close();
503             // Restore current directory
504             if (opwd) opwd->cd();
505             // Check if a special output location was provided or the output files have to be merged
506             if (strlen(fSpecialOutputLocation.Data())) {
507                TString remote = fSpecialOutputLocation;
508                remote += "/";
509                Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
510                remote += Form("%s_%d_", gSystem->HostName(), gid);
511                remote += output->GetFileName();
512                TFile::Cp(output->GetFileName(), remote.Data());
513             } else {
514             // No special location specified-> use TProofOutputFile as merging utility
515             // The file at this output slot must be opened in CreateOutputObjects
516                if (fDebug > 1) printf("   File %s to be merged...\n", output->GetFileName());
517             }
518          }      
519       }
520    } 
521    if (fDebug > 0) printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
522 }
523
524 //______________________________________________________________________________
525 void AliAnalysisManager::ImportWrappers(TList *source)
526 {
527 // Import data in output containers from wrappers coming in source.
528    if (fDebug > 0) printf("->AliAnalysisManager::ImportWrappers()\n");
529    TIter next(fOutputs);
530    AliAnalysisDataContainer *cont;
531    AliAnalysisDataWrapper   *wrap;
532    Int_t icont = 0;
533    while ((cont=(AliAnalysisDataContainer*)next())) {
534       wrap = 0;
535       if (cont->GetProducer()->IsPostEventLoop()) continue;
536       const char *filename = cont->GetFileName();
537       Bool_t isManagedByHandler = kFALSE;
538       if (!(strcmp(filename, "default")) && fOutputEventHandler) {
539          isManagedByHandler = kTRUE;
540          filename = fOutputEventHandler->GetOutputFileName();
541       }
542       if (cont->IsSpecialOutput()) {
543          if (strlen(fSpecialOutputLocation.Data()) && !isManagedByHandler) continue;
544          // Copy merged file from PROOF scratch space
545          char full_path[512];
546          TObject *pof =  source->FindObject(filename);
547          if (!pof || !pof->InheritsFrom("TProofOutputFile")) {
548             Error("ImportWrappers", "TProofOutputFile object not found in output list for container %s", cont->GetName());
549             continue;
550          }
551          gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", ((TProofOutputFile*)0x%lx)->GetOutputFileName();)", full_path, pof));
552          if (fDebug > 1) 
553             printf("   Copying file %s from PROOF scratch space\n", full_path);
554          Bool_t gotit = TFile::Cp(full_path, filename); 
555          if (!gotit) {
556             Error("ImportWrappers", "Could not get file %s from proof scratch space", cont->GetFileName());
557          }
558          // Normally we should connect data from the copied file to the
559          // corresponding output container, but it is not obvious how to do this
560          // automatically if several objects in file...
561          TFile *f = new TFile(filename, "READ");
562          TObject *obj = 0;
563          if (!isManagedByHandler) obj = f->Get(cont->GetName());
564          if (!obj && !isManagedByHandler) {
565             Error("ImportWrappers", "Could not find object %s in file %s", cont->GetName(), filename);
566             continue;
567          }
568          wrap = new AliAnalysisDataWrapper(obj);
569          wrap->SetDeleteData(kFALSE);
570       }   
571       if (!wrap) wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
572       if (!wrap) {
573          Error("ImportWrappers","Container %s not found in analysis output !", cont->GetName());
574          continue;
575       }
576       icont++;
577       if (fDebug > 1) {
578          printf("   Importing data for container %s", cont->GetName());
579          if (strlen(filename)) printf("    -> file %s\n", cont->GetFileName());
580          else printf("\n");
581       }   
582       cont->ImportData(wrap);
583    }         
584    if (fDebug > 0) printf("<-AliAnalysisManager::ImportWrappers(): %d containers imported\n", icont);
585 }
586
587 //______________________________________________________________________________
588 void AliAnalysisManager::UnpackOutput(TList *source)
589 {
590   // Called by AliAnalysisSelector::Terminate only on the client.
591    if (fDebug > 0) printf("->AliAnalysisManager::UnpackOutput()\n");
592    if (!source) {
593       Error("UnpackOutput", "No target. Aborting.");
594       return;
595    }
596    if (fDebug > 1) printf("   Source list contains %d containers\n", source->GetSize());
597
598    if (fMode == kProofAnalysis) ImportWrappers(source);
599
600    TIter next(fOutputs);
601    AliAnalysisDataContainer *output;
602    while ((output=(AliAnalysisDataContainer*)next())) {
603       if (!output->GetData()) continue;
604       // Check if there are client tasks that run post event loop
605       if (output->HasConsumers()) {
606          // Disable event loop semaphore
607          output->SetPostEventLoop(kTRUE);
608          TObjArray *list = output->GetConsumers();
609          Int_t ncons = list->GetEntriesFast();
610          for (Int_t i=0; i<ncons; i++) {
611             AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
612             task->CheckNotify(kTRUE);
613             // If task is active, execute it
614             if (task->IsPostEventLoop() && task->IsActive()) {
615                if (fDebug > 0) printf("== Executing post event loop task %s\n", task->GetName());
616                task->ExecuteTask();
617             }   
618          }
619       }   
620    }
621    if (fDebug > 0) printf("<-AliAnalysisManager::UnpackOutput()\n");
622 }
623
624 //______________________________________________________________________________
625 void AliAnalysisManager::Terminate()
626 {
627   // The Terminate() function is the last function to be called during
628   // a query. It always runs on the client, it can be used to present
629   // the results graphically.
630    if (fDebug > 0) printf("->AliAnalysisManager::Terminate()\n");
631    AliAnalysisTask *task;
632    TIter next(fTasks);
633    // Call Terminate() for tasks
634    while ((task=(AliAnalysisTask*)next())) task->Terminate();
635    //
636    TIter next1(fOutputs);
637    AliAnalysisDataContainer *output;
638    while ((output=(AliAnalysisDataContainer*)next1())) {
639       // Special outputs have the files already closed and written.
640       if (output->IsSpecialOutput()) continue;
641       const char *filename = output->GetFileName();
642       if (!(strcmp(filename, "default"))) {
643          if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
644          TFile *aodfile = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
645          if (aodfile) {
646             if (fDebug > 1) printf("Writing output handler file: %s\n", filename);
647             aodfile->Write();
648             continue;
649          }   
650       }      
651       if (!strlen(filename)) continue;
652       if (!output->GetData()) continue;
653       TFile *file = output->GetFile();
654       TDirectory *opwd = gDirectory;
655       file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
656       if (!file) file = new TFile(filename, "RECREATE");
657       if (file->IsZombie()) continue;
658       output->SetFile(file);
659       file->cd();
660       if (fDebug > 1) printf("   writing output data %s to file %s\n", output->GetData()->GetName(), file->GetName());
661       if (output->GetData()->InheritsFrom(TCollection::Class())) {
662       // If data is a collection, we set the name of the collection 
663       // as the one of the container and we save as a single key.
664          TCollection *coll = (TCollection*)output->GetData();
665          coll->SetName(output->GetName());
666          coll->Write(output->GetName(), TObject::kSingleKey);
667       } else {
668          if (output->GetData()->InheritsFrom(TTree::Class())) {
669             TTree *tree = (TTree*)output->GetData();
670             tree->SetDirectory(file);
671             tree->AutoSave();
672          } else {
673             output->GetData()->Write();
674          }   
675       }      
676       if (opwd) opwd->cd();
677    }   
678    next1.Reset();
679    while ((output=(AliAnalysisDataContainer*)next1())) {
680       // Close all files at output
681       TDirectory *opwd = gDirectory;
682       if (output->GetFile()) output->GetFile()->Close();
683       if (opwd) opwd->cd();
684    }   
685
686    if (fInputEventHandler)   fInputEventHandler  ->TerminateIO();
687    if (fOutputEventHandler)  fOutputEventHandler ->TerminateIO();
688    if (fMCtruthEventHandler) fMCtruthEventHandler->TerminateIO();
689
690    Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
691    if (getsysInfo) {
692       TDirectory *cdir = gDirectory;
693       TFile f("syswatch.root", "RECREATE");
694       if (!f.IsZombie()) {
695          TTree *tree = AliSysInfo::MakeTree("syswatch.log");
696          tree->SetMarkerStyle(kCircle);
697          tree->SetMarkerColor(kBlue);
698          tree->SetMarkerSize(0.5);
699          if (!gROOT->IsBatch()) {
700             tree->SetAlias("event", "id0");
701             tree->SetAlias("memUSED", "pI.fMemVirtual");
702             tree->SetAlias("userCPU", "pI.fCpuUser");
703             TCanvas *c = new TCanvas("SysInfo","SysInfo",10,10,800,600);
704             c->Divide(2,1,0.01,0.01);
705             c->cd(1);
706             tree->Draw("memUSED:event","","", 1234567890, 0);
707             c->cd(2);
708             tree->Draw("userCPU:event","","", 1234567890, 0);
709          }   
710          tree->Write();
711          f.Close();
712          delete tree;
713       }
714       if (cdir) cdir->cd();
715    }      
716    if (fDebug > 0) printf("<-AliAnalysisManager::Terminate()\n");
717 }
718
719 //______________________________________________________________________________
720 void AliAnalysisManager::AddTask(AliAnalysisTask *task)
721 {
722 // Adds a user task to the global list of tasks.
723    if (fTasks->FindObject(task)) {
724       Warning("AddTask", "Task %s: the same object already added to the analysis manager. Not adding.", task->GetName());
725       return;
726    }   
727    task->SetActive(kFALSE);
728    fTasks->Add(task);
729 }  
730
731 //______________________________________________________________________________
732 AliAnalysisTask *AliAnalysisManager::GetTask(const char *name) const
733 {
734 // Retreive task by name.
735    if (!fTasks) return NULL;
736    return (AliAnalysisTask*)fTasks->FindObject(name);
737 }
738
739 //______________________________________________________________________________
740 AliAnalysisDataContainer *AliAnalysisManager::CreateContainer(const char *name, 
741                                 TClass *datatype, EAliAnalysisContType type, const char *filename)
742 {
743 // Create a data container of a certain type. Types can be:
744 //   kExchangeContainer  = 0, used to exchange date between tasks
745 //   kInputContainer   = 1, used to store input data
746 //   kOutputContainer  = 2, used for posting results
747    if (fContainers->FindObject(name)) {
748       Error("CreateContainer","A container named %s already defined !\n",name);
749       return NULL;
750    }   
751    AliAnalysisDataContainer *cont = new AliAnalysisDataContainer(name, datatype);
752    fContainers->Add(cont);
753    switch (type) {
754       case kInputContainer:
755          fInputs->Add(cont);
756          break;
757       case kOutputContainer:
758          fOutputs->Add(cont);
759          if (filename && strlen(filename)) {
760             cont->SetFileName(filename);
761             cont->SetDataOwned(kFALSE);  // data owned by the file
762          }   
763          break;
764       case kExchangeContainer:
765          break;   
766    }
767    return cont;
768 }
769          
770 //______________________________________________________________________________
771 Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
772                                         AliAnalysisDataContainer *cont)
773 {
774 // Connect input of an existing task to a data container.
775    if (!fTasks->FindObject(task)) {
776       AddTask(task);
777       Info("ConnectInput", "Task %s was not registered. Now owned by analysis manager", task->GetName());
778    } 
779    Bool_t connected = task->ConnectInput(islot, cont);
780    return connected;
781 }   
782
783 //______________________________________________________________________________
784 Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
785                                         AliAnalysisDataContainer *cont)
786 {
787 // Connect output of an existing task to a data container.
788    if (!fTasks->FindObject(task)) {
789       AddTask(task);
790       Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
791    } 
792    Bool_t connected = task->ConnectOutput(islot, cont);
793    return connected;
794 }   
795                                
796 //______________________________________________________________________________
797 void AliAnalysisManager::CleanContainers()
798 {
799 // Clean data from all containers that have already finished all client tasks.
800    TIter next(fContainers);
801    AliAnalysisDataContainer *cont;
802    while ((cont=(AliAnalysisDataContainer *)next())) {
803       if (cont->IsOwnedData() && 
804           cont->IsDataReady() && 
805           cont->ClientsExecuted()) cont->DeleteData();
806    }
807 }
808
809 //______________________________________________________________________________
810 Bool_t AliAnalysisManager::InitAnalysis()
811 {
812 // Initialization of analysis chain of tasks. Should be called after all tasks
813 // and data containers are properly connected
814    // Check for input/output containers
815    fInitOK = kFALSE;
816    // Check for top tasks (depending only on input data containers)
817    if (!fTasks->First()) {
818       Error("InitAnalysis", "Analysis has no tasks !");
819       return kFALSE;
820    }   
821    TIter next(fTasks);
822    AliAnalysisTask *task;
823    AliAnalysisDataContainer *cont;
824    Int_t ntop = 0;
825    Int_t nzombies = 0;
826    Bool_t iszombie = kFALSE;
827    Bool_t istop = kTRUE;
828    Int_t i;
829    while ((task=(AliAnalysisTask*)next())) {
830       istop = kTRUE;
831       iszombie = kFALSE;
832       Int_t ninputs = task->GetNinputs();
833       for (i=0; i<ninputs; i++) {
834          cont = task->GetInputSlot(i)->GetContainer();
835          if (!cont) {
836             if (!iszombie) {
837                task->SetZombie();
838                fZombies->Add(task);
839                nzombies++;
840                iszombie = kTRUE;
841             }   
842             Error("InitAnalysis", "Input slot %d of task %s has no container connected ! Declared zombie...", 
843                   i, task->GetName()); 
844          }
845          if (iszombie) continue;
846          // Check if cont is an input container
847          if (istop && !fInputs->FindObject(cont)) istop=kFALSE;
848          // Connect to parent task
849       }
850       if (istop) {
851          ntop++;
852          fTopTasks->Add(task);
853       }
854    }
855    if (!ntop) {
856       Error("InitAnalysis", "No top task defined. At least one task should be connected only to input containers");
857       return kFALSE;
858    }                        
859    // Check now if there are orphan tasks
860    for (i=0; i<ntop; i++) {
861       task = (AliAnalysisTask*)fTopTasks->At(i);
862       task->SetUsed();
863    }
864    Int_t norphans = 0;
865    next.Reset();
866    while ((task=(AliAnalysisTask*)next())) {
867       if (!task->IsUsed()) {
868          norphans++;
869          Warning("InitAnalysis", "Task %s is orphan", task->GetName());
870       }   
871    }          
872    // Check the task hierarchy (no parent task should depend on data provided
873    // by a daughter task)
874    for (i=0; i<ntop; i++) {
875       task = (AliAnalysisTask*)fTopTasks->At(i);
876       if (task->CheckCircularDeps()) {
877          Error("InitAnalysis", "Found illegal circular dependencies between following tasks:");
878          PrintStatus("dep");
879          return kFALSE;
880       }   
881    }
882    // Check that all containers feeding post-event loop tasks are in the outputs list
883    TIter nextcont(fContainers); // loop over all containers
884    while ((cont=(AliAnalysisDataContainer*)nextcont())) {
885       if (!cont->IsPostEventLoop() && !fOutputs->FindObject(cont)) {
886          if (cont->HasConsumers()) {
887          // Check if one of the consumers is post event loop
888             TIter nextconsumer(cont->GetConsumers());
889             while ((task=(AliAnalysisTask*)nextconsumer())) {
890                if (task->IsPostEventLoop()) {
891                   fOutputs->Add(cont);
892                   break;
893                }
894             }
895          }
896       }
897    }   
898    // Check if all special output containers have a file name provided
899    TIter nextout(fOutputs);
900    while ((cont=(AliAnalysisDataContainer*)nextout())) {
901       if (cont->IsSpecialOutput() && !strlen(cont->GetFileName())) {
902          Error("InitAnalysis", "Wrong container %s : a file name MUST be provided for special outputs", cont->GetName());
903          return kFALSE;
904       }
905    }      
906    fInitOK = kTRUE;
907    return kTRUE;
908 }   
909
910 //______________________________________________________________________________
911 void AliAnalysisManager::PrintStatus(Option_t *option) const
912 {
913 // Print task hierarchy.
914    if (!fInitOK) {
915       Info("PrintStatus", "Analysis manager %s not initialized : call InitAnalysis() first", GetName());
916       return;
917    }   
918    Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
919    if (getsysInfo)
920       Info("PrintStatus", "System information will be collected each %lld events", fNSysInfo);
921    TIter next(fTopTasks);
922    AliAnalysisTask *task;
923    while ((task=(AliAnalysisTask*)next()))
924       task->PrintTask(option);
925 }
926
927 //______________________________________________________________________________
928 void AliAnalysisManager::ResetAnalysis()
929 {
930 // Reset all execution flags and clean containers.
931    CleanContainers();
932 }
933
934 //______________________________________________________________________________
935 void AliAnalysisManager::StartAnalysis(const char *type, TTree *tree, Long64_t nentries, Long64_t firstentry)
936 {
937 // Start analysis for this manager. Analysis task can be: LOCAL, PROOF, GRID or
938 // MIX. Process nentries starting from firstentry
939    if (!fInitOK) {
940       Error("StartAnalysis","Analysis manager was not initialized !");
941       return;
942    }
943    if (fDebug > 0) printf("StartAnalysis %s\n",GetName());
944    TString anaType = type;
945    anaType.ToLower();
946    fMode = kLocalAnalysis;
947    if (anaType.Contains("proof"))     fMode = kProofAnalysis;
948    else if (anaType.Contains("grid")) fMode = kGridAnalysis;
949    else if (anaType.Contains("mix"))  fMode = kMixingAnalysis;
950
951    if (fMode == kGridAnalysis) {
952       Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
953       fMode = kLocalAnalysis;
954    }
955    char line[256];
956    SetEventLoop(kFALSE);
957    // Enable event loop mode if a tree was provided
958    if (tree || fMode==kMixingAnalysis) SetEventLoop(kTRUE);
959
960    TChain *chain = 0;
961    TString ttype = "TTree";
962    if (tree && tree->IsA() == TChain::Class()) {
963       chain = (TChain*)tree;
964       if (!chain || !chain->GetListOfFiles()->First()) {
965          Error("StartAnalysis", "Cannot process null or empty chain...");
966          return;
967       }   
968       ttype = "TChain";
969    }   
970
971    // Initialize locally all tasks (happens for all modes)
972    TIter next(fTasks);
973    AliAnalysisTask *task;
974    while ((task=(AliAnalysisTask*)next())) {
975       task->LocalInit();
976    }
977    
978    switch (fMode) {
979       case kLocalAnalysis:
980          if (!tree) {
981             TIter nextT(fTasks);
982             // Call CreateOutputObjects for all tasks
983             while ((task=(AliAnalysisTask*)nextT())) {
984                TDirectory *curdir = gDirectory;
985                task->CreateOutputObjects();
986                if (curdir) curdir->cd();
987             }   
988             ExecAnalysis();
989             Terminate();
990             return;
991          } 
992          // Run tree-based analysis via AliAnalysisSelector  
993          cout << "===== RUNNING LOCAL ANALYSIS " << GetName() << " ON TREE " << tree->GetName() << endl;
994          fSelector = new AliAnalysisSelector(this);
995          tree->Process(fSelector, "", nentries, firstentry);
996          break;
997       case kProofAnalysis:
998          if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
999             printf("StartAnalysis: no PROOF!!!\n");
1000             return;
1001          }   
1002          sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1003          gROOT->ProcessLine(line);
1004          if (chain) {
1005             chain->SetProof();
1006             cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON CHAIN " << chain->GetName() << endl;
1007             chain->Process("AliAnalysisSelector", "", nentries, firstentry);
1008          } else {
1009             printf("StartAnalysis: no chain\n");
1010             return;
1011          }      
1012          break;
1013       case kGridAnalysis:
1014          Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
1015          break;
1016       case kMixingAnalysis:   
1017          // Run event mixing analysis
1018          if (!fEventPool) {
1019             Error("StartAnalysis", "Cannot run event mixing without event pool");
1020             return;
1021          }
1022          cout << "===== RUNNING EVENT MIXING ANALYSIS " << GetName() << endl;
1023          fSelector = new AliAnalysisSelector(this);
1024          while ((chain=fEventPool->GetNextChain())) {
1025             next.Reset();
1026             // Call NotifyBinChange for all tasks
1027             while ((task=(AliAnalysisTask*)next()))
1028                if (!task->IsPostEventLoop()) task->NotifyBinChange();
1029             chain->Process(fSelector);
1030          }
1031          PackOutput(fSelector->GetOutputList());
1032          Terminate();
1033    }   
1034 }   
1035
1036 //______________________________________________________________________________
1037 void AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Long64_t nentries, Long64_t firstentry)
1038 {
1039 // Start analysis for this manager on a given dataset. Analysis task can be: 
1040 // LOCAL, PROOF or GRID. Process nentries starting from firstentry.
1041    if (!fInitOK) {
1042       Error("StartAnalysis","Analysis manager was not initialized !");
1043       return;
1044    }
1045    if (fDebug > 0) printf("StartAnalysis %s\n",GetName());
1046    TString anaType = type;
1047    anaType.ToLower();
1048    if (!anaType.Contains("proof")) {
1049       Error("StartAnalysis", "Cannot process datasets in %s mode. Try PROOF.", type);
1050       return;
1051    }   
1052    fMode = kProofAnalysis;
1053    char line[256];
1054    SetEventLoop(kTRUE);
1055    // Set the dataset flag
1056    TObject::SetBit(kUseDataSet);
1057    fTree = 0;
1058
1059    // Initialize locally all tasks
1060    TIter next(fTasks);
1061    AliAnalysisTask *task;
1062    while ((task=(AliAnalysisTask*)next())) {
1063       task->LocalInit();
1064    }
1065    
1066    if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1067       printf("StartAnalysis: no PROOF!!!\n");
1068       return;
1069    }   
1070    sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1071    gROOT->ProcessLine(line);
1072    sprintf(line, "gProof->GetDataSet(\"%s\");", dataset);
1073    if (!gROOT->ProcessLine(line)) {
1074       Error("StartAnalysis", "Dataset %s not found", dataset);
1075       return;
1076    }   
1077    sprintf(line, "gProof->Process(\"%s\", \"AliAnalysisSelector\", \"\", %lld, %lld);",
1078            dataset, nentries, firstentry);
1079    cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON DATASET " << dataset << endl;
1080    gROOT->ProcessLine(line);
1081 }   
1082
1083 //______________________________________________________________________________
1084 TFile *AliAnalysisManager::OpenProofFile(const char *filename, const char *option)
1085 {
1086 // Opens a special output file used in PROOF.
1087    char line[256];
1088    if (fMode!=kProofAnalysis || !fSelector) {
1089       Error("OpenProofFile","Cannot open PROOF file %s",filename);
1090       return NULL;
1091    }   
1092    sprintf(line, "TProofOutputFile *pf = new TProofOutputFile(\"%s\");", filename);
1093    if (fDebug > 1) printf("=== %s\n", line);
1094    gROOT->ProcessLine(line);
1095    sprintf(line, "pf->OpenFile(\"%s\");", option);
1096    gROOT->ProcessLine(line);
1097    if (fDebug > 1) {
1098       gROOT->ProcessLine("pf->Print()");
1099       printf(" == proof file name: %s\n", gFile->GetName());
1100    }   
1101    sprintf(line, "((TList*)0x%lx)->Add(pf);",(ULong_t)fSelector->GetOutputList());
1102    if (fDebug > 1) printf("=== %s\n", line);
1103    gROOT->ProcessLine(line);
1104    return gFile;
1105 }   
1106
1107 //______________________________________________________________________________
1108 void AliAnalysisManager::ExecAnalysis(Option_t *option)
1109 {
1110 // Execute analysis.
1111    static Long64_t ncalls = 0;
1112    Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1113    if (getsysInfo && ncalls==0) AliSysInfo::AddStamp("Start", (Int_t)ncalls);
1114    ncalls++;
1115    if (!fInitOK) {
1116      Error("ExecAnalysis", "Analysis manager was not initialized !");
1117       return;
1118    }   
1119    AliAnalysisTask *task;
1120    // Check if the top tree is active.
1121    if (fTree) {
1122       TIter next(fTasks);
1123    // De-activate all tasks
1124       while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
1125       AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)fInputs->At(0);
1126       if (!cont) {
1127               Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
1128          return;
1129       }   
1130       cont->SetData(fTree); // This will notify all consumers
1131       Long64_t entry = fTree->GetTree()->GetReadEntry();
1132       
1133 //
1134 //    Call BeginEvent() for optional input/output and MC services 
1135       if (fInputEventHandler)   fInputEventHandler  ->BeginEvent(entry);
1136       if (fOutputEventHandler)  fOutputEventHandler ->BeginEvent(entry);
1137       if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
1138 //
1139 //    Execute the tasks
1140 //      TIter next1(cont->GetConsumers());
1141       TIter next1(fTopTasks);
1142       while ((task=(AliAnalysisTask*)next1())) {
1143          if (fDebug >1) {
1144             cout << "    Executing task " << task->GetName() << endl;
1145          }   
1146          
1147          task->ExecuteTask(option);
1148       }
1149 //
1150 //    Call FinishEvent() for optional output and MC services 
1151       if (fInputEventHandler)   fInputEventHandler  ->FinishEvent();
1152       if (fOutputEventHandler)  fOutputEventHandler ->FinishEvent();
1153       if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1154       // Gather system information if requested
1155       if (getsysInfo && ((ncalls%fNSysInfo)==0)) 
1156          AliSysInfo::AddStamp(Form("Event#%lld",ncalls),(Int_t)ncalls);
1157       return;
1158    }   
1159    // The event loop is not controlled by TSelector   
1160 //
1161 //  Call BeginEvent() for optional input/output and MC services 
1162    if (fInputEventHandler)   fInputEventHandler  ->BeginEvent(-1);
1163    if (fOutputEventHandler)  fOutputEventHandler ->BeginEvent(-1);
1164    if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(-1);
1165    TIter next2(fTopTasks);
1166    while ((task=(AliAnalysisTask*)next2())) {
1167       task->SetActive(kTRUE);
1168       if (fDebug > 1) {
1169          cout << "    Executing task " << task->GetName() << endl;
1170       }   
1171       task->ExecuteTask(option);
1172    }   
1173 //
1174 // Call FinishEvent() for optional output and MC services 
1175    if (fInputEventHandler)   fInputEventHandler  ->FinishEvent();
1176    if (fOutputEventHandler)  fOutputEventHandler ->FinishEvent();
1177    if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1178 }
1179
1180 //______________________________________________________________________________
1181 void AliAnalysisManager::FinishAnalysis()
1182 {
1183 // Finish analysis.
1184 }