ATLAS Offline Software
DumpMC.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TruthIO/DumpMC.h"
6 #include "GaudiKernel/PhysicalConstants.h"
7 #include <CLHEP/Vector/LorentzVector.h>
8 using namespace Gaudi::Units;
9 
10 
11 DumpMC::DumpMC(const std::string& name, ISvcLocator* pSvcLocator)
12  : GenBase(name, pSvcLocator)
13 {
14  declareProperty("McEventOutputKey", m_keyout="GEN_EVENT");
15  declareProperty("VerboseOutput", m_VerboseOutput=true);
16  declareProperty("DeepCopy", m_DeepCopy=false);
17  declareProperty("EtaPhi", m_EtaPhi=false);
18  declareProperty("PrintQuasiStableParticles", m_PrintQuasiStableParticles=false);
19 }
20 
21 
24  if (m_mcEventKey == m_keyout && m_DeepCopy) {
25  ATH_MSG_FATAL("Input and output MC event keys cannot be the same (" << m_mcEventKey << ")");
26  return StatusCode::FAILURE;
27  }
28  return StatusCode::SUCCESS;
29 }
30 
31 
33  if (m_DeepCopy) {
34  McEventCollection* mcCollptra = new McEventCollection();
35  // Fill the new McEventCollection with a copy of the initial HepMC::GenEvent
36  for(const HepMC::GenEvent* evt : *events_const()) {
37  mcCollptra->push_back(new HepMC::GenEvent(*evt));
38  }
39  // Loop over all events in McEventCollection
40  for (McEventCollection::iterator evt = mcCollptra->begin(); evt != mcCollptra->end(); ++evt) {
41 #ifdef HEPMC3
42  std::vector<HepMC::GenVertexPtr> lambda_vertices_to_remove;
43  for (auto part: **evt){
44  const int abspid = std::abs(part->pdg_id());
45  if (abspid != 310 && abspid != 3122 && abspid != 3222 && abspid != 3112 && abspid != 3322 && abspid != 3312 && abspid != 3334 ) continue;
46  if (!part->end_vertex()) continue;
47  lambda_vertices_to_remove.push_back(part->end_vertex());
48  part->set_status(1);
49  }
50  for (auto v: lambda_vertices_to_remove) (*evt)->remove_vertex(v);
51 #else
52  // Loop over the vertices of the event
53  std::set<int> Lambdas;
54  std::set<int> vtx_to_delete;
55  for (HepMC::GenEvent::vertex_const_iterator vtx = (*evt)->vertices_begin(); vtx != (*evt)->vertices_end(); ++vtx) {
56  // Loop over the particles that produced the vertex
57  if ( vtx_to_delete.find((*vtx)->barcode()) == vtx_to_delete.end() &&
58  (*vtx)->particles_in_const_begin() != (*vtx)->particles_in_const_end() ) {
59  HepMC::GenVertex::particles_in_const_iterator part = (*vtx)->particles_in_const_begin();
60  bool lambda_not_found = true;
61  const int abspid = std::abs((*part)->pdg_id());
62  do {
63  if (abspid == 310 || abspid == 3122 || abspid == 3222 || abspid == 3112 ||
64  abspid == 3322 || abspid == 3312 || abspid == 3334 ) {
65  lambda_not_found = false;
66  Lambdas.insert((*part)->barcode());
67  vtx_to_delete.insert((*vtx)->barcode());
68  // In case a lambda was found, store in vertices to be deleted all the vertices
69  // that the products of Lambda created
70  for (HepMC::GenVertex::vertex_iterator desc = (*vtx)->vertices_begin(HepMC::descendants);
71  desc != (*vtx)->vertices_end(HepMC::descendants); ++desc) {
72  vtx_to_delete.insert((*desc)->barcode());
73  }
74  }
75  ++part;
76  } while (part != (*vtx)->particles_in_const_end() && lambda_not_found);
77  }
78  }
79 
80  // Set Lambda's status to stable
81  for (std::set<int>::iterator l = Lambdas.begin(); l != Lambdas.end(); ++l) {
82  HepMC::GenParticle* lam = (*evt)->barcode_to_particle(*l);
83  lam->set_status(1);
84  }
85  // Delete all Lambda vertices from the event
86  for (std::set<int>::iterator v = vtx_to_delete.begin(); v != vtx_to_delete.end(); ++v) {
87  HepMC::GenVertex* vdel = (*evt)->barcode_to_vertex(*v);
88  (*evt)->remove_vertex(vdel);
89  delete vdel;
90  }
91 #endif
92  }
93 
94  if (evtStore()->record(mcCollptra, m_keyout).isFailure()){
95  ATH_MSG_ERROR("Could not record McEventCollection");
96  return StatusCode::FAILURE;
97  }
98  }
99 
100  // Loop over all events in McEventCollection
101  for(const HepMC::GenEvent* evt : *events_const()) {
102  auto pdfinfo = evt->pdf_info();
103  auto ion = evt->heavy_ion();
104 #ifdef HEPMC3
105  if (pdfinfo) {
106  std::cout << "PdfInfo: "
107  << pdfinfo->parton_id[0] << ", "
108  << pdfinfo->parton_id[1] << ", "
109  << pdfinfo->x[0] << ", "
110  << pdfinfo->x[1] << ", "
111  << pdfinfo->scale << ", "
112  << pdfinfo->xf[0] << ", "
113  << pdfinfo->xf[1] << ", "
114  << pdfinfo->pdf_id[0] << ", "
115  << pdfinfo->pdf_id[1]
116  << std::endl;
117  }
118 
119  if (ion) {
120  std::cout << std::endl;
121  std::cout << "Heavy Ion: "
122  << ion->Ncoll_hard <<", "
123  << ion->Npart_proj <<" , "
124  << ion->Npart_targ<< ", "
125  << ion->Ncoll<< ", "
126  << ion->spectator_neutrons << ", "
127  << ion->spectator_protons << ", "
128  << ion->N_Nwounded_collisions << ", "
129  << ion->Nwounded_N_collisions << ", "
130  << ion->Nwounded_Nwounded_collisions << ", "
131  << ion->impact_parameter << ", "
132  << ion->event_plane_angle << ", "
133  << ion->eccentricity << ", "
134  << ion->sigma_inel_NN
135  << std::endl;
136  }
137 #else
138  if (pdfinfo) {
139  std::cout << "PdfInfo: "
140  << pdfinfo->id1() << ", "
141  << pdfinfo->id2() << ", "
142  << pdfinfo->x1() << ", "
143  << pdfinfo->x2() << ", "
144  << pdfinfo->scalePDF() << ", "
145  << pdfinfo->pdf1() << ", "
146  << pdfinfo->pdf2() << ", "
147  << pdfinfo->pdf_id1() << ", "
148  << pdfinfo->pdf_id2()
149  << std::endl;
150  }
151 
152  if (ion) {
153  std::cout << std::endl;
154  std::cout << "Heavy Ion: "
155  << ion->Ncoll_hard() <<", "
156  << ion->Npart_proj() <<" , "
157  << ion->Npart_targ()<< ", "
158  << ion->Ncoll()<< ", "
159  << ion->spectator_neutrons() << ", "
160  << ion->spectator_protons() << ", "
161  << ion->N_Nwounded_collisions() << ", "
162  << ion->Nwounded_N_collisions() << ", "
163  << ion->Nwounded_Nwounded_collisions() << ", "
164  << ion->impact_parameter() << ", "
165  << ion->event_plane_angle() << ", "
166  << ion->eccentricity() << ", "
167  << ion->sigma_inel_NN()
168  << std::endl;
169  }
170 #endif
171  if (m_VerboseOutput) {
172  if (!m_EtaPhi) {
173  HepMC::Print::line(std::cout,*evt); // standard HepMc dump
174  } else { // sort particles by rapidity and then dump
175  // Loop over all particles in the event and build up the grid
176  for (auto part: *evt) {
177  double rapid =part->momentum().pseudoRapidity();
178  double phi = part->momentum().phi(); //phi is in range -pi to pi
179  double et=part->momentum().perp();
180  int p_stat=part->status();
181  int p_id = part->pdg_id();
182  std::cout << " eta = " << rapid<< " Phi = " << phi << " Et = " <<et/GeV << " Status= " << p_stat << " PDG ID= "<< p_id << std::endl;
183  }
184  }
185  if(m_PrintQuasiStableParticles) {
186  for (const auto& pitr: *evt) {
187  int p_stat=pitr->status();
188  if(p_stat==2 && pitr->production_vertex() && pitr->end_vertex()) {
189  const auto& prodVtx = pitr->production_vertex()->position();
190  const auto& endVtx = pitr->end_vertex()->position();
191  const CLHEP::HepLorentzVector lv0( prodVtx.x(), prodVtx.y(), prodVtx.z(), prodVtx.t() );
192  const CLHEP::HepLorentzVector lv1( endVtx.x(), endVtx.y(), endVtx.z(), endVtx.t() );
193 
194  CLHEP::HepLorentzVector dist4D(lv1);
195  dist4D-=lv0;
196  CLHEP::Hep3Vector dist3D=dist4D.vect();
197  if(dist3D.mag()>1*Gaudi::Units::mm) {
198  const auto& GenMom = pitr->momentum();
199  const CLHEP::HepLorentzVector mom( GenMom.x(), GenMom.y(), GenMom.z(), GenMom.t() );
200  ATH_MSG_INFO("Quasi stable particle "<<pitr);
201  ATH_MSG_INFO(" Prod V:"<<pitr->production_vertex ());
202  ATH_MSG_INFO(" Decay V:"<<pitr->end_vertex ());
203  ATH_MSG_INFO(" gamma(Momentum)="<<mom.gamma()<<" gamma(Vertices)="<<dist4D.gamma());
204  }
205  }
206  }
207  }
208  }
209  }
210  return StatusCode::SUCCESS;
211 }
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
et
Extra patterns decribing particle interation process.
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
DumpMC::DumpMC
DumpMC(const std::string &name, ISvcLocator *pSvcLocator)
Definition: DumpMC.cxx:11
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
HepMC::Print::line
void line(std::ostream &os, const GenEvent &e)
Definition: GenEvent.h:676
DumpMC::m_keyout
std::string m_keyout
Definition: DumpMC.h:19
DumpMC::m_DeepCopy
bool m_DeepCopy
Definition: DumpMC.h:21
DumpMC.h
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
CaloCondBlobAlgs_fillNoiseFromASCII.desc
desc
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:54
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
GenBase
Base class for common behaviour of MC truth algorithms.
Definition: GenBase.h:47
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
DumpMC::execute
StatusCode execute()
Definition: DumpMC.cxx:32
DumpMC::m_PrintQuasiStableParticles
bool m_PrintQuasiStableParticles
Definition: DumpMC.h:23
DumpMC::initialize
StatusCode initialize()
Definition: DumpMC.cxx:22
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
python.PyAthena.v
v
Definition: PyAthena.py:154
DumpMC::m_VerboseOutput
bool m_VerboseOutput
Definition: DumpMC.h:20
DumpMC::m_EtaPhi
bool m_EtaPhi
Definition: DumpMC.h:22
GenBase::initialize
virtual StatusCode initialize() override
Definition: GenBase.cxx:17
GenParticle
@ GenParticle
Definition: TruthClasses.h:30