ATLAS Offline Software
AnalysisConfig_Ntuple.cxx
Go to the documentation of this file.
1 
10 #include <cstdio>
11 
12 #include <sys/time.h>
13 
15 
17 #include "AtlasHepMC/GenEvent.h"
18 #include "AtlasHepMC/GenVertex.h"
19 #include "AtlasHepMC/GenParticle.h"
20 
21 
22 #include "EventInfo/EventInfo.h"
23 #include "EventInfo/EventID.h"
25 
26 
29 
31 
35 
37 // #include "TrigInDetAnalysisUtils/OfflineObjectSelection.h"
38 
41 #include "TrkTrack/Track.h"
42 
43 
44 #include "VxVertex/VxContainer.h"
45 
47 
49 
51 
52 //#include "JetEvent/JetCollection.h"
53 
56 
58 
61 
62 
63 #define endmsg endmsg
64 
65 
66 
67 std::string date() {
68  time_t t;
69  time(&t);
70  char buf[26];
71  std::string mtime = ctime_r(&t, buf);
72  mtime.erase( std::remove(mtime.begin(), mtime.end(), '\n'), mtime.end() );
73  return mtime;
74 }
75 
76 
77 //function to find true taus
78 HepMC::ConstGenParticlePtr fromParent( int pdg_id, HepMC::ConstGenParticlePtr p, bool printout=false ) {
79 
80  if ( p==0 ) return 0;
81  if (std::abs(p->pdg_id())==11 || std::abs(p->pdg_id())==13 ) return 0; //don't want light leptons from tau decays
82  if ( std::abs(p->pdg_id())==pdg_id ) return p;
83 
84  auto vertex = p->production_vertex();
85  if ( !vertex) return 0; // has no production vertex !!!
86 
87 #ifdef HEPMC3
88  if ( vertex->particles_in().size() < 1 ) return 0;
89 
91  // if ( printout ) {
92  // TruthParticle t(p);
93  // std::cout << "particle " << *p << " " << t.pdgId() << "\tparent " << p << std::endl;
94  // }
95 
96  for ( auto in: vertex->particles_in()) {
97  auto parent = fromParent( pdg_id, in, printout );
98  TruthParticle t(in);
99  if ( parent && std::abs(parent->pdg_id())==pdg_id) {
100  return parent;
101  }
102  }
103 #else
104  if ( vertex->particles_in_size() < 1 ) return 0;
105 
106  HepMC::GenVertex::particles_in_const_iterator in = vertex->particles_in_const_begin();
107  HepMC::GenVertex::particles_in_const_iterator end = vertex->particles_in_const_end();
108  while ( in!=end ) {
109  const HepMC::GenParticle* parent = fromParent( pdg_id, *in, printout );
110  TruthParticle t(*in);
111  // if ( printout ) std::cout << "\tvalue for particle " << *in << " " << t.pdgId() << "\tparent " << parent << std::endl;
112  if ( parent && std::abs(parent->pdg_id())==pdg_id) {
113  //if ( printout ) std::cout << "found tau! - in parents" << std::endl;
114  return parent;
115  }
116  in++;
117  }
118 #endif
119 
120  return 0;
121 }
122 
123 
124 
125 template<class T>
126 void remove_duplicates(std::vector<T>& vec) {
127  std::sort(vec.begin(), vec.end());
128  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
129 }
130 
131 
133 
135  std::vector<TrackTrigObject>& objects, const std::string& key ) {
136 
137  objects.clear();
138 
139  const std::vector< Trig::Feature<xAOD::JetContainer> > jetfeatures = citr->get<xAOD::JetContainer>( key, TrigDefs::alsoDeactivateTEs );
140 
141  if ( jetfeatures.empty() ) return 0;
142 
143  for ( size_t ifeature=0 ; ifeature<jetfeatures.size() ; ifeature++ ) {
144  Trig::Feature<xAOD::JetContainer> jetfeature = jetfeatures.at(ifeature);
145 
146  if ( jetfeature.empty() ) continue;
147 
148  const xAOD::JetContainer* jets = jetfeature.cptr();
149 
150  if ( jets == 0 ) continue;
151 
153 
154  for ( int j=0 ; jitr!=jets->end() ; ++jitr, j++ ) {
155 
156  const xAOD::Jet* ajet = (*jitr);
157 
158  long unsigned jetid = (unsigned long)ajet;
159 
160  TrackTrigObject jet = TrackTrigObject( ajet->eta(), ajet->phi(), ajet->pt(), 0, ajet->type(), jetid );
161 
162  objects.push_back( jet );
163 
164  }
165  }
166 
167  return objects.size();
168 
169 }
170 
171 
172 
173 
175 
176  m_provider->msg(MSG::INFO) << "" << "AnalysisConfig_Ntuple::loop() for " << m_analysisInstanceName
177  << " compiled " << __DATE__ << " " << __TIME__ << "\t: " << date() << "" << endmsg;
178 
179  m_provider->msg(MSG::ERROR) << "" << "This should no longer be called" << m_analysisInstanceName << endmsg;
180 
181 }
182 
183 
184 
185 
187 
189 
190  m_provider->msg(MSG::INFO) << "AnalysisConfig_Ntuple::book() name " << name() << endmsg;
191 
195  if ( !m_finalised ) {
196  m_provider->msg(MSG::INFO) << "AnalysisConfig_Ntuple::book() not booking " << name() << endmsg;
197  return;
198  }
199 
200 
201 
202  // get the TriggerDecisionTool
203 
204  if( m_tdt->retrieve().isFailure() ) {
205  m_provider->msg(MSG::FATAL) << " Unable to retrieve the TrigDecisionTool: Please check job options file" << endmsg;
206  // return StatusCode::FAILURE;
207  return;
208  }
209 
210  m_provider->msg(MSG::INFO) << "" << " Successfully retrived the TrigDecisionTool" << "" << endmsg;
211  m_provider->msg(MSG::INFO) << "" << " booking ntuple" << "" << endmsg;
212  m_provider->msg(MSG::INFO) << "" << " trying to create new ntple file" << "" << endmsg;
213 
215  TDirectory* dir = gDirectory;
216 
217  static std::atomic<bool> first_open = true;
218 
219  std::string outputFileName = m_outputFileName;
220 
221  if ( genericFlag() ) {
222  static std::atomic<int> file_index = 0;
223  std::string::size_type pos = outputFileName.find(".root");
224  if ( pos != std::string::npos ) outputFileName.erase(pos, outputFileName.size());
225  char file_label[64];
226  sprintf( file_label, "-%04d.root", file_index++ );
227  outputFileName += file_label;
228  }
229 
230  m_provider->msg(MSG::INFO) << "book() Writing to file " << outputFileName << endmsg;
231 
232  if ( first_open || genericFlag() ) {
234  m_File = new TFile( outputFileName.c_str(), "recreate");
235 
236  TTree* dataTree = new TTree("dataTree", "dataTree");
237  TString releaseData(m_releaseData.c_str());
238  dataTree->Branch( "ReleaseMetaData", "TString", &releaseData);
239  dataTree->Fill();
240  dataTree->Write("", TObject::kOverwrite);
241  delete dataTree;
242 
243 
244  m_Tree = new TTree("tree", "tree");
245  m_Tree->Branch( "TIDA::Event", "TIDA::Event", m_event, 6400, 1 );
246 
247 
248  }
249  else {
251  m_File = new TFile( outputFileName.c_str(), "update");
252  m_Tree = (TTree *)m_File->Get("tree");
253  m_Tree->SetBranchAddress( "TIDA::Event", &m_event );
254  }
255 
256  m_Dir = gDirectory;
257 
258  first_open = false;
259 
260 
261  m_provider->msg(MSG::DEBUG) << "change directory " << name() << " " << dir->GetName() << endmsg;
262 
263  // std::cout << "change directory " << name() << " " << dir->GetName() << std::endl;
265  dir->cd();
266 
267  // gDirectory->pwd();
268 
269  m_finalised = false; // flag we have an open file that is not yet finalised
270 
271  m_provider->msg(MSG::INFO) << "AnalysisConfig_Ntuple::book() exiting" << endmsg;
272 
273 }
274 
275 
276 
278 
280 
281  // gDirectory->pwd();
282 
283 
285  if ( m_finalised ) {
286  m_provider->msg(MSG::INFO) << "AnalysisConfig_Ntuple::finalise() flagged, not finalising " << m_provider->name() << "\t" << m_Tree->GetEntries() << " entries" << endmsg;
287  return;
288  }
289 
290  m_provider->msg(MSG::INFO) << "AnalysisConfig_Ntuple::finalise() writing " << m_provider->name() << "\t" << m_Tree->GetEntries() << " entries" << endmsg;
291 
292  TDirectory* directory = gDirectory;
293 
294  // std::cout << "change directory " << name() << " " << m_Dir->GetName() << std::endl;
295 
296  m_provider->msg(MSG::DEBUG) << "change directory " << name() << " " << m_Dir->GetName() << endmsg;
297 
298 
299  m_Dir->cd();
300 
301  // gDirectory->pwd();
302 
303  if ( m_Tree ) m_Tree->Write("", TObject::kOverwrite);
304 
305  // m_File->Write();
306  if ( m_File ) m_File->Close();
307 
308 
309  m_finalised = true;
310 
311  // m_Tree "belongs" to the m_File so was (possibly) deleted on the m_File->Close();
312  // so don't delete it !
313  // delete m_Tree;
314  delete m_File;
315 
316  m_Tree = 0;
317  m_File = 0;
318 
319  // f.Write();
320  // f.Close();
321 
322  // std::cout << "change directory " << name() << " " << directory->GetName() << std::endl;
323 
324  directory->cd();
325 
326  // gDirectory->pwd();
327 
328 }
329 
330 
331 
AnalysisConfig_Ntuple::get_jets
size_t get_jets(Trig::FeatureContainer::combination_const_iterator citr, std::vector< TrackTrigObject > &objects, const std::string &key="")
retrieve the jets from the Roi
Definition: AnalysisConfig_Ntuple.cxx:134
Trig::FeatureContainer::combination_const_iterator
std::vector< Combination >::const_iterator combination_const_iterator
Definition: FeatureContainer.h:69
TIDARoiDescriptorBuilder.h
Trig::Feature
Definition: Feature.h:112
Filter_RoiSelector.h
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
GenEvent.h
ElectronContainer.h
fromParent
HepMC::ConstGenParticlePtr fromParent(int pdg_id, HepMC::ConstGenParticlePtr p, bool printout=false)
Definition: AnalysisConfig_Ntuple.cxx:78
TrackParameters.h
LArConditions2Ntuple.objects
objects
Definition: LArConditions2Ntuple.py:58
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
T_AnalysisConfig< AthReentrantAlgorithm >::m_provider
AthReentrantAlgorithm * m_provider
Definition: T_AnalysisConfig.h:955
T_AnalysisConfig< AthReentrantAlgorithm >::m_tdt
ToolHandle< Trig::TrigDecisionTool > * m_tdt
Definition: T_AnalysisConfig.h:957
TruthParticleContainer.h
GenVertex.h
MuonContainer.h
Filters.h
TruthParticle
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
xAOD::Jet_v1::phi
virtual double phi() const
The azimuthal angle ( ) of the particle.
Definition: Jet_v1.cxx:54
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
ExpertMethods.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
remove_duplicates
void remove_duplicates(std::vector< T > &vec)
Definition: AnalysisConfig_Ntuple.cxx:126
Filter_AcceptAll.h
HLTResult.h
GenParticle.h
Trig::Feature::empty
bool empty() const
test method to check if the object is truly there
Definition: Feature.h:197
AnalysisConfig_Ntuple.h
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
T_AnalysisConfig< AthReentrantAlgorithm >::m_analysisInstanceName
std::string m_analysisInstanceName
Definition: T_AnalysisConfig.h:963
Track.h
T_AnalysisConfig< AthReentrantAlgorithm >::genericFlag
bool genericFlag() const
Definition: T_AnalysisConfig.h:268
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
TIDDirectory.h
class to keep a directory for each object in a root sort of way, but needed to keep the root objects ...
EventID.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
xAOD::Jet_v1::type
virtual Type::ObjectType type() const
The type of the object as a simple enumeration.
Definition: Jet_v1.cxx:85
AnalysisConfig_Ntuple::m_Dir
TDirectory * m_Dir
Definition: AnalysisConfig_Ntuple.h:160
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
T_AnalysisConfig< AthReentrantAlgorithm >::releaseData
std::string releaseData() const
Definition: T_AnalysisConfig.h:271
AnalysisConfig_Ntuple::finalize
virtual void finalize()
finalise the analysis - take ratios for efficiencies etc
Definition: AnalysisConfig_Ntuple.cxx:279
McEventCollection.h
DeMoScan.directory
string directory
Definition: DeMoScan.py:80
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
AnalysisConfig_Ntuple::m_Tree
TTree * m_Tree
Definition: AnalysisConfig_Ntuple.h:159
AnalysisConfig_Ntuple::loop
virtual void loop()
Definition: AnalysisConfig_Ntuple.cxx:174
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
TrackCollection.h
test_pyathena.parent
parent
Definition: test_pyathena.py:15
Filter_etaPT.h
TauJetContainer.h
taskman.mtime
mtime
Definition: taskman.py:313
VxContainer.h
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::Jet_v1::eta
virtual double eta() const
The pseudorapidity ( ) of the particle.
Definition: Jet_v1.cxx:49
beamspotman.dir
string dir
Definition: beamspotman.py:623
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
AnalysisConfig_Ntuple::m_finalised
bool m_finalised
Definition: AnalysisConfig_Ntuple.h:189
AnalysisConfig_Ntuple::m_File
TFile * m_File
Definition: AnalysisConfig_Ntuple.h:158
EventInfo.h
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
TrackParticle.h
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
AnalysisConfig_Ntuple::book
virtual void book()
setup the analysis the analysis, retrieve the tools etc
Definition: AnalysisConfig_Ntuple.cxx:188
TrackTrigObject
Definition: TrackTrigObject.h:21
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
DEBUG
#define DEBUG
Definition: page_access.h:11
AthCommonMsg::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
date
std::string date()
sadly, includes a return at the end
Definition: AnalysisConfig_Ntuple.cxx:67
AnalysisConfig_Ntuple::m_event
TIDA::Event * m_event
Definition: AnalysisConfig_Ntuple.h:156
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
T_AnalysisConfig< AthReentrantAlgorithm >::name
const std::string name() const
Definition: T_AnalysisConfig.h:233
xAOD::Jet_v1::pt
virtual double pt() const
The transverse momentum ( ) of the particle.
Definition: Jet_v1.cxx:44
AthenaPoolExample_Copy.outputFileName
string outputFileName
Definition: AthenaPoolExample_Copy.py:40
TrigRoiDescriptorCollection.h
TrackParticleContainer.h
AnalysisConfig_Ntuple::m_outputFileName
std::string m_outputFileName
Definition: AnalysisConfig_Ntuple.h:185
Trig::Feature::cptr
const T * cptr() const
explicit conversion to object
Definition: Feature.h:171
GenParticle
@ GenParticle
Definition: TruthClasses.h:30
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
T_AnalysisConfig< AthReentrantAlgorithm >::m_releaseData
std::string m_releaseData
Definition: T_AnalysisConfig.h:991