new method mgr->AddStatisticsTask() that adds a special framework task to any train...
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskStat.cxx
CommitLineData
f5cbe261 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, 20/12/2010
18
19//==============================================================================
20// AliAnalysisTaskStat - basic task that attaches a AliAnalysisTaskstatistics
21// object to the analysis manager. Use: AliAnalysisManager::AddStatisticsTask
22// to attach to a train.
23//==============================================================================
24
25#include "AliAnalysisTaskStat.h"
26
27#include <TList.h>
28#include "AliVEvent.h"
29#include "AliAnalysisManager.h"
30#include "AliAnalysisDataContainer.h"
31#include "AliAnalysisStatistics.h"
32
33ClassImp(AliAnalysisTaskStat)
34
35//______________________________________________________________________________
36AliAnalysisTaskStat::AliAnalysisTaskStat(const char *name)
37 :AliAnalysisTaskSE(name),
38 fStatistics(0),
39 fOutputList(0)
40{
41// Named constructor.
42 DefineOutput(1, TList::Class());
43 fBranchNames = "ESD:AliESDHeader. AOD:header";
44 fStatistics = new AliAnalysisStatistics("MgrStat");
45}
46
47//______________________________________________________________________________
48AliAnalysisTaskStat::~AliAnalysisTaskStat()
49{
50// Destructor.
51 if (fOutputList) {
52 if (!AliAnalysisManager::GetAnalysisManager()->IsProofMode()) delete fOutputList;
53 } else {
54 if (fStatistics) delete fStatistics;
55 }
56}
57
58//______________________________________________________________________________
59AliAnalysisTaskStat *AliAnalysisTaskStat::AddToManager()
60{
61// Add this task to the analysis manager. By default it selects MB events.
62 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
63 if (!mgr) {
64 ::Error("AliAnalysisTaskStat::AddToManager", "You need a manager first");
65 return 0;
66 }
67 AliAnalysisDataContainer *cinput = mgr->GetCommonInputContainer();
68 if (!cinput) {
69 ::Error("AliAnalysisTaskStat::AddToManager", "Attach first the input handler");
70 return 0;
71 }
72 AliAnalysisDataContainer *coutput = mgr->CreateContainer("MgrStat", TList::Class(), AliAnalysisManager::kOutputContainer,
73 mgr->GetCommonFileName());
74 AliAnalysisTaskStat *taskStatistics = new AliAnalysisTaskStat("MgrStat");
75 mgr->AddTask(taskStatistics);
76 taskStatistics->SelectCollisionCandidates(AliVEvent::kMB);
77 mgr->ConnectInput(taskStatistics, 0, cinput);
78 mgr->ConnectOutput(taskStatistics, 1, coutput);
79 return taskStatistics;
80}
81
82//______________________________________________________________________________
83void AliAnalysisTaskStat::UserCreateOutputObjects()
84{
85// Create the output list.
86 if (!fStatistics) {
87 Fatal("UserCreateOutputObjects", "You are not allowed to create this task using the dummy constructor. Use the named one.");
88 }
89 fOutputList = new TList();
90 fOutputList->SetOwner();
91 if (fStatistics) fOutputList->Add(fStatistics);
92 PostData(1, fOutputList);
93}
94
95//______________________________________________________________________________
96void AliAnalysisTaskStat::UserExec(Option_t *)
97{
98// Event loop.
99 fStatistics->AddAccepted();
100}
101
102//______________________________________________________________________________
103void AliAnalysisTaskStat::Terminate(Option_t *)
104{
105// Get the statistics from its container and copy to manager.
106 fOutputList = dynamic_cast<TList*>(GetOutputData(1));
107 if (!fOutputList) {
108 Error("Terminate", "Cannot get output list from container");
109 return;
110 }
111 AliAnalysisStatistics *stat = dynamic_cast<AliAnalysisStatistics*>(fOutputList->At(0));
112 if (!stat) {
113 Error("Terminate", "Statistics object not found in list");
114 return;
115 }
116 if (fStatistics) {
117 fStatistics->AddInput(stat->GetNinput());
118 fStatistics->AddProcessed(stat->GetNprocessed());
119 fStatistics->AddFailed(stat->GetNfailed());
120 fStatistics->AddAccepted(stat->GetNaccepted());
121 fStatistics->Print();
122 }
123}