]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisStatistics.cxx
merge
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisStatistics.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 // AliAnalysisStatistics - basic class for storing statistics for the processed
21 //   events. The object is mergeable and can be used for general purpose. In case
22 //   a AliAnalysisTaskStat is used, this will set the global statistics object
23 //   to the analysis manager and will update it for the accepted events.
24 //==============================================================================
25
26 #include "AliAnalysisStatistics.h"
27
28 #include "Riostream.h"
29 #include "TObjArray.h"
30 #include "TStopwatch.h"
31
32 #include "AliVEvent.h"
33
34 using std::cout;
35 using std::endl;
36 ClassImp(AliAnalysisStatistics)
37
38 //______________________________________________________________________________
39 AliAnalysisStatistics::AliAnalysisStatistics(const AliAnalysisStatistics &other)
40       :TNamed(other),
41        fNinput(other.fNinput),
42        fNprocessed(other.fNprocessed),
43        fNfailed(other.fNfailed),
44        fNaccepted(other.fNaccepted),
45        fOfflineMask(other.fOfflineMask),
46        fMaxTasks(other.fMaxTasks),
47        fNtasks(other.fNtasks),
48        fCurrentTask(other.fCurrentTask),
49        fTaskTimeReal(0),
50        fTaskTimeCPU(0),
51        fTaskNames(0),
52        fTaskTimer(0)
53 {
54 // Copy constructor.
55   if (fNtasks) {
56     fTaskTimer = new TStopwatch();
57     fTaskTimeReal = new Double_t[fMaxTasks];
58     memset(fTaskTimeReal, 0, fMaxTasks*sizeof(Double_t));
59     memcpy(fTaskTimeReal, other.fTaskTimeReal, fNtasks*sizeof(Double_t));
60     fTaskTimeCPU = new Double_t[fMaxTasks];
61     memset(fTaskTimeCPU, 0, fMaxTasks*sizeof(Double_t));
62     memcpy(fTaskTimeCPU, other.fTaskTimeCPU, fNtasks*sizeof(Double_t));
63     fTaskNames = new TObjArray(fMaxTasks);
64     for (Int_t i=0; i<fNtasks; i++) fTaskNames->AddAt(new TObjString(other.GetTaskName(i)), i);
65   }
66 }
67
68 //______________________________________________________________________________
69 AliAnalysisStatistics &AliAnalysisStatistics::operator=(const AliAnalysisStatistics &other)
70 {
71 // Assignment.
72   if (&other == this) return *this;
73   fNinput       = other.fNinput;
74   fNprocessed   = other.fNprocessed;
75   fNfailed      = other.fNfailed;
76   fNaccepted    = other.fNaccepted;
77   fOfflineMask  = other.fOfflineMask;
78   fMaxTasks     = other.fMaxTasks;
79   fNtasks       = other.fNtasks;
80   fCurrentTask  = other.fCurrentTask;
81   fTaskTimeReal = 0;
82   fTaskTimeCPU  = 0;
83   fTaskNames    = 0;
84   fTaskTimer   = 0;
85   if (fNtasks) {
86     fTaskTimer = new TStopwatch();
87     fTaskTimeReal = new Double_t[fMaxTasks];
88     memset(fTaskTimeReal, 0, fMaxTasks*sizeof(Double_t));
89     memcpy(fTaskTimeReal, other.fTaskTimeReal, fNtasks*sizeof(Double_t));
90     fTaskTimeCPU = new Double_t[fMaxTasks];
91     memset(fTaskTimeCPU, 0, fMaxTasks*sizeof(Double_t));
92     memcpy(fTaskTimeCPU, other.fTaskTimeCPU, fNtasks*sizeof(Double_t));
93     fTaskNames = new TObjArray(fMaxTasks);
94     for (Int_t i=0; i<fNtasks; i++) fTaskNames->AddAt(new TObjString(other.GetTaskName(i)), i);
95   }  
96   return *this;
97 }
98
99 //______________________________________________________________________________
100 void AliAnalysisStatistics::StartTimer(Int_t itask, const char *name, const char *classname)
101 {
102 // Measure the CPU time done by this task in the interval
103   if (!fTaskTimer) {
104     // Create the arrays with timings with the initial size
105     if (!fMaxTasks) fMaxTasks = 100;
106     fTaskTimer = new TStopwatch();
107     fTaskTimeReal = new Double_t[fMaxTasks];
108     memset(fTaskTimeReal, 0, fMaxTasks*sizeof(Double_t));
109     fTaskTimeCPU = new Double_t[fMaxTasks];
110     memset(fTaskTimeCPU, 0, fMaxTasks*sizeof(Double_t));
111     fTaskNames = new TObjArray(fMaxTasks);
112   } else {
113   // Stop the timer if it was timing some task
114     StopTimer();
115   }  
116   
117   if (fNtasks<itask+1) {
118   // Double the array size
119     if (itask>=fMaxTasks) {
120       Int_t newsize = TMath::Max(2*fMaxTasks, itask+1);
121       Double_t *taskTimeReal = new Double_t[newsize];
122       memset(taskTimeReal, 0, newsize*sizeof(Double_t));
123       memcpy(taskTimeReal, fTaskTimeReal, fMaxTasks*sizeof(Double_t));
124       delete [] fTaskTimeReal;
125       fTaskTimeReal = taskTimeReal;
126       Double_t *taskTimeCPU = new Double_t[newsize];
127       memset(taskTimeCPU, 0, newsize*sizeof(Double_t));
128       memcpy(taskTimeCPU, fTaskTimeCPU, fMaxTasks*sizeof(Double_t));
129       delete [] fTaskTimeCPU;
130       fTaskTimeCPU = taskTimeCPU;
131       fMaxTasks = newsize;
132     }  
133     fNtasks = itask+1;
134   }
135   // Start the timer for the new task
136   fCurrentTask = itask;
137   if (!fTaskNames->At(fCurrentTask)) {
138     TString sname = name;
139     if (strlen(classname)) sname += Form("(%s)", classname);
140     fTaskNames->AddAt(new TObjString(sname), fCurrentTask);
141   }  
142   fTaskTimer->Start(kTRUE);  
143 }   
144
145 //______________________________________________________________________________
146 void AliAnalysisStatistics::StopTimer()
147 {
148 // Stop the current task timing.
149   if (fCurrentTask>=0) {
150     fTaskTimer->Stop();
151     fTaskTimeReal[fCurrentTask] += fTaskTimer->RealTime();
152     fTaskTimeCPU[fCurrentTask]  += fTaskTimer->CpuTime();
153     fCurrentTask = -1;
154   }
155 }  
156
157 //______________________________________________________________________________
158 Long64_t AliAnalysisStatistics::Merge(TCollection* list)
159 {
160 // Merge statistics objets from list on top of this.
161   TIter next(list);
162   AliAnalysisStatistics *current;
163   Long64_t count = 1;
164   while ((current = (AliAnalysisStatistics*)next())) {
165     fNinput     += current->GetNinput();
166     fNprocessed += current->GetNprocessed();
167     fNfailed    += current->GetNfailed();
168     fNaccepted  += current->GetNaccepted();
169     for (Int_t i=0; i<fNtasks; i++) {
170       fTaskTimeReal[i] += current->GetRealTime(i);
171       fTaskTimeCPU[i] += current->GetCPUTime(i);
172     }   
173   }
174   return count;
175 }
176
177 //______________________________________________________________________________
178 void AliAnalysisStatistics::Print(const Option_t *) const
179 {
180 // Print info about the processed statistics.
181   cout << "### Input events                 : " << fNinput << endl;
182   cout << "### Processed events w/o errors  : " << fNprocessed << endl;
183   cout << "### Failed events                : " << fNfailed << endl;
184   cout << "### Accepted events for mask: " << GetMaskAsString(fOfflineMask) << ": " << fNaccepted << endl;
185   if (fNtasks) {
186     cout << "Timing per task:" <<endl;
187     TString s;
188     for (Int_t i=0; i<fNtasks; i++) {
189       s = Form("%03d:  real: %9.2f   cpu: %9.2f   => %s", i,fTaskTimeReal[i], fTaskTimeCPU[i], GetTaskName(i));
190       cout << s << endl;
191     }
192   }  
193 }
194
195 //______________________________________________________________________________
196 const char *AliAnalysisStatistics::GetMaskAsString(UInt_t mask)
197 {
198 // Returns a string corresponding to the offline mask.
199    static TString smask;
200    smask = "ALL EVT.";
201    if (!mask) return smask.Data();
202    smask.Clear();
203    if (mask & AliVEvent::kMB)   smask = "MB";
204    if (mask & AliVEvent::kMUON) {
205       if (!smask.IsNull()) smask += " | ";
206       smask += "MUON";
207    }
208    if (mask & AliVEvent::kHighMult) {
209       if (!smask.IsNull()) smask += " | ";
210       smask += "HighMult";
211    }
212    if (mask & AliVEvent::kUserDefined) {
213       if (!smask.IsNull()) smask += " | ";
214       smask += "UserDefined";
215    }
216    if (mask ==  AliVEvent::kAny) smask = "ANY";
217    return smask.Data();
218 }
219
220 //______________________________________________________________________________
221 const char *AliAnalysisStatistics::GetTaskName(Int_t itask) const
222 {
223 // Returns task name
224   if (!fTaskNames || !fTaskNames->At(itask)) return 0;
225   return fTaskNames->At(itask)->GetName();
226 }
227