a585053ad12c540dce33884d789268f2b959a92f
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisSelector.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 //   AliAnalysisSelector - A transparent selector to be created by 
21 // AliAnalysisManager to handle analysis.
22 //==============================================================================
23
24 #include "Riostream.h"
25
26 #include "AliAnalysisManager.h"
27 #include "AliAnalysisTask.h"
28 #include "AliAnalysisDataContainer.h"
29 #include "AliAnalysisSelector.h"
30
31 ClassImp(AliAnalysisSelector)
32
33 //______________________________________________________________________________
34 AliAnalysisSelector::AliAnalysisSelector(AliAnalysisManager *mgr)
35                     :TSelector(),
36                      fInitialized(kFALSE),
37                      fAnalysis(mgr)
38 {
39 // Constructor. Called by AliAnalysisManager which registers itself on the
40 // selector running on the master.
41 }
42
43 //______________________________________________________________________________
44 AliAnalysisSelector::~AliAnalysisSelector()
45 {
46 // Dtor. The analysis manager object is sent in the input list and duplicated
47 // on the workers - it needs to be deleted (?)
48 //   if (fAnalysis) delete fAnalysis;
49 }
50
51 //______________________________________________________________________________
52 void AliAnalysisSelector::Init(TTree *tree)
53 {
54 // Called after Begin/SlaveBegin, assumes that fAnalysis is already initialized.
55 // Is Init called on workers in case of PROOF.
56    if (!fAnalysis) {
57       Error("Init", "Analysis manager NULL !");
58       return;
59    }
60    if (!tree) {
61       Error("Init", "Input tree is NULL !");
62       return;
63    }
64    fAnalysis->Init(tree);
65    fInitialized = kTRUE;
66 }
67
68 //______________________________________________________________________________
69 void AliAnalysisSelector::Begin(TTree *)
70 {
71 // Assembly the input list.
72    RestoreAnalysisManager();
73 }
74
75 //______________________________________________________________________________
76 void AliAnalysisSelector::SlaveBegin(TTree *tree)
77 {
78 // Called on each worker. We "unpack" analysis manager here and call InitAnalysis.
79    RestoreAnalysisManager();
80    if (fAnalysis) fAnalysis->SlaveBegin(tree);   
81 }      
82
83 //______________________________________________________________________________
84 Bool_t AliAnalysisSelector::Process(Long64_t entry)
85 {
86 // Event loop.
87    if (fAnalysis->GetDebugLevel() >1 ) {
88       printf("AliAnalysisSelector::Process()\n");
89    }   
90    fAnalysis->GetEntry(entry); // Not needed anymore in version 2
91    fAnalysis->ExecAnalysis();
92    return kTRUE;
93 }   
94
95 //______________________________________________________________________________
96 void AliAnalysisSelector::RestoreAnalysisManager()
97 {
98 // Restores analysis manager from the input list.
99    if (!fAnalysis) {
100       TIter next(fInput);
101       TObject *obj;
102       while ((obj=next())) {
103          if (obj->IsA() == AliAnalysisManager::Class()) {
104             fAnalysis = (AliAnalysisManager*)obj;
105             break;
106          }
107       }
108       if (!fAnalysis) {
109          Error("SlaveBegin", "Analysis manager not found in the input list");
110          return;
111       }   
112    }
113 }
114
115 //______________________________________________________________________________
116 void AliAnalysisSelector::SlaveTerminate()
117 {
118   // The SlaveTerminate() function is called after all entries or objects
119   // have been processed. When running with PROOF SlaveTerminate() is called
120   // on each slave server.
121    if (fAnalysis->GetDebugLevel() >1 ) {
122       printf("AliAnalysisSelector::SlaveTerminate()\n");
123    }   
124    fAnalysis->PackOutput(fOutput);
125 }  
126
127 //______________________________________________________________________________
128 void AliAnalysisSelector::Terminate()
129 {
130   // The Terminate() function is the last function to be called during
131   // a query. It always runs on the client, it can be used to present
132   // the results graphically or save the results to file.
133    if (!fAnalysis) {
134       Error("Terminate","AliAnalysisSelector::Terminate: No analysisManager!!!");
135       return;
136    }   
137    if (fAnalysis->GetDebugLevel() >1 ) {
138       printf("AliAnalysisSelector::Terminate()\n");
139    }   
140    fAnalysis->UnpackOutput(fOutput);
141    fAnalysis->Terminate();   
142 }