]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muon/AliMuonEventCuts.cxx
Allow to store pass-dependent parameters for standard Muon track cuts in OADB (Diego)
[u/mrichter/AliRoot.git] / PWG / muon / AliMuonEventCuts.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 #include "AliMuonEventCuts.h"
17
18 #include "TMath.h"
19 #include "THashList.h"
20 #include "TObjArray.h"
21 #include "TObjString.h"
22 #include "TFile.h"
23 #include "TParameter.h"
24 #include "TKey.h"
25 #include "TSystem.h"
26 #include "TAxis.h"
27 #include "TArrayI.h"
28
29 #include "AliLog.h"
30 #include "AliInputEventHandler.h"
31 #include "AliVEvent.h"
32 #include "AliESDEvent.h"
33 #include "AliAODEvent.h"
34 #include "AliVVertex.h"
35 #include "AliCentrality.h"
36 #include "AliTimeStamp.h"
37
38 #include "AliAnalysisMuonUtility.h"
39
40 /// \cond CLASSIMP
41 ClassImp(AliMuonEventCuts) // Class implementation in ROOT context
42 /// \endcond
43
44
45 //________________________________________________________________________
46 AliMuonEventCuts::AliMuonEventCuts() :
47   AliAnalysisCuts(),
48   fPhysicsSelectionMask(0),
49   fVertexMinNContributors(0),
50   fVertexVzMin(0.),
51   fVertexVzMax(0.),
52   fDefaultTrigClassPatterns(""),
53   fSelectedTrigPattern(0x0),
54   fRejectedTrigPattern(0x0),
55   fSelectedTrigLevel(0x0),
56   fAllSelectedTrigClasses(0x0),
57   fCentralityClasses(0x0),
58   fTimeStamp(0x0),
59   fSelectedTrigClassesInEvent(0x0)
60 {
61   /// Default ctor.
62 }
63
64 //________________________________________________________________________
65 AliMuonEventCuts::AliMuonEventCuts(const char* name, const char* title ) :
66 AliAnalysisCuts(name, title),
67   fPhysicsSelectionMask(0),
68   fVertexMinNContributors(0),
69   fVertexVzMin(0.),
70   fVertexVzMax(0.),
71   fDefaultTrigClassPatterns(""),
72   fSelectedTrigPattern(new TObjArray()),
73   fRejectedTrigPattern(new TObjArray()),
74   fSelectedTrigLevel(new TObjArray()),
75   fAllSelectedTrigClasses(new THashList()),
76   fCentralityClasses(0x0),
77   fTimeStamp(0x0),
78   fSelectedTrigClassesInEvent(new TObjArray())
79 {
80   /// Constructor
81   SetDefaultParameters();
82   SetDefaultFilterMask();
83   SetDefaultTrigClassPatterns();
84   SetTrigClassLevels();
85   SetCentralityClasses();
86   fAllSelectedTrigClasses->SetOwner();
87   fSelectedTrigClassesInEvent->SetOwner();
88 }
89
90 //________________________________________________________________________
91 AliMuonEventCuts::AliMuonEventCuts(const AliMuonEventCuts& obj) :
92   AliAnalysisCuts(obj),
93   fPhysicsSelectionMask(obj.fPhysicsSelectionMask),
94   fVertexMinNContributors(obj.fVertexMinNContributors),
95   fVertexVzMin(obj.fVertexVzMin),
96   fVertexVzMax(obj.fVertexVzMax),
97   fDefaultTrigClassPatterns(obj.fDefaultTrigClassPatterns),
98   fSelectedTrigPattern(obj.fSelectedTrigPattern),
99   fRejectedTrigPattern(obj.fRejectedTrigPattern),
100   fSelectedTrigLevel(obj.fSelectedTrigLevel),
101   fAllSelectedTrigClasses(obj.fAllSelectedTrigClasses),
102   fCentralityClasses(obj.fCentralityClasses),
103   fTimeStamp(obj.fTimeStamp),
104   fSelectedTrigClassesInEvent(obj.fSelectedTrigClassesInEvent)
105 {
106   /// Copy constructor
107 }
108
109
110 //________________________________________________________________________
111 AliMuonEventCuts& AliMuonEventCuts::operator=(const AliMuonEventCuts& obj)
112 {
113   /// Assignment operator
114   if ( this != &obj ) { 
115     AliAnalysisCuts::operator=(obj);
116     fPhysicsSelectionMask = obj.fPhysicsSelectionMask;
117     fVertexMinNContributors = obj.fVertexMinNContributors,
118     fVertexVzMin = obj.fVertexVzMin;
119     fVertexVzMax = obj.fVertexVzMax;
120     fDefaultTrigClassPatterns = obj.fDefaultTrigClassPatterns;
121     fSelectedTrigPattern = obj.fSelectedTrigPattern;
122     fRejectedTrigPattern = obj.fRejectedTrigPattern;
123     fSelectedTrigLevel = obj.fSelectedTrigLevel;
124     fAllSelectedTrigClasses = obj.fAllSelectedTrigClasses;
125     fCentralityClasses = obj.fCentralityClasses;
126     fTimeStamp = obj.fTimeStamp;
127     fSelectedTrigClassesInEvent = obj.fSelectedTrigClassesInEvent;
128   }
129   return *this;
130 }
131
132
133 //________________________________________________________________________
134 AliMuonEventCuts::~AliMuonEventCuts()
135 {
136   /// Destructor
137   delete fSelectedTrigPattern;
138   delete fRejectedTrigPattern;
139   delete fSelectedTrigLevel;
140   delete fAllSelectedTrigClasses;
141   delete fSelectedTrigClassesInEvent;
142   delete fCentralityClasses;
143   delete fTimeStamp;
144 }
145
146 //________________________________________________________________________
147 Bool_t AliMuonEventCuts::IsSelected( TObject* obj )
148 {
149   /// Track is selected
150   UInt_t filterMask = GetFilterMask();
151   UInt_t selectionMask = GetSelectionMask(obj);
152   
153   return ( ( selectionMask & filterMask ) == filterMask );
154 }
155
156
157 //________________________________________________________________________
158 UInt_t AliMuonEventCuts::GetSelectionMask( const TObject* obj )
159 {
160   /// Get selection mask
161   
162   UInt_t selectionMask = 0;
163   
164   const AliInputEventHandler* inputHandler = static_cast<const AliInputEventHandler*> ( obj );
165   
166   if ( const_cast<AliInputEventHandler*>(inputHandler)->IsEventSelected() & fPhysicsSelectionMask ) selectionMask |= kPhysicsSelected;
167   
168   const AliVEvent* event = inputHandler->GetEvent();
169   
170   Double_t centrality = GetCentrality(event);
171   if ( centrality >= fCentralityClasses->GetXmin() && centrality <= fCentralityClasses->GetXmax() ) selectionMask |= kSelectedCentrality;
172   
173   UpdateEvent(event);
174   
175   if ( fSelectedTrigClassesInEvent->GetEntries() > 0 ) selectionMask |= kSelectedTrig;
176   
177   AliVVertex* vertex = AliAnalysisMuonUtility::GetVertexSPD(event);
178   if ( vertex->GetNContributors() >= GetVertexMinNContributors() && 
179       vertex->GetZ() >= GetVertexVzMin() && vertex->GetZ() <= GetVertexVzMax() ) selectionMask |= kGoodVertex;
180   
181   AliDebug(1, Form("Selection mask 0x%x\n", selectionMask));
182
183   return selectionMask;
184 }
185
186
187 //________________________________________________________________________
188 Bool_t AliMuonEventCuts::IsSelected( TList* /* list */)
189 {
190   /// Not implemented
191   AliError("Function not implemented: Use IsSelected(TObject*)");
192   return kFALSE;
193 }
194
195 //________________________________________________________________________
196 Bool_t AliMuonEventCuts::UpdateEvent ( const AliVEvent* event )
197 {
198   /// Update the transient data member per event
199   
200   AliTimeStamp currTimeStamp(event->GetOrbitNumber(), event->GetPeriodNumber(), event->GetBunchCrossNumber());
201   if ( fTimeStamp && fTimeStamp->Compare(&currTimeStamp) == 0 ) return kFALSE;
202   
203   BuildTriggerClasses(AliAnalysisMuonUtility::GetFiredTriggerClasses(event));
204   
205   delete fTimeStamp;
206   fTimeStamp = new AliTimeStamp(currTimeStamp);
207   
208   return kTRUE;
209 }
210
211 //________________________________________________________________________
212 void AliMuonEventCuts::SetDefaultTrigClassPatterns ()
213 {
214   /// Set the default patterns
215   /// (done in such a way to get all muon triggers)
216   fDefaultTrigClassPatterns = "CINT CMU CMBAC CPBI !-ACE- !-AC- !-E- !WU !EGA !EJE !PHS";
217   SetTrigClassPatterns(fDefaultTrigClassPatterns);
218 }
219
220 //________________________________________________________________________
221 void AliMuonEventCuts::SetTrigClassPatterns ( TString pattern )
222 {
223   /// Set trigger classes
224   ///
225   /// Classes are filled dynamically according to the pattern
226   /// - if name contains ! (without spaces): reject it
227   /// - otherwise, keep it
228   /// example:
229   /// SetTrigClassPatterns("CMBAC !ALLNOTRD")
230   /// keeps classes containing CMBAC, and not containing ALLNOTRD.
231   ///
232   /// CAVEAT: if you use an fCFContainer and you want an axis to contain the trigger classes,
233   ///         please be sure that each pattern matches only 1 trigger class, or triggers will be mixed up
234   ///         when merging different chuncks.
235   
236   fSelectedTrigPattern->SetOwner();
237   if ( fSelectedTrigPattern->GetEntries() > 0 ) fSelectedTrigPattern->Delete();
238   fRejectedTrigPattern->SetOwner();
239   if ( fRejectedTrigPattern->GetEntries() > 0 ) fRejectedTrigPattern->Delete();
240   
241   pattern.ReplaceAll("  "," ");
242   pattern.ReplaceAll("! ","!");
243   
244   TObjArray* fullList = pattern.Tokenize(" ");
245   
246   for ( Int_t ipat=0; ipat<fullList->GetEntries(); ++ipat ) {
247     TString currPattern = fullList->At(ipat)->GetName();
248     if ( currPattern.Contains("!") ) {
249       currPattern.ReplaceAll("!","");
250       fRejectedTrigPattern->AddLast(new TObjString(currPattern));
251     }
252     else fSelectedTrigPattern->AddLast(new TObjString(currPattern));
253   }
254   
255   delete fullList;
256 }
257
258 //________________________________________________________________________
259 void AliMuonEventCuts::SetTrigClassLevels ( TString pattern )
260 {
261   /// Set trigger cut level associated to the trigger class
262   ///
263   /// example:
264   /// SetTrigClassLevels("MSL:Lpt MSH:Hpt MUL:LptLpt")
265   ///
266   /// For the trigger classes defined in SetTrigClassPatterns
267   /// it check if they contains the keywords MSL or MSH
268   /// Hence, in the analysis, the function
269   /// TrackPtCutMatchTrigClass(track, "CPBIMSL") returns true if track match Lpt
270   /// TrackPtCutMatchTrigClass(track, "CPBIMSH") returns true if track match Hpt
271   /// TrackPtCutMatchTrigClass(track, "CMBAC") always returns true
272   
273   fSelectedTrigLevel->SetOwner();
274   if ( fSelectedTrigLevel->GetEntries() > 0 ) fSelectedTrigLevel->Delete();
275   
276   pattern.ReplaceAll("  "," ");
277   pattern.ReplaceAll(" :",":");
278   
279   TObjArray* fullList = pattern.Tokenize(" ");
280   UInt_t offset = 2;
281   for ( Int_t ipat=0; ipat<fullList->GetEntries(); ++ipat ) {
282     TString currPattern = fullList->At(ipat)->GetName();
283     TObjArray* arr = currPattern.Tokenize(":");
284     TObjString* trigClassPattern = new TObjString(arr->At(0)->GetName());
285     TString selTrigLevel = arr->At(1)->GetName();
286     selTrigLevel.ToUpper();
287     UInt_t trigLevel = 0;
288     if ( selTrigLevel.Contains("LPT") ) {
289       trigLevel = 2;
290       if ( selTrigLevel.Contains("LPTLPT") ) trigLevel += 2<<offset;
291     }
292     else if ( selTrigLevel.Contains("HPT") ) {
293       trigLevel = 3;
294       if ( selTrigLevel.Contains("HPTHPT") ) trigLevel += 3<<offset;
295     }
296     trigClassPattern->SetUniqueID(trigLevel);
297     fSelectedTrigLevel->AddLast(trigClassPattern);
298     delete arr;
299   }
300   
301   delete fullList;
302 }
303
304 //________________________________________________________________________
305 TArrayI AliMuonEventCuts::GetTrigClassPtCutLevel ( const TString trigClassName ) const
306 {
307   /// Get trigger class pt cut level for tracking/trigger matching
308   TObject* obj = fAllSelectedTrigClasses->FindObject(trigClassName.Data());
309   if ( ! obj ) {
310     AliWarning(Form("Class %s not in the list!", trigClassName.Data()));
311     return -1;
312   }
313   
314   TArrayI ptCutLevel(2);
315   ptCutLevel.Reset();
316   ptCutLevel[0] = obj->GetUniqueID() & 0x3;
317   ptCutLevel[1] = ( obj->GetUniqueID() >> 2 ) & 0x3;
318   
319   AliDebug(1,Form("Class %s ptCutLevel %i %i",trigClassName.Data(),ptCutLevel[0],ptCutLevel[1]));
320   
321   return ptCutLevel;
322 }
323
324
325 //________________________________________________________________________
326 TObjArray* AliMuonEventCuts::GetSelectedTrigClassesInEvent( const AliVEvent* event )
327 {
328   /// Return the selected trigger classes in the current event
329   UpdateEvent(event);
330   return fSelectedTrigClassesInEvent;
331 }
332
333
334 //________________________________________________________________________
335 void AliMuonEventCuts::BuildTriggerClasses ( const TString firedTrigClasses )
336 {
337   //
338   /// Return the list of trigger classes to be considered
339   /// for current event. Update the global list if necessary
340   //
341   
342   delete fSelectedTrigClassesInEvent;
343   fSelectedTrigClassesInEvent = new TObjArray(0);
344   fSelectedTrigClassesInEvent->SetOwner();
345
346   TString firedTrigClassesAny = "ANY " + firedTrigClasses;
347   TObjArray* firedTrigClassesList = firedTrigClassesAny.Tokenize(" ");
348   
349   UInt_t trigLevel = 0;
350   for ( Int_t itrig=0; itrig<firedTrigClassesList->GetEntries(); ++itrig ) {
351     TString trigName = ((TObjString*)firedTrigClassesList->At(itrig))->GetString();
352     
353     TObject* foundTrig = fAllSelectedTrigClasses->FindObject(trigName.Data());
354     if ( foundTrig ) trigLevel = foundTrig->GetUniqueID();
355     else {
356       Bool_t rejectTrig = kFALSE;
357       for ( Int_t ipat=0; ipat<fRejectedTrigPattern->GetEntries(); ++ipat ) {
358         if ( trigName.Contains(fRejectedTrigPattern->At(ipat)->GetName() ) ) {
359           rejectTrig = kTRUE;
360           break;
361         }
362       } // loop on reject pattern
363       if ( rejectTrig ) continue;
364       
365       rejectTrig = kTRUE;
366       for ( Int_t ipat=0; ipat<fSelectedTrigPattern->GetEntries(); ++ipat ) {
367         if ( trigName.Contains(fSelectedTrigPattern->At(ipat)->GetName() ) ) {
368           rejectTrig = kFALSE;
369           break;
370         }
371       } // loop on keep pattern
372       if ( rejectTrig ) continue;
373       
374       trigLevel = 0;
375       for ( Int_t ipat=0; ipat<fSelectedTrigLevel->GetEntries(); ++ipat ) {
376         if ( trigName.Contains(fSelectedTrigLevel->At(ipat)->GetName() ) ) {
377           trigLevel = fSelectedTrigLevel->At(ipat)->GetUniqueID();
378           break;
379         }
380       } // loop on trig level patterns      
381     }
382     TObjString* currTrig = new TObjString(trigName);
383     currTrig->SetUniqueID(trigLevel);
384     fSelectedTrigClassesInEvent->AddLast(currTrig);
385     
386     if ( foundTrig ) continue;
387     TObjString* addTrig = new TObjString(trigName);
388     addTrig->SetUniqueID(trigLevel);
389     fAllSelectedTrigClasses->Add(addTrig);
390     TString trigLevelInfo = Form("trig level %i ", trigLevel & 0x3);
391     trigLevelInfo += ( trigLevel > 3 ) ? "di-muon" : "single-muon";
392     AliInfo(Form("Adding %s (%s) to considered trigger classes",trigName.Data(),trigLevelInfo.Data()));
393   } // loop on trigger classes
394   
395   delete firedTrigClassesList;
396 }
397
398 //________________________________________________________________________
399 void AliMuonEventCuts::SetCentralityClasses(Int_t nCentralityBins, Double_t* centralityBins)
400 {
401   //
402   /// Set centrality classes
403   //
404   Double_t* bins = centralityBins;
405   Int_t nbins = nCentralityBins;
406   
407   Double_t defaultCentralityBins[] = {-5., 0., 5., 10., 20., 30., 40., 50., 60., 70., 80., 100., 105.};
408   if ( ! centralityBins ) {
409     bins = defaultCentralityBins;
410     nbins = sizeof(defaultCentralityBins)/sizeof(defaultCentralityBins[0])-1;
411   }
412   
413   TString centralityEstimator = "V0M";
414   if ( fCentralityClasses ) {
415     centralityEstimator = GetCentralityEstimator();
416     delete fCentralityClasses;
417   }
418   fCentralityClasses = new TAxis(nbins, bins);
419   TString currClass = "";
420   for ( Int_t ibin=1; ibin<=fCentralityClasses->GetNbins(); ++ibin ){
421     currClass = Form("%.0f_%.0f",fCentralityClasses->GetBinLowEdge(ibin),fCentralityClasses->GetBinUpEdge(ibin));
422     fCentralityClasses->SetBinLabel(ibin, currClass.Data());
423   }
424   
425   SetCentralityEstimator(centralityEstimator);
426 }
427
428 //________________________________________________________________________
429 void AliMuonEventCuts::SetCentralityEstimator ( const TString centralityEstimator )
430 {
431   /// Set centrality estimator
432   fCentralityClasses->SetName(centralityEstimator.Data());
433 }
434
435
436 //________________________________________________________________________
437 TString AliMuonEventCuts::GetCentralityEstimator () const
438 {
439   /// Get centrality estimator
440   return fCentralityClasses->GetName();
441 }
442
443 //________________________________________________________________________
444 Double_t AliMuonEventCuts::GetCentrality ( const AliVEvent* event ) const
445 {
446   /// Get centrality
447   AliVEvent* evt = const_cast<AliVEvent*>(event);
448   return evt->GetCentrality()->GetCentralityPercentile(GetCentralityEstimator());
449 }
450
451 //________________________________________________________________________
452 void AliMuonEventCuts::SetDefaultParameters ()
453 {
454   /// Standard parameters for muon event
455   SetPhysicsSelectionMask(AliVEvent::kAny);
456   SetVertexMinNContributors(1);
457   SetVertexVzLimits();
458 }
459
460
461 //________________________________________________________________________
462 void AliMuonEventCuts::SetDefaultFilterMask ()
463 {
464   /// Standard cuts for muon event
465   SetFilterMask ( kPhysicsSelected | kSelectedTrig | kGoodVertex );
466 }
467
468 //________________________________________________________________________
469 void AliMuonEventCuts::Print(Option_t* option) const
470 {
471   //
472   /// Print info
473   //
474   TString sopt(option);
475   sopt.ToLower();
476   if ( sopt.IsNull() || sopt.Contains("*") || sopt.Contains("all") ) sopt = "mask param";
477   UInt_t filterMask = GetFilterMask();
478   if ( sopt.Contains("mask") ) {
479     printf(" *** Muon event filter mask: *** \n");
480     printf("  0x%x\n", filterMask);
481     if ( filterMask & kPhysicsSelected ) printf("  Pass physics selection 0x%x\n", fPhysicsSelectionMask);
482     if ( filterMask & kSelectedCentrality ) printf(  "%g < centrality < %g", fCentralityClasses->GetXmin(), fCentralityClasses->GetXmax() );
483     if ( filterMask & kSelectedTrig )    printf("  Has selected trigger classes\n");
484     if ( filterMask & kGoodVertex )      printf("  SPD vertex with %i contributors && %g < Vz < %g\n", GetVertexMinNContributors(), GetVertexVzMin(), GetVertexVzMax());
485     printf(" ******************** \n");
486   }
487 }