- AliAnalysisTask::OpenFile(i) opens and returns the file for the container
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisManager.h
1 #ifndef ALIANALYSISMANAGER_H
2 #define ALIANALYSISMANAGER_H
3 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  * See cxx source for full Copyright notice                               */
5
6 /* $Id$ */
7 // Author: Andrei Gheata, 31/05/2006
8
9 //==============================================================================
10 //   AliAnalysysManager - Manager analysis class. Allows creation of several
11 // analysis tasks and data containers storing their input/output. Allows 
12 // connecting/chaining tasks via shared data containers. Serializes the current
13 // event for all tasks depending only on initial input data.
14 //==============================================================================
15
16 #ifndef ROOT_TNamed
17 #include <TNamed.h>
18 #endif
19
20 class TClass;
21 class TTree;
22 class AliAnalysisDataContainer;
23 class AliAnalysisTask;
24 class AliVEventHandler;
25
26
27 class AliAnalysisManager : public TNamed {
28
29 public:
30
31 enum EAliAnalysisContType {
32    kExchangeContainer  = 0,
33    kInputContainer   = 1,
34    kOutputContainer  = 2
35 };   
36
37 enum EAliAnalysisExecMode {
38    kLocalAnalysis    = 0,
39    kProofAnalysis    = 1,
40    kGridAnalysis     = 2
41 };
42
43 enum EAliAnalysisFlags {
44    kEventLoop        = BIT(14),
45    kDisableBranches  = BIT(15)
46 };   
47
48    AliAnalysisManager(const char *name = "mgr", const char *title="");
49    virtual            ~AliAnalysisManager();
50
51    AliAnalysisManager(const AliAnalysisManager& other);
52    AliAnalysisManager& operator=(const AliAnalysisManager& other);
53    
54    // Management
55    void                StartAnalysis(const char *type="local", TTree *tree=0, Long64_t nentries=1234567890, Long64_t firstentry=0);
56
57    virtual void        Init(TTree *tree);   
58    virtual Bool_t      Notify();
59    virtual void        SlaveBegin(TTree *tree);
60    virtual Bool_t      ProcessCut(Long64_t entry) {return Process(entry);}
61    virtual Bool_t      Process(Long64_t entry);
62    virtual Int_t       GetEntry(Long64_t entry, Int_t getall = 0);
63    void                PackOutput(TList *target);
64    void                UnpackOutput(TList *source);
65    virtual void        Terminate();
66
67    // Getters/Setters
68    static AliAnalysisManager *GetAnalysisManager() {return fgAnalysisManager;}
69    TObjArray          *GetContainers() const {return fContainers;}
70    UInt_t              GetDebugLevel() const {return fDebug;}
71    TObjArray          *GetInputs() const     {return fInputs;}
72    TObjArray          *GetOutputs() const    {return fOutputs;}
73    TObjArray          *GetTasks() const      {return fTasks;}
74    TObjArray          *GetTopTasks() const   {return fTopTasks;}
75    TTree              *GetTree() const       {return fTree;}
76    TObjArray          *GetZombieTasks() const {return fZombies;}
77    Long64_t            GetCurrentEntry() const {return fCurrentEntry;}
78    EAliAnalysisExecMode 
79                        GetAnalysisType() const {return fMode;}
80
81    void                SetAnalysisType(EAliAnalysisExecMode mode) {fMode = mode;}
82    void                SetCurrentEntry(Long64_t entry) {fCurrentEntry = entry;}
83    void                SetDebugLevel(UInt_t level) {fDebug = level;}
84    void                SetSpecialOutputLocation(const char *location) {fSpecialOutputLocation = location;}
85    void                SetDisableBranches(Bool_t disable=kTRUE) {TObject::SetBit(kDisableBranches,disable);}
86    void                SetCollectSysInfoEach(Int_t nevents=0) {fNSysInfo = nevents;}
87    void                SetInputEventHandler(AliVEventHandler*  handler)  {fInputEventHandler   = handler;}
88    void                SetOutputEventHandler(AliVEventHandler*  handler) {fOutputEventHandler  = handler;}
89    void                SetMCtruthEventHandler(AliVEventHandler* handler) {fMCtruthEventHandler = handler;}
90    void                SetNSysInfo(Long64_t nevents) {fNSysInfo = nevents;}
91    AliVEventHandler*   GetInputEventHandler()   {return fInputEventHandler;}
92    AliVEventHandler*   GetOutputEventHandler()  {return fOutputEventHandler;}
93    AliVEventHandler*   GetMCtruthEventHandler() {return fMCtruthEventHandler;}
94
95    // Container handling
96    AliAnalysisDataContainer *CreateContainer(const char *name, TClass *datatype, 
97                        EAliAnalysisContType type     = kExchangeContainer, 
98                        const char          *filename = NULL);
99    
100    // Including tasks and getting them
101    void                 AddTask(AliAnalysisTask *task);
102    AliAnalysisTask     *GetTask(const char *name) const;
103    
104    // Connecting data containers to task inputs/outputs
105    Bool_t               ConnectInput(AliAnalysisTask *task, Int_t islot,
106                                      AliAnalysisDataContainer *cont);
107    Bool_t               ConnectOutput(AliAnalysisTask *task, Int_t islot,
108                                      AliAnalysisDataContainer *cont);
109    // Garbage collection
110    void                 CleanContainers();
111    
112    // Analysis initialization and execution, status
113    Bool_t               InitAnalysis();
114    Bool_t               IsInitialized() const {return fInitOK;}
115    Bool_t               IsEventLoop() const {return TObject::TestBit(kEventLoop);}
116    void                 ResetAnalysis();
117    void                 ExecAnalysis(Option_t *option="");
118    void                 FinishAnalysis();
119    void                 PrintStatus(Option_t *option="all") const;
120
121 protected:
122    void                 ImportWrappers(TList *source);
123    void                 SetEventLoop(Bool_t flag=kTRUE) {TObject::SetBit(kEventLoop,flag);}
124
125 private:
126    TTree                  *fTree;                //! Input tree in case of TSelector model
127    AliVEventHandler       *fInputEventHandler;   //  Optional common input  event handler
128    AliVEventHandler       *fOutputEventHandler;  //  Optional common output event handler
129    AliVEventHandler       *fMCtruthEventHandler; //  Optional common MC Truth event handler
130    Long64_t                fCurrentEntry;        //! Current processed entry in the tree
131    Long64_t                fNSysInfo;            // Event frequency for collecting system information
132    EAliAnalysisExecMode    fMode;                // Execution mode
133    Bool_t                  fInitOK;              // Initialisation done
134    UInt_t                  fDebug;               // Debug level
135    TString                 fSpecialOutputLocation; // URL/path where the special outputs will be copied
136    TObjArray              *fTasks;               // List of analysis tasks
137    TObjArray              *fTopTasks;            // List of top tasks
138    TObjArray              *fZombies;             // List of zombie tasks
139    TObjArray              *fContainers;          // List of all containers
140    TObjArray              *fInputs;              // List of containers with input data
141    TObjArray              *fOutputs;             // List of containers with results
142
143    static AliAnalysisManager *fgAnalysisManager; //! static pointer to object instance
144    ClassDef(AliAnalysisManager,3)  // Analysis manager class
145 };   
146 #endif