Corrected Clear method
[u/mrichter/AliRoot.git] / PWG0 / AliPhysicsSelection.cxx
CommitLineData
61899827 1/* $Id: AliPhysicsSelection.cxx 35782 2009-10-22 11:54:31Z jgrosseo $ */
2
3/**************************************************************************
4 * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Author: The ALICE Off-line Project. *
7 * Contributors are mentioned in the code where appropriate. *
8 * *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
17
18//-------------------------------------------------------------------------
19// Implementation of Class AliPhysicsSelection
528640ed 20// This class selects collision candidates from data runs, applying selection cuts on triggers
21// and background rejection based on the content of the ESD
22//
23// Usage:
24//
25// Create the object and initialize it with the correct run number:
26// fPhysicsSelection = new AliPhysicsSelection;
27// fPhysicsSelection->Initialize(104160);
28//
29// To check if an event is a collision candidate, use:
30// fPhysicsSelection->IsCollisionCandidate(fESD)
31//
32// After processing save the resulting histograms to a file with (a folder physics_selection
33// will be created that contains the histograms):
34// fPhysicsSelection->SaveHistograms("physics_selection")
35//
36// To print statistics after processing use:
37// fPhysicsSelection->Print();
38//
61899827 39// Origin: Jan Fiete Grosse-Oetringhaus, CERN
40//-------------------------------------------------------------------------
41
42#include <Riostream.h>
43#include <TH1F.h>
44#include <TH2F.h>
45#include <TList.h>
46#include <TIterator.h>
47#include <TDirectory.h>
296dd262 48#include <TObjArray.h>
61899827 49
50#include <AliPhysicsSelection.h>
51
52#include <AliTriggerAnalysis.h>
53#include <AliLog.h>
54
55#include <AliESDEvent.h>
56
57ClassImp(AliPhysicsSelection)
58
59AliPhysicsSelection::AliPhysicsSelection() :
296dd262 60 AliAnalysisCuts("AliPhysicsSelection", "AliPhysicsSelection"),
61 fCurrentRun(-1),
62 fCollTrigClasses(),
63 fBGTrigClasses(),
64 fTriggerAnalysis(),
65 fBackgroundIdentification(0),
61899827 66 fHistStatistics(0),
67 fHistBunchCrossing(0)
68{
69 // constructor
70
296dd262 71 fCollTrigClasses.SetOwner(1);
72 fBGTrigClasses.SetOwner(1);
73 fTriggerAnalysis.SetOwner(1);
74
61899827 75 AliLog::SetClassDebugLevel("AliPhysicsSelection", AliLog::kWarning);
76}
77
78AliPhysicsSelection::~AliPhysicsSelection()
79{
80 // destructor
81
296dd262 82 fCollTrigClasses.Delete();
83 fBGTrigClasses.Delete();
84 fTriggerAnalysis.Delete();
61899827 85
86 if (fHistStatistics)
87 {
88 delete fHistStatistics;
89 fHistStatistics = 0;
90 }
91
92 if (fHistBunchCrossing)
93 {
94 delete fHistBunchCrossing;
95 fHistBunchCrossing = 0;
96 }
97}
296dd262 98
99Bool_t AliPhysicsSelection::CheckTriggerClass(const AliESDEvent* aEsd, const char* trigger) const
100{
101 // checks if the given trigger class(es) are found for the current event
102 // format of trigger: +TRIGGER1 -TRIGGER2
103 // requires TRIGGER1 and rejects TRIGGER2
104
105 TString str(trigger);
106 TObjArray* tokens = str.Tokenize(" ");
107
108 for (Int_t i=0; i < tokens->GetEntries(); i++)
109 {
110 TString str2(((TObjString*) tokens->At(i))->String());
111
112 if (str2[0] != '+' && str2[0] != '-')
113 AliFatal(Form("Invalid trigger syntax: %s", trigger));
114
115 Bool_t flag = (str2[0] == '+');
116
117 str2.Remove(0, 1);
118
119 if (flag && !aEsd->IsTriggerClassFired(str2))
120 {
121 AliDebug(AliLog::kDebug, Form("Rejecting event because trigger class %s is not present", str2.Data()));
122 delete tokens;
123 return kFALSE;
124 }
125 if (!flag && aEsd->IsTriggerClassFired(str2))
126 {
127 AliDebug(AliLog::kDebug, Form("Rejecting event because trigger class %s is present", str2.Data()));
128 delete tokens;
129 return kFALSE;
130 }
131 }
132
133 delete tokens;
134 return kTRUE;
135}
61899827 136
137Bool_t AliPhysicsSelection::IsCollisionCandidate(const AliESDEvent* aEsd)
138{
139 // checks if the given event is a collision candidate
140
61899827 141 const AliESDHeader* esdHeader = aEsd->GetHeader();
142 if (!esdHeader)
143 {
144 AliError("ESD Header could not be retrieved");
145 return kFALSE;
146 }
147
148 // check event type (should be PHYSICS = 7)
149 if (esdHeader->GetEventType() != 7)
150 return kFALSE;
61899827 151
296dd262 152 if (fCurrentRun != aEsd->GetRunNumber())
153 if (!Initialize(aEsd->GetRunNumber()))
154 AliFatal(Form("Could not initialize for run %d", aEsd->GetRunNumber()));
155
156 Bool_t accept = kFALSE;
157
158 Int_t count = fCollTrigClasses.GetEntries() + fBGTrigClasses.GetEntries();
159 for (Int_t i=0; i < count; i++)
61899827 160 {
296dd262 161 const char* triggerClass = 0;
162 if (i < fCollTrigClasses.GetEntries())
163 triggerClass = ((TObjString*) fCollTrigClasses.At(i))->String();
164 else
165 triggerClass = ((TObjString*) fBGTrigClasses.At(i - fCollTrigClasses.GetEntries()))->String();
61899827 166
296dd262 167 AliDebug(AliLog::kDebug, Form("Processing trigger class %s", triggerClass));
61899827 168
296dd262 169 AliTriggerAnalysis* triggerAnalysis = static_cast<AliTriggerAnalysis*> (fTriggerAnalysis.At(i));
61899827 170
296dd262 171 triggerAnalysis->FillTriggerClasses(aEsd);
61899827 172
296dd262 173 if (CheckTriggerClass(aEsd, triggerClass))
174 {
175 triggerAnalysis->FillHistograms(aEsd);
176
177 fHistStatistics->Fill(1, i);
61899827 178
296dd262 179 Int_t fastOR = triggerAnalysis->SPDFiredChips(aEsd, 0);
180 Bool_t v0A = triggerAnalysis->IsOfflineTriggerFired(aEsd, AliTriggerAnalysis::kV0A);
181 Bool_t v0C = triggerAnalysis->IsOfflineTriggerFired(aEsd, AliTriggerAnalysis::kV0C);
f4ca8f20 182 Bool_t v0ABG = triggerAnalysis->IsOfflineTriggerFired(aEsd, AliTriggerAnalysis::kV0ABG);
183 Bool_t v0CBG = triggerAnalysis->IsOfflineTriggerFired(aEsd, AliTriggerAnalysis::kV0CBG);
184 Bool_t v0BG = v0ABG || v0CBG;
61899827 185
296dd262 186 if (fastOR > 0)
187 fHistStatistics->Fill(2, i);
188 if (fastOR > 1)
189 fHistStatistics->Fill(3, i);
190
191 if (v0A)
192 fHistStatistics->Fill(4, i);
193 if (v0C)
194 fHistStatistics->Fill(5, i);
f4ca8f20 195 if (v0ABG)
296dd262 196 fHistStatistics->Fill(6, i);
f4ca8f20 197 if (v0CBG)
198 fHistStatistics->Fill(7, i);
296dd262 199
200 if ((fastOR > 0 || v0A || v0C) && !v0BG)
f4ca8f20 201 fHistStatistics->Fill(8, i);
296dd262 202
203 if (fastOR > 0 && (v0A || v0C) && !v0BG)
f4ca8f20 204 fHistStatistics->Fill(9, i);
296dd262 205
206 if (v0A && v0C && !v0BG)
f4ca8f20 207 fHistStatistics->Fill(10, i);
296dd262 208
209 if (fastOR > 1 || (fastOR > 0 && (v0A || v0C)) || (v0A && v0C))
210 {
211 if (!v0BG)
212 {
f4ca8f20 213 fHistStatistics->Fill(11, i);
61899827 214
296dd262 215 if (fBackgroundIdentification && !fBackgroundIdentification->IsSelected(const_cast<AliESDEvent*> (aEsd)))
216 {
217 AliDebug(AliLog::kDebug, "Rejecting event because of background identification");
f4ca8f20 218 fHistStatistics->Fill(12, i);
296dd262 219 }
220 else
221 {
222 AliDebug(AliLog::kDebug, "Accepted event for histograms");
223
f4ca8f20 224 fHistStatistics->Fill(13, i);
296dd262 225 fHistBunchCrossing->Fill(aEsd->GetBunchCrossNumber(), i);
52d4048c 226 if (i < fCollTrigClasses.GetEntries())
296dd262 227 accept = kTRUE;
228 }
229 }
230 else
231 AliDebug(AliLog::kDebug, "Rejecting event because of V0 BG flag");
232 }
233 else
234 AliDebug(AliLog::kDebug, "Rejecting event because trigger condition is not fulfilled");
235 }
236 }
237
238 if (accept)
239 AliDebug(AliLog::kDebug, "Accepted event as collision candidate");
61899827 240
296dd262 241 return accept;
61899827 242}
243
244Bool_t AliPhysicsSelection::Initialize(UInt_t runNumber)
245{
246 // initializes the object for the given run
247 // TODO having the run number here and parameters hardcoded is clearly temporary, a way needs to be found to have a CDB-like configuration also for analysis
248
29e8486e 249 Bool_t oldStatus = TH1::AddDirectoryStatus();
250 TH1::AddDirectory(kFALSE);
251
296dd262 252 if (fCurrentRun != -1)
253 AliFatal("Processing several runs is not supported, yet");
254
61899827 255 AliInfo(Form("Initializing for run %d", runNumber));
296dd262 256 fCurrentRun = runNumber;
61899827 257
296dd262 258 fTriggerAnalysis.Delete();
259 fCollTrigClasses.Delete();
260 fBGTrigClasses.Delete();
61899827 261
296dd262 262 fCollTrigClasses.Add(new TObjString("+CINT1B-ABCE-NOPF-ALL"));
263 fBGTrigClasses.Add(new TObjString("+CINT1A-ABCE-NOPF-ALL"));
264 fBGTrigClasses.Add(new TObjString("+CINT1C-ABCE-NOPF-ALL"));
265 fBGTrigClasses.Add(new TObjString("+CINT1-E-NOPF-ALL"));
61899827 266
296dd262 267 Int_t count = fCollTrigClasses.GetEntries() + fBGTrigClasses.GetEntries();
268
269 for (Int_t i=0; i<count; i++)
61899827 270 {
296dd262 271 AliTriggerAnalysis* triggerAnalysis = new AliTriggerAnalysis;
272 triggerAnalysis->EnableHistograms();
273 triggerAnalysis->SetSPDGFOThreshhold(1);
274 triggerAnalysis->SetV0TimeOffset(0);
61899827 275
296dd262 276 switch (runNumber)
277 {
278 case 104316:
279 case 104320:
280 case 104321: //OK
281 case 104439:
282 triggerAnalysis->SetV0TimeOffset(7.5);
283 break;
284 }
61899827 285
296dd262 286 fTriggerAnalysis.Add(triggerAnalysis);
61899827 287 }
296dd262 288
289 if (fHistStatistics)
290 delete fHistStatistics;
291
f4ca8f20 292 fHistStatistics = new TH2F("fHistStatistics", "fHistStatistics;;", 13, 0.5, 13.5, count, -0.5, -0.5 + count);
61899827 293
296dd262 294 Int_t n = 1;
295 fHistStatistics->GetXaxis()->SetBinLabel(n++, "Correct trigger class(es)");
296 fHistStatistics->GetXaxis()->SetBinLabel(n++, "FO >= 1");
297 fHistStatistics->GetXaxis()->SetBinLabel(n++, "FO >= 2");
298 fHistStatistics->GetXaxis()->SetBinLabel(n++, "V0A");
299 fHistStatistics->GetXaxis()->SetBinLabel(n++, "V0C");
f4ca8f20 300 fHistStatistics->GetXaxis()->SetBinLabel(n++, "V0A BG");
301 fHistStatistics->GetXaxis()->SetBinLabel(n++, "V0C BG");
296dd262 302 fHistStatistics->GetXaxis()->SetBinLabel(n++, "(FO >= 1 | V0A | VOC) & !V0 BG");
303 fHistStatistics->GetXaxis()->SetBinLabel(n++, "FO >= 1 & (V0A | VOC) & !V0 BG");
304 fHistStatistics->GetXaxis()->SetBinLabel(n++, "V0A & VOC & !V0 BG");
305 fHistStatistics->GetXaxis()->SetBinLabel(n++, "(FO >= 2 | (FO >= 1 & (V0A | VOC)) | (V0A & VOC)) & !V0 BG");
306 fHistStatistics->GetXaxis()->SetBinLabel(n++, "Background identification");
307 fHistStatistics->GetXaxis()->SetBinLabel(n++, "Accepted");
308
61899827 309 if (fHistBunchCrossing)
296dd262 310 delete fHistBunchCrossing;
311
312 fHistBunchCrossing = new TH2F("fHistBunchCrossing", "fHistBunchCrossing;bunch crossing number;", 4000, -0.5, 3999.5, count, -0.5, -0.5 + count);
61899827 313
f4ca8f20 314 n = 1;
315 for (Int_t i=0; i < fCollTrigClasses.GetEntries(); i++)
316 {
317 fHistStatistics->GetYaxis()->SetBinLabel(n, ((TObjString*) fCollTrigClasses.At(i))->String());
318 fHistBunchCrossing->GetYaxis()->SetBinLabel(n, ((TObjString*) fCollTrigClasses.At(i))->String());
319 n++;
320 }
321 for (Int_t i=0; i < fBGTrigClasses.GetEntries(); i++)
322 {
323 fHistStatistics->GetYaxis()->SetBinLabel(n, ((TObjString*) fBGTrigClasses.At(i))->String());
324 fHistBunchCrossing->GetYaxis()->SetBinLabel(n, ((TObjString*) fBGTrigClasses.At(i))->String());
325 n++;
326 }
327
29e8486e 328 TH1::AddDirectory(oldStatus);
329
61899827 330 return kTRUE;
331}
332
333void AliPhysicsSelection::Print(Option_t* /* option */) const
334{
335 // print the configuration
336
b43e01ed 337 Printf("Configuration initialized for run %d:", fCurrentRun);
61899827 338
296dd262 339 Printf("Collision trigger classes:");
340 for (Int_t i=0; i < fCollTrigClasses.GetEntries(); i++)
341 Printf("%s", ((TObjString*) fCollTrigClasses.At(i))->String().Data());
61899827 342
296dd262 343 Printf("Background trigger classes:");
344 for (Int_t i=0; i < fBGTrigClasses.GetEntries(); i++)
345 Printf("%s", ((TObjString*) fBGTrigClasses.At(i))->String().Data());
346
347 AliTriggerAnalysis* triggerAnalysis = dynamic_cast<AliTriggerAnalysis*> (fTriggerAnalysis.At(0));
61899827 348
296dd262 349 if (triggerAnalysis)
350 {
351 if (triggerAnalysis->GetV0TimeOffset() > 0)
352 Printf("V0 time offset active: %.2f ns", triggerAnalysis->GetV0TimeOffset());
61899827 353
296dd262 354 Printf("\nTotal available events:");
528640ed 355
296dd262 356 triggerAnalysis->PrintTriggerClasses();
357 }
528640ed 358
b43e01ed 359 if (fHistStatistics)
360 {
361 Printf("\nSelection statistics for first collision trigger:");
362
363 Printf("Total events with correct trigger class: %d", (Int_t) fHistStatistics->GetBinContent(1, 1));
f4ca8f20 364 Printf("Selected collision candidates: %d", (Int_t) fHistStatistics->GetBinContent(13, 1));
b43e01ed 365 }
61899827 366}
367
368Long64_t AliPhysicsSelection::Merge(TCollection* list)
369{
370 // Merge a list of AliMultiplicityCorrection objects with this (needed for
371 // PROOF).
372 // Returns the number of merged objects (including this).
373
374 if (!list)
375 return 0;
376
377 if (list->IsEmpty())
378 return 1;
379
380 TIterator* iter = list->MakeIterator();
381 TObject* obj;
382
383 // collections of all histograms
384 const Int_t nHists = 9;
385 TList collections[nHists];
386
387 Int_t count = 0;
388 while ((obj = iter->Next())) {
389
390 AliPhysicsSelection* entry = dynamic_cast<AliPhysicsSelection*> (obj);
391 if (entry == 0)
392 continue;
393
394 Int_t n = 0;
296dd262 395 collections[n++].Add(&(entry->fTriggerAnalysis));
61899827 396 collections[n++].Add(entry->fHistStatistics);
397 collections[n++].Add(entry->fHistBunchCrossing);
296dd262 398 if (entry->fBackgroundIdentification)
399 collections[n++].Add(entry->fBackgroundIdentification);
61899827 400
401 count++;
402 }
403
404 Int_t n = 0;
296dd262 405 fTriggerAnalysis.Merge(&collections[n++]);
61899827 406 fHistStatistics->Merge(&collections[n++]);
407 fHistBunchCrossing->Merge(&collections[n++]);
296dd262 408 if (fBackgroundIdentification)
409 fBackgroundIdentification->Merge(&collections[n++]);
61899827 410
411 delete iter;
412
413 return count+1;
414}
415
416void AliPhysicsSelection::SaveHistograms(const char* folder) const
417{
418 // write histograms to current directory
419
420 if (!fHistStatistics)
421 return;
422
423 if (folder)
424 {
425 gDirectory->mkdir(folder);
426 gDirectory->cd(folder);
427 }
428
429 fHistStatistics->Write();
430 fHistBunchCrossing->Write();
431
296dd262 432 Int_t count = fCollTrigClasses.GetEntries() + fBGTrigClasses.GetEntries();
433 for (Int_t i=0; i < count; i++)
434 {
435 TString triggerClass = "trigger_histograms_";
436 if (i < fCollTrigClasses.GetEntries())
437 triggerClass += ((TObjString*) fCollTrigClasses.At(i))->String();
438 else
439 triggerClass += ((TObjString*) fBGTrigClasses.At(i - fCollTrigClasses.GetEntries()))->String();
61899827 440
296dd262 441 gDirectory->mkdir(triggerClass);
442 gDirectory->cd(triggerClass);
61899827 443
296dd262 444 static_cast<AliTriggerAnalysis*> (fTriggerAnalysis.At(i))->SaveHistograms();
445
446 gDirectory->cd("..");
447 }
448
449 if (fBackgroundIdentification)
450 {
451 gDirectory->mkdir("background_identification");
452 gDirectory->cd("background_identification");
453
454 fBackgroundIdentification->GetOutput()->Write();
455
456 gDirectory->cd("..");
457 }
61899827 458
459 if (folder)
460 gDirectory->cd("..");
461}