1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // Author: Andrei Gheata, 31/05/2006
19 //==============================================================================
20 // AliAnalysisSelector - A transparent selector to be created by
21 // AliAnalysisManager to handle analysis.
22 //==============================================================================
24 #include <Riostream.h>
25 #include <TProcessID.h>
28 #include "AliAnalysisManager.h"
29 #include "AliAnalysisTask.h"
30 #include "AliAnalysisDataContainer.h"
31 #include "AliAnalysisSelector.h"
35 ClassImp(AliAnalysisSelector)
37 //______________________________________________________________________________
38 AliAnalysisSelector::AliAnalysisSelector()
44 fAnalysis = AliAnalysisManager::GetAnalysisManager();
45 if (fAnalysis) fAnalysis->SetSelector(this);
48 //______________________________________________________________________________
49 AliAnalysisSelector::AliAnalysisSelector(AliAnalysisManager *mgr)
54 // Constructor. Called by AliAnalysisManager which registers itself on the
55 // selector running on the master.
56 mgr->SetSelector(this);
59 //______________________________________________________________________________
60 AliAnalysisSelector::~AliAnalysisSelector()
62 // Dtor. The analysis manager object is sent in the input list and duplicated
63 // on the workers - it needs to be deleted (?)
64 // if (fAnalysis) delete fAnalysis;
67 //______________________________________________________________________________
68 void AliAnalysisSelector::Init(TTree *tree)
70 // Called after Begin/SlaveBegin, assumes that fAnalysis is already initialized.
71 // Is Init called on workers in case of PROOF.
73 Error("Init", "Analysis manager NULL !");
74 Abort("Cannot initialize without analysis manager. Aborting.");
78 if (fAnalysis->GetDebugLevel()>1) {
79 cout << "->AliAnalysisSelector->Init()" << endl;
82 Error("Init", "Input tree is NULL !");
83 Abort("Cannot initialize without tree. Aborting.");
87 fInitialized = fAnalysis->Init(tree);
89 Error("Init", "Some error occured during analysis manager initialization. Aborting.");
90 Abort("Error during AliAnalysisManager::Init()");
94 if (fAnalysis->GetDebugLevel()>1) {
95 cout << "<-AliAnalysisSelector->Init()" << endl;
99 //______________________________________________________________________________
100 void AliAnalysisSelector::Begin(TTree *)
102 // Assembly the input list.
103 RestoreAnalysisManager();
104 if (fAnalysis && fAnalysis->GetDebugLevel()>1) {
105 cout << "->AliAnalysisSelector->Begin: Analysis manager restored" << endl;
106 gROOT->SetMustClean(fAnalysis->MustClean());
110 //______________________________________________________________________________
111 void AliAnalysisSelector::SlaveBegin(TTree *tree)
113 // Called on each worker. We "unpack" analysis manager here and call InitAnalysis.
114 RestoreAnalysisManager();
116 gROOT->SetMustClean(fAnalysis->MustClean());
117 if (fAnalysis->GetDebugLevel()>1) {
118 cout << "->AliAnalysisSelector->SlaveBegin() after Restore" << endl;
120 fAnalysis->SlaveBegin(tree);
121 if (fAnalysis->GetDebugLevel()>1) {
122 cout << "<-AliAnalysisSelector->SlaveBegin()" << endl;
127 //______________________________________________________________________________
128 Bool_t AliAnalysisSelector::Notify()
130 // The Notify() function is called when a new file is opened. This
131 // can be either for a new TTree in a TChain or when when a new TTree
132 // is started when using PROOF. It is normaly not necessary to make changes
133 // to the generated code, but the routine can be extended by the
134 // user if needed. The return value is currently not used.
135 if (fAnalysis) return fAnalysis->Notify();
139 //______________________________________________________________________________
140 Bool_t AliAnalysisSelector::Process(Long64_t entry)
143 static Int_t count = 0;
145 if (fAnalysis->GetDebugLevel() > 1) {
146 cout << "->AliAnalysisSelector::Process()" << endl;
148 static Bool_t init=kTRUE;
149 static Int_t nobjCount = 0;
151 nobjCount = TProcessID::GetObjectCount();
154 TProcessID::SetObjectCount(nobjCount);
155 Int_t returnCode = fAnalysis->GetEntry(entry);
156 if (returnCode <= 0) {
157 cout << "Error retrieving event:" << entry << " Skipping ..." << endl;
158 fAnalysis->CountEvent(1,0,1,0);
160 Abort("Bad stream to file. Trying next image.", kAbortFile);
163 fAnalysis->ExecAnalysis();
164 fAnalysis->CountEvent(1,1,0,0);
166 if (fAnalysis->GetDebugLevel() > 1) {
167 cout << "<-AliAnalysisSelector::Process()" << endl;
172 //______________________________________________________________________________
173 void AliAnalysisSelector::RestoreAnalysisManager()
175 // Restores analysis manager from the input list.
179 while ((obj=next())) {
180 if (obj->IsA() == AliAnalysisManager::Class()) {
181 fAnalysis = (AliAnalysisManager*)obj;
182 fAnalysis->SetSelector(this);
183 if (fAnalysis->GetDebugLevel()>1) {
184 cout << "->AliAnalysisSelector->RestoreAnalysisManager: Analysis manager restored" << endl;
190 Error("SlaveBegin", "Analysis manager not found in the input list");
196 //______________________________________________________________________________
197 void AliAnalysisSelector::SlaveTerminate()
199 // The SlaveTerminate() function is called after all entries or objects
200 // have been processed. When running with PROOF SlaveTerminate() is called
201 // on each slave server.
202 gROOT->SetMustClean(kTRUE);
203 if (fStatus == -1) return; // TSelector won't abort...
204 if (fAnalysis->GetAnalysisType() == AliAnalysisManager::kMixingAnalysis) return;
205 if (fAnalysis->GetDebugLevel() > 1) {
206 cout << "->AliAnalysisSelector::SlaveTerminate()" << endl;
208 fAnalysis->PackOutput(fOutput);
209 if (fAnalysis->GetDebugLevel() > 1) {
210 cout << "<-AliAnalysisSelector::SlaveTerminate()" << endl;
214 //______________________________________________________________________________
215 void AliAnalysisSelector::Terminate()
217 // The Terminate() function is the last function to be called during
218 // a query. It always runs on the client, it can be used to present
219 // the results graphically or save the results to file.
220 gROOT->SetMustClean(kTRUE);
221 if (fStatus == -1) return; // TSelector won't abort...
223 Error("Terminate","AliAnalysisSelector::Terminate: No analysis manager!!!");
226 // No Terminate() in case of event mixing
227 if (fAnalysis->GetAnalysisType() == AliAnalysisManager::kMixingAnalysis) return;
228 if (fAnalysis->GetDebugLevel() > 1) {
229 cout << "->AliAnalysisSelector::Terminate()" << endl;
231 fAnalysis->UnpackOutput(fOutput);
232 fAnalysis->Terminate();
233 if (fAnalysis->GetDebugLevel() > 1) {
234 cout << "<-AliAnalysisSelector::Terminate()" << endl;