Changes to the DEtaDPhi corr fun (making 0 in the center of the bin in the histogram...
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskStat.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, 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
33 ClassImp(AliAnalysisTaskStat)
34
35 //______________________________________________________________________________
36 AliAnalysisTaskStat::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 //______________________________________________________________________________
48 AliAnalysisTaskStat::~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 //______________________________________________________________________________
59 AliAnalysisTaskStat *AliAnalysisTaskStat::AddToManager(UInt_t offlineMask)
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   AliAnalysisStatistics *stat = taskStatistics->GetStatistics();
77   stat->SetOfflineMask(offlineMask);
78   mgr->SetStatistics(stat);
79   taskStatistics->SelectCollisionCandidates(offlineMask);
80   mgr->ConnectInput(taskStatistics, 0, cinput);
81   mgr->ConnectOutput(taskStatistics, 1, coutput);
82   return taskStatistics;
83 }
84
85 //______________________________________________________________________________
86 void  AliAnalysisTaskStat::UserCreateOutputObjects()
87 {
88 // Create the output list.
89   if (!fStatistics) {
90     Fatal("UserCreateOutputObjects", "You are not allowed to create this task using the dummy constructor. Use the named one.");
91   }
92   fOutputList = new TList();
93   fOutputList->SetOwner();
94   if (fStatistics) fOutputList->Add(fStatistics);
95   PostData(1, fOutputList);
96 }   
97
98 //______________________________________________________________________________
99 void  AliAnalysisTaskStat::UserExec(Option_t *)
100 {
101 // Event loop.
102   fStatistics->AddAccepted();
103 }
104
105 //______________________________________________________________________________
106 void  AliAnalysisTaskStat::Terminate(Option_t *)
107 {
108 // Get the statistics from its container and copy to manager.
109   fOutputList = dynamic_cast<TList*>(GetOutputData(1));
110   if (!fOutputList) {
111     Error("Terminate", "Cannot get output list from container");
112     return;
113   }
114   AliAnalysisStatistics *stat = dynamic_cast<AliAnalysisStatistics*>(fOutputList->At(0));
115   if (!stat) {
116     Error("Terminate", "Statistics object not found in list");
117     return;
118   }
119   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
120   if (stat != fStatistics) {
121     // Non-local mode
122     fStatistics->AddInput(stat->GetNinput());
123     fStatistics->AddProcessed(stat->GetNprocessed());
124     fStatistics->AddFailed(stat->GetNfailed());
125     fStatistics->AddAccepted(stat->GetNaccepted());
126     mgr->SetStatistics(fStatistics);
127   }
128   fStatistics->Print();
129 }