ATLAS Offline Software
HFORSelectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /*
6  * HFORSelectionTool.cxx
7  *
8  * Created on: 15 de mai de 2016
9  * Author: Marco Leite (leite@cern.ch)
10  *
11  * This tool implements the Heavy Flavor Overlap removal as a SelectionTool. This only mkaes sense to
12  * use in AlpgenPythia6 samples where light and HF samples are mixed together (so the same event topology is
13  * not double counted.
14  *
15  * See https://twiki.cern.ch/twiki/bin/viewauth/AtlasProtected/HforTool for the Run1 version of the tool
16  *
17  * The heavy lifting will be done by HFOR_Truth class. This should be isolated from the normal user and all
18  * interfacing and setup is done through the tool framework .
19  *
20  * TODO: List
21  * TODO: 1) Improve documentation around the code
22  * TODO: 2) Provide a "tap" method to debug the HFOR_Truth class (eg histograms of deltaR, quark masses etc.)
23  * TODO: 3) Implement selection based on other discrimination methods. This requires further studies
24  *
25  */
26 #include <string>
27 
29 
32 #include "HFORTools/HFOR_Truth.h"
33 
34 
35 //==============================================================================
37  : asg::AsgMetadataTool( name ),
38  m_sampleType (HFORType::noType),
39  m_decisionType (HFORType::noType),
40  m_sampleRunNumber (0),
41  m_sampleName ("unknown"),
42  m_evtCounterAll (0),
43  m_evtCounterKilled (0),
44  m_isConfigured (false) {
45  declareProperty( "MatchCone", m_matchCone = 0.4 );
46  declareProperty( "runConfigFile", m_runConfigFile = "HFORTools/mc15_AlpgenPythia_2016.cfg" ) ;
47  declareProperty( "HFORStrategy", m_HFORStrategy = "DRBased" );
48  declareProperty( "TruthJetCollectionName", m_truthJetCollectionName = "AntiKt4TruthWZJets" );
49 }
50 //==============================================================================
51 
52 //==============================================================================
53 // During the initialization, the toll checks for the strategy to be used
54 // (jet based or angular overlap removal) and loads the configuration file
55 // with the list of the samples, setting the present analysis sample accordingly
56 //==============================================================================
58 
59  // Greet the user:
60  ATH_MSG_INFO( __PRETTY_FUNCTION__ << ": Starting tool initialization ..." );
61  ATH_MSG_INFO( __PRETTY_FUNCTION__ << ": Using matching cone deltaR = " << m_matchCone );
62  ATH_MSG_INFO(" ") ;
63  ATH_MSG_INFO( __PRETTY_FUNCTION__ << " ************************************ " );
64  ATH_MSG_INFO( __PRETTY_FUNCTION__ << " ** THIS TOOL IS UNDER DEVELOPMENT ** " );
65  ATH_MSG_INFO( __PRETTY_FUNCTION__ << " ************************************ " );
66  ATH_MSG_INFO(" ") ;
67 
68  //Initialize the Overlap Removal engine
70 
71  if(m_HFORStrategy == "DRBased"){
73  ATH_MSG_INFO( __PRETTY_FUNCTION__ << " ** Angular-Based HFOR ** " );
74  } else if(m_HFORStrategy == "JetBased"){
76  ATH_MSG_INFO( __PRETTY_FUNCTION__ << " ** Jet-Based HFOR ** " );
77  } else {
78  ATH_MSG_ERROR( __PRETTY_FUNCTION__ << " ** HFORStrategy is not known! ** " );
79  return StatusCode::FAILURE;
80  }
81 
82  //FIXME: Check the return of this also ..
85 
86  ATH_MSG_INFO( __PRETTY_FUNCTION__ << ": Initialization done.");
87  m_isConfigured = false ;
88 
89  return StatusCode::SUCCESS ;
90 }
91 //==============================================================================
92 
93 //==============================================================================
94 // Make sure we clean the the configuration information, forcing the call to
95 // setSampleType when the next file is opened.
96 //==============================================================================
98 
99  //ATH_MSG_DEBUG( __PRETTY_FUNCTION__ << " File change detected, will check the sample type " ) ;
100  //ATH_CHECK( setSampleType() ) ;
101  m_isConfigured = false ;
102 
103  return StatusCode::SUCCESS ;
104 }
105 //==============================================================================
106 
107 //==============================================================================
108 // At the end of processing, we may want to access the bookkeeping information
109 // of how many events are killed/passed. Also makes sure we unconfigure for
110 // the next file forcing the call to setSampleType when the next file is opened.
111 //==============================================================================
113 
114  ATH_MSG_DEBUG( __PRETTY_FUNCTION__ << " End Of File detected ");
115  m_isConfigured = false ;
116 
117  if (m_evtCounterAll != 0) {
118  ATH_MSG_DEBUG(" ") ;
119  ATH_MSG_DEBUG("****************************************************************************") ;
120  ATH_MSG_DEBUG(__PRETTY_FUNCTION__ << ": Sample type : " << m_sampleName ) ;
121  ATH_MSG_DEBUG(__PRETTY_FUNCTION__ << ": Total Events : " << m_evtCounterAll );
122  ATH_MSG_DEBUG(__PRETTY_FUNCTION__ << ": Killed Events : " << m_evtCounterKilled <<
123  " (" << 100*float(m_evtCounterKilled)/float(m_evtCounterAll) << "%)");
124  ATH_MSG_DEBUG("****************************************************************************") ;
125  ATH_MSG_DEBUG(" ") ;
126  }
127 
128  //Reset the counters
129  m_evtCounterAll = 0;
130  m_evtCounterKilled = 0;
131 
132  return StatusCode::SUCCESS ;
133 }
134 
135 //==============================================================================
136 
137 //==============================================================================
138 // This method will read the Truth information (particle and/or truthJet
139 // container and decide if this event should be killed or kept in face of the
140 // overlap found by the HFOR_Truth method. It does nothing if the file is
141 // not an AlpgenPythia6 sample. Also keeps track of how many events passed or
142 // killed
143 //==============================================================================
145 
146  if (! m_isConfigured) {
147  ATH_CHECK( setSampleType(), false ) ;
148  m_isConfigured = true ;
149  }
150 
151  //This toll will tell if this event is to removed (returns 0) or kept
152  //(returns 1)
153  //In case of error (container not present) it returns -1
154 
155  bool selected = true ;
156 
158 
159  // Only run this function on Monte Carlo
160  const xAOD::EventInfo* eventInfo = nullptr;
161  ATH_CHECK( evtStore()->retrieve(eventInfo, "EventInfo"), false );
162  const bool isSim = eventInfo->eventType(xAOD::EventInfo::EventType::IS_SIMULATION);
163  if ( !isSim ) {
164  ATH_MSG_DEBUG (__PRETTY_FUNCTION__ << "It is a data event... nothing to be done...");
165  return selected;
166  }
167 
168 
169  //===========================
170  // Truth Event
171  //===========================
172  std::string truthCollectionName = "TruthEvents" ;
173  const xAOD::TruthEventContainer* truthEvent = nullptr ;
174  ATH_CHECK ( evtStore()->retrieve(truthEvent, truthCollectionName ), false ) ;
175 
176  //=========================================
177  // truth JET info (not needed for DR-HFOR)
178  //=========================================
179  const xAOD::JetContainer* jets = nullptr ;
181 
182  //=========================================================
183  // Truth Event DeltaR based and Jet Based switched in init
184  //=========================================================
185  m_decisionType = m_hforTruth.findOverlap(*truthEvent, jets) ;
186 
187 
188  ATH_MSG_DEBUG( __PRETTY_FUNCTION__ << " Event Type " << (unsigned int) m_decisionType) ;
189 
190  selected = m_decisionType != HFORType::kill ;
191 
192  //BookKeeping of how many kills/passed we have.
193  //TODO: create a dictionary with key as sample type so we can count per type,
194  //and per run. This allows for debugging
195  m_evtCounterAll++ ;
196  if (! selected) {
198  }
199 
200  return selected ;
201 
202 }
203 //==============================================================================
204 
205 //==============================================================================
206 // Extract the MC sample inforation from EventInfo container and using the
207 // configuration file classifies the sample. Given the fact that some derivation
208 // change the run number, we use the mcChannelNumber, failing back to run number
209 // for truth derivations as in these the mcChannelNumber is 0. Heavy Flavor
210 // Overlap removal technique will depend on it. To avoid any surprises, it is
211 // good to check it at every file change. Due to the definition of tool
212 // Property, this cannot be a Property of the tool (it will require
213 // re-initialization of the tool).
214 //==============================================================================
216 
217  //Check what type of MC sample we have.
218 
219  //=============
220  // Event Info
221  //=============
222  const xAOD::EventInfo* eventInfo = nullptr;
223  ATH_CHECK( evtStore()->retrieve( eventInfo, "EventInfo" ) );
224 
225  // Only run this function on Monte Carlo
226  const bool isSim = eventInfo->eventType(xAOD::EventInfo::EventType::IS_SIMULATION);
227  if ( !isSim ) {
228  ATH_MSG_DEBUG (__PRETTY_FUNCTION__ << "It is a data event... nothing to be done...");
229  return StatusCode::SUCCESS;
230  }
231 
232 
233  //Derivations changes runNumber due to pileup reweight,
234  //while mcChannelNumber is 0 in Truth derivations
235  m_sampleRunNumber = eventInfo->mcChannelNumber() ;
236  if (m_sampleRunNumber == 0 ) {
237  ATH_MSG_WARNING(__PRETTY_FUNCTION__ << "mcChannelNumber is 0, falling back to runNumber" ) ;
238  m_sampleRunNumber = eventInfo->runNumber() ;
239  }
240 
242 
243  //Some bookkeeping
246 
248  ATH_MSG_WARNING(__PRETTY_FUNCTION__ <<
249  ": This MC (Run " << m_sampleRunNumber <<
250  " ) is not an mc15 Alpgen+Pythia6 sample - this tool is useless ") ;
251  }
252  else {
253  ATH_MSG_INFO(__PRETTY_FUNCTION__ <<
254  ": Good ! This is a valid Alpgen+Pythia6 sample ( " << m_sampleName << " )" ) ;
255  ATH_MSG_INFO(__PRETTY_FUNCTION__ << ": MC Channel Number = " << eventInfo->mcChannelNumber()
256  << " , MC Run Number = " << eventInfo->runNumber() );
257  }
258 
259  //This routine never fails (unless it cannot get the container)
260  //It may not identify the sample, in this case it will do nothing
261  return StatusCode::SUCCESS;
262 }
263 
264 //==============================================================================
265 
266 //==============================================================================
267 // Getter to access the sample type
268 //==============================================================================
270  if (! m_isConfigured) {
272  m_isConfigured = true;
273  }
274  //Return an enum object with the type of the sample
275  return m_sampleType ;
276 }
277 //==============================================================================
278 
279 //==============================================================================
280 // Getter to access the sample name
281 //==============================================================================
283  if (! m_isConfigured) {
284  ATH_CHECK( setSampleType(), "unknown" );
285  m_isConfigured = true;
286  }
287  //Return a string with the type of the sample (bb, cc, c, light or unknown)
288  return m_sampleName ;
289 }
290 //==============================================================================
291 
292 //==============================================================================
293 // Return a string with the LAST type of classification of this event (bb, cc,
294 // c, light, kill or unknown)
295 //==============================================================================
297  //TODO: Provide a method to return a string (like "isBB" etc.)
298  return m_decisionType ;
299 }
300 //==============================================================================
301 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
HFORSelectionTool::m_sampleType
HFORType m_sampleType
Definition: HFORSelectionTool.h:81
HFOR_Truth
Definition: HFOR_Truth.h:30
HFORSelectionTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: HFORSelectionTool.cxx:57
HFORSelectionTool::m_HFORStrategy
std::string m_HFORStrategy
Definition: HFORSelectionTool.h:92
HFOR_Truth.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HFORSelectionTool::m_sampleName
std::string m_sampleName
Definition: HFORSelectionTool.h:84
HFORType
HFORType
Definition: HFORTypes.h:9
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
HFORSelectionTool::getSampleName
std::string getSampleName() override
Definition: HFORSelectionTool.cxx:282
HFORType::noType
@ noType
asg
Definition: DataHandleTestTool.h:28
HFOR_Truth::setSampleType
void setSampleType(unsigned int runNumber)
Definition: HFOR_Truth.cxx:687
HFORSelectionTool::m_runConfigFile
std::string m_runConfigFile
Definition: HFORSelectionTool.h:91
HFORSelectionTool::m_evtCounterAll
unsigned long int m_evtCounterAll
Definition: HFORSelectionTool.h:86
HFORSelectionTool::m_isConfigured
bool m_isConfigured
Definition: HFORSelectionTool.h:93
HFOR_Truth::readRunConfig
int readRunConfig(std::string runConfigFile)
Definition: HFOR_Truth.cxx:658
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
HFORSelectionTool::m_hforTruth
HFOR_Truth m_hforTruth
Definition: HFORSelectionTool.h:89
HFORSelectionTool::m_decisionType
HFORType m_decisionType
Definition: HFORSelectionTool.h:82
xAOD::EventInfo_v1::mcChannelNumber
uint32_t mcChannelNumber() const
The MC generator's channel number.
HFORSelectionTool::HFORSelectionTool
HFORSelectionTool(const std::string &name)
Definition: HFORSelectionTool.cxx:36
HFORSelectionTool::getDecisionType
HFORType getDecisionType() override
Definition: HFORSelectionTool.cxx:296
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
HFOR_Truth::getSampleName
std::string getSampleName()
Definition: HFOR_Truth.cxx:720
HFORSelectionTool::m_sampleRunNumber
unsigned long int m_sampleRunNumber
Definition: HFORSelectionTool.h:83
HFORSelectionTool::beginInputFile
virtual StatusCode beginInputFile() override
Function called when a new input file is opened.
Definition: HFORSelectionTool.cxx:97
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
HFORSelectionTool::isSelected
virtual bool isSelected() override
Definition: HFORSelectionTool.cxx:144
HFORSelectionTool::setSampleType
StatusCode setSampleType()
Definition: HFORSelectionTool.cxx:215
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
PathResolver.h
HFOR_Truth::getSampleType
HFORType getSampleType()
Definition: HFOR_Truth.cxx:712
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
HFOR_Truth::findOverlap
HFORType findOverlap(const xAOD::TruthEventContainer &truthEvent, const xAOD::JetContainer *jets=nullptr)
Definition: HFOR_Truth.cxx:106
HFORType::kill
@ kill
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HFORSelectionTool::m_truthJetCollectionName
std::string m_truthJetCollectionName
Definition: HFORSelectionTool.h:80
HFORSelectionTool::getSampleType
HFORType getSampleType() override
Definition: HFORSelectionTool.cxx:269
HFORSelectionTool::m_evtCounterKilled
unsigned long int m_evtCounterKilled
Definition: HFORSelectionTool.h:87
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
HFORSelectionTool::m_matchCone
double m_matchCone
Definition: HFORSelectionTool.h:79
HFORSelectionTool.h
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
HFORSelectionTool::endInputFile
virtual StatusCode endInputFile() override
Function called when the currently open input file got completely processed.
Definition: HFORSelectionTool.cxx:112
HFOR_Truth::setAngularBasedHFOR
void setAngularBasedHFOR()
Definition: HFOR_Truth.h:57
xAOD::EventInfo_v1::eventType
bool eventType(EventType type) const
Check for one particular bitmask value.
HFOR_Truth::setJetBasedHFOR
void setJetBasedHFOR()
Definition: HFOR_Truth.h:54