6dfe3fdb7867b1b7080faa54af219a4597362411
[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 #ifndef ROOT_THashTable
20 #include <THashTable.h>
21 #endif
22 #ifndef ROOT_Riostream
23 #include <Riostream.h>
24 #endif
25
26 class TClass;
27 class TTree;
28 class TFile;
29 class TStopwatch;
30 class TMap;
31 class AliAnalysisSelector;
32 class AliAnalysisDataContainer;
33 class AliAnalysisFileDescriptor;
34 class AliAnalysisTask;
35 class AliVEventHandler;
36 class AliVEventPool;
37 class AliAnalysisGrid;
38 class AliAnalysisStatistics;
39
40
41 class AliAnalysisManager : public TNamed {
42
43 public:
44
45 enum EAliAnalysisContType {
46    kExchangeContainer  = 0,   // use to exchange data between tasks
47    kInputContainer     = 1,   // use for the task private input data
48    kOutputContainer    = 2,   // use for the task private output data
49    kParamContainer     = 3    // use for storing task private parameters/cuts per analysis session
50 };   
51
52 enum EAliAnalysisExecMode {
53    kLocalAnalysis    = 0,
54    kProofAnalysis    = 1,
55    kGridAnalysis     = 2,
56    kMixingAnalysis   = 3
57 };
58
59 enum EAliAnalysisFlags {
60    kEventLoop        = BIT(14),
61    kDisableBranches  = BIT(15),
62    kUseDataSet       = BIT(16),
63    kSaveCanvases     = BIT(17),
64    kExternalLoop     = BIT(18),
65    kSkipTerminate    = BIT(19),
66    kUseProgressBar   = BIT(20),
67    kTrueNotify       = BIT(21),
68    kTasksInitialized = BIT(22),
69    kCollectThroughput= BIT(23)
70 };
71
72    AliAnalysisManager(const char *name = "mgr", const char *title="");
73    virtual            ~AliAnalysisManager();
74
75    AliAnalysisManager(const AliAnalysisManager& other);
76    AliAnalysisManager& operator=(const AliAnalysisManager& other);
77    
78    // Event loop control
79    virtual Int_t       GetEntry(Long64_t entry, Int_t getall = 0);
80    virtual Bool_t      Init(TTree *tree);
81    virtual Bool_t      Notify();
82    virtual Bool_t      ProcessCut(Long64_t entry) {return Process(entry);}
83    virtual Bool_t      Process(Long64_t entry);
84    TFile              *OpenProofFile(AliAnalysisDataContainer *cont, const char *option, const char *extaod="");
85    static TFile       *OpenFile(AliAnalysisDataContainer *cont, const char *option, Bool_t ignoreProof=kFALSE);
86    void                PackOutput(TList *target);
87    void                RegisterExtraFile(const char *fname);
88    Long64_t            StartAnalysis(const char *type, TTree * const tree, Long64_t nentries=1234567890, Long64_t firstentry=0);
89    Long64_t            StartAnalysis(const char *type, const char *dataset, Long64_t nentries=1234567890, Long64_t firstentry=0);
90    Long64_t            StartAnalysis(const char *type, Long64_t nentries=1234567890, Long64_t firstentry=0);
91    virtual void        SlaveBegin(TTree *tree);
92    virtual void        Terminate();
93    void                UnpackOutput(TList *source);
94
95    // Getters/Setters
96    static AliAnalysisManager *GetAnalysisManager() {return fgAnalysisManager;}
97    EAliAnalysisExecMode 
98                        GetAnalysisType() const    {return fMode;}
99    void                GetAnalysisTypeString(TString &type) const;                    
100    Bool_t              GetAutoBranchLoading() const {return fAutoBranchHandling;} 
101    static const char  *GetCommonFileName()        {return fgCommonFileName.Data();}
102    AliAnalysisDataContainer *
103                        GetCommonInputContainer() const  {return fCommonInput;}
104    AliAnalysisDataContainer *
105                        GetCommonOutputContainer() const {return fCommonOutput;}
106    TObjArray          *GetContainers() const      {return fContainers;}
107    Long64_t            GetCurrentEntry() const    {return fCurrentEntry;}
108    UInt_t              GetDebugLevel() const      {return fDebug;}
109    TString             GetExtraFiles() const      {return fExtraFiles;}
110    AliVEventPool*      GetEventPool()  const      {return fEventPool;}
111    Bool_t              GetFileFromWrapper(const char *filename, const TList *source);
112    const char         *GetFileInfoLog() const     {return fFileInfoLog.Data();}
113    static Int_t        GetRunFromAlienPath(const char *path);
114    AliAnalysisGrid*    GetGridHandler()           {return fGridHandler;}
115    TObjArray          *GetInputs() const          {return fInputs;}
116    AliVEventHandler*   GetInputEventHandler() const   {return fInputEventHandler;}
117    AliVEventHandler*   GetMCtruthEventHandler() const {return fMCtruthEventHandler;}
118    Int_t               GetNsysInfo() const        {return fNSysInfo;}
119    AliVEventHandler*   GetOutputEventHandler() const  {return fOutputEventHandler;}
120    TObjArray          *GetOutputs() const         {return fOutputs;}
121    TObjArray          *GetParamOutputs() const    {return fParamCont;}
122    Int_t               GetRunFromPath() const     {return fRunFromPath;}
123    const char         *GetRequestedBranches() const {return fRequestedBranches.Data();}
124    TObjArray          *GetTasks() const           {return fTasks;}
125    TObjArray          *GetTopTasks() const        {return fTopTasks;}
126    TTree              *GetTree() const            {return fTree;}
127    TObjArray          *GetZombieTasks() const     {return fZombies;}
128    static const char  *GetGlobalStr(const char *key, Bool_t &valid);
129    static Int_t        GetGlobalInt(const char *key, Bool_t &valid);
130    static Double_t     GetGlobalDbl(const char *key, Bool_t &valid);
131    TMap               *GetGlobals()               {return fGlobals;}
132    static Bool_t       IsPipe(std::ostream &out);
133    Bool_t              IsProofMode() const        {return (fMode==kProofAnalysis)?kTRUE:kFALSE;}
134    Bool_t              IsRemote() const           {return fIsRemote;}
135    Bool_t              IsCollectThroughput()      {return TObject::TestBit(kCollectThroughput);}
136    Bool_t              IsUsingDataSet() const     {return TObject::TestBit(kUseDataSet);}
137    void                LoadBranch(const char *n)  { if(fAutoBranchHandling) return; DoLoadBranch(n); }
138    void                RunLocalInit();
139    void                SetAnalysisType(EAliAnalysisExecMode mode) {fMode = mode;}
140    void                SetAutoBranchLoading(Bool_t b) { fAutoBranchHandling = b; }
141    void                SetCurrentEntry(Long64_t entry)            {fCurrentEntry = entry;}
142    void                SetCollectSysInfoEach(Int_t nevents=0)     {fNSysInfo = nevents;}
143    void                SetCollectThroughput(Bool_t flag)          {Changed(); TObject::SetBit(kCollectThroughput,flag);}
144    static void         SetCommonFileName(const char *name)        {fgCommonFileName = name;}
145    void                SetDebugLevel(UInt_t level);
146    void                SetDisableBranches(Bool_t disable=kTRUE)   {Changed(); TObject::SetBit(kDisableBranches,disable);}
147    void                SetExternalLoop(Bool_t flag)               {Changed(); TObject::SetBit(kExternalLoop,flag);}
148    void                SetEventPool(AliVEventPool* const epool)   {Changed(); fEventPool = epool;}
149    void                SetFileInfoLog(const char *name) {TObject::SetBit(kCollectThroughput,kTRUE); fFileInfoLog = name;}
150    void                SetGridHandler(AliAnalysisGrid * const handler) {Changed(); fGridHandler = handler;}
151    void                SetInputEventHandler(AliVEventHandler* const handler);
152    void                SetMCtruthEventHandler(AliVEventHandler* const handler) {Changed(); fMCtruthEventHandler = handler;}
153    void                SetNSysInfo(Long64_t nevents)              {fNSysInfo = nevents;}
154    void                SetOutputEventHandler(AliVEventHandler* const handler);
155    void                SetRunFromPath(Int_t run)                  {fRunFromPath = run;}
156    void                SetSelector(AliAnalysisSelector * const sel)      {fSelector = sel;}
157    void                SetSaveCanvases(Bool_t flag=kTRUE)         {TObject::SetBit(kSaveCanvases,flag);}
158    void                SetSkipTerminate(Bool_t flag)              {TObject::SetBit(kSkipTerminate,flag);}
159    void                SetUseProgressBar(Bool_t flag, Int_t freq=1);
160    void                SetSpecialOutputLocation(const char *loc)  {fSpecialOutputLocation = loc;}
161    static void         SetGlobalStr(const char *key, const char *value);
162    static void         SetGlobalInt(const char *key, Int_t value);
163    static void         SetGlobalDbl(const char *key, Double_t value);
164    
165
166    // Container handling
167    AliAnalysisDataContainer *CreateContainer(const char *name, TClass *datatype, 
168                        EAliAnalysisContType type     = kExchangeContainer, 
169                        const char          *filename = NULL);
170    
171    // Including tasks and getting them
172    void                 AddTask(AliAnalysisTask *task);
173    AliAnalysisTask     *GetTask(const char *name) const;
174    
175    // Connecting data containers to task inputs/outputs
176    Bool_t               ConnectInput(AliAnalysisTask *task, Int_t islot,
177                                      AliAnalysisDataContainer *cont);
178    Bool_t               ConnectOutput(AliAnalysisTask *task, Int_t islot,
179                                      AliAnalysisDataContainer *cont);
180    // Garbage collection
181    void                 CleanContainers();
182    
183    // Analysis initialization and execution, status
184    void                 AddBranches(const char *branches);
185    void                 AddStatisticsTask(UInt_t offlineMask=0);
186    void                 CheckBranches(Bool_t load=kFALSE);
187    Bool_t               CheckTasks() const;
188    void                 CountEvent(Int_t ninput, Int_t nprocessed, Int_t nfailed, Int_t naccepted);
189    Bool_t               InitAnalysis();
190    Bool_t               IsInitialized() const {return fInitOK;}
191    Bool_t               IsTrainInitialized() const {return TObject::TestBit(kTasksInitialized);}
192    Bool_t               IsExternalLoop() const {return TObject::TestBit(kExternalLoop);}
193    Bool_t               IsEventLoop() const {return TObject::TestBit(kEventLoop);}
194    Bool_t               IsSkipTerminate() const {return TObject::TestBit(kSkipTerminate);}
195    Bool_t               MustClean() const {return fMustClean;}
196    void                 SetMustClean(Bool_t flag=kTRUE) {fMustClean = flag;}
197    void                 ResetAnalysis();
198    void                 ExecAnalysis(Option_t *option="");
199    void                 PrintStatus(Option_t *option="all") const;
200    void                 ProfileTask(const char *name, const char *option="VM") const;
201    void                 ProfileTask(Int_t itop, const char *option="VM") const;
202    static void          ProgressBar(const char *opname, Long64_t current, Long64_t size, TStopwatch * const watch=0, Bool_t last=kFALSE, Bool_t refresh=kFALSE);
203    void                 AddStatisticsMsg(const char *line);
204    const char          *GetStatisticsMsg() const {return fStatisticsMsg.Data();}
205    const AliAnalysisStatistics *GetStatistics() const {return fStatistics;}
206    void                 SetStatistics(AliAnalysisStatistics *stat) {fStatistics = stat;}
207    void                 WriteStatisticsMsg(Int_t nevents);
208    Int_t                GetNcalls() const {return fNcalls;}
209    Bool_t               ValidateOutputFiles() const;
210    
211    static const char*   GetOADBPath();
212
213    void                 ApplyDebugOptions();
214    void                 AddClassDebug(const char *className, Int_t debugLevel);
215    
216    // Security
217    Bool_t               IsLocked() const {return fLocked;}
218    void                 Lock();
219    void                 UnLock();
220    void                 Changed();
221 protected:
222    void                 ImportWrappers(TList *source);
223    void                 SetEventLoop(Bool_t flag=kTRUE) {TObject::SetBit(kEventLoop,flag);}
224    void                 DoLoadBranch(const char *name);
225
226 private:
227    TTree                  *fTree;                //! Input tree in case of TSelector model
228    AliVEventHandler       *fInputEventHandler;   //  Optional common input  event handler
229    AliVEventHandler       *fOutputEventHandler;  //  Optional common output event handler
230    AliVEventHandler       *fMCtruthEventHandler; //  Optional common MC Truth event handler
231    AliVEventPool          *fEventPool;           //  Event pool for mixing analysis
232    Long64_t                fCurrentEntry;        //! Current processed entry in the tree
233    Long64_t                fNSysInfo;            // Event frequency for collecting system information
234    EAliAnalysisExecMode    fMode;                // Execution mode
235    Bool_t                  fInitOK;              // Initialisation done
236    Bool_t                  fMustClean;           // Flag to let ROOT do cleanup
237    Bool_t                  fIsRemote;            //! Flag is set for remote analysis
238    Bool_t                  fLocked;              //! Lock for the manager and handlers
239    UInt_t                  fDebug;               // Debug level
240    TString                 fSpecialOutputLocation; // URL/path where the special outputs will be copied
241    TObjArray              *fTasks;               // List of analysis tasks
242    TObjArray              *fTopTasks;            // List of top tasks
243    TObjArray              *fZombies;             // List of zombie tasks
244    TObjArray              *fContainers;          // List of all containers
245    TObjArray              *fInputs;              // List of containers with input data
246    TObjArray              *fOutputs;             // List of containers with results
247    TObjArray              *fParamCont;           // List of containers with results
248    TObjArray              *fDebugOptions;        // List of debug options
249    TObjArray              *fFileDescriptors;     //! List of file descriptors
250    AliAnalysisFileDescriptor *fCurrentDescriptor; //! Current file descriptor
251    AliAnalysisDataContainer *fCommonInput;       // Common input container
252    AliAnalysisDataContainer *fCommonOutput;      // Common output container
253    AliAnalysisSelector    *fSelector;            //! Current selector
254    AliAnalysisGrid        *fGridHandler;         //! Grid handler plugin
255    TString                 fExtraFiles;          // List of extra files to be merged
256    TString                 fFileInfoLog;         // File name for fileinfo logs
257    Bool_t                  fAutoBranchHandling;  // def=kTRUE, turn off if you use LoadBranch
258    THashTable              fTable;               // keep branch ptrs in case of manual branch loading
259    Int_t                   fRunFromPath;         // Run number retrieved from path to input data
260    Int_t                   fNcalls;              // Total number of calls (events) of ExecAnalysis
261    Long64_t                fMaxEntries;          // Maximum number of entries
262    static Int_t            fPBUpdateFreq;        // Progress bar update freq.
263    TString                 fStatisticsMsg;       // Statistics user message
264    TString                 fRequestedBranches;   // Requested branch names
265    AliAnalysisStatistics  *fStatistics;          // Statistics info about input events
266    TMap                   *fGlobals;             // Map with global variables
267    TStopwatch             *fIOTimer;             //! Timer for I/O + deserialization
268    TStopwatch             *fCPUTimer;            //! Timer for useful processing
269    TStopwatch             *fInitTimer;           //! Timer for initialization
270    Double_t                fIOTime;              //! Cumulated time in IO
271    Double_t                fCPUTime;             //! Cumulated time in Exec
272    Double_t                fInitTime;            //! Cumulated time in initialization
273    static TString          fgCommonFileName;     //! Common output file name (not streamed)
274    static AliAnalysisManager *fgAnalysisManager; //! static pointer to object instance
275    ClassDef(AliAnalysisManager,17)  // Analysis manager class
276 };   
277 #endif