ATLAS Offline Software
SG_StepNtuple.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 #include "SG_StepNtuple.h"
6 
7 #include "GaudiKernel/Bootstrap.h"
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/IMessageSvc.h"
10 #include "GaudiKernel/IDataProviderSvc.h"
11 #include "GaudiKernel/INTupleSvc.h"
12 #include "GaudiKernel/NTuple.h"
13 #include "GaudiKernel/SmartDataPtr.h"
14 
16 
17 #include "G4Step.hh"
18 #include "G4Event.hh"
19 
20 #include <iostream>
21 
22 
23 namespace G4UA
24 {
25 
26  SG_StepNtuple::SG_StepNtuple(const std::vector<int>& pdgids)
27  : AthMessaging(Gaudi::svcLocator()->service< IMessageSvc >( "MessageSvc" ),
28  "SG_StepNtuple")
29  {
30  // Load up the PDG IDs
31  for (int i : pdgids ){
32  m_rhs.insert(i);
33  }
34  }
35 
37  {
38  NTupleFilePtr file1(ntupleSvc(), "/NTUPLES/FILE1");
39 
40  SmartDataPtr<NTuple::Directory>
41  ntdir(ntupleSvc(),"/NTUPLES/FILE1/StepNtuple");
42 
43  if ( !ntdir ) ntdir = ntupleSvc()->createDirectory(file1,"StepNtuple");
44  //if ( !ntdir ) log << MSG::ERROR << " failed to get ntuple directory" << endmsg;
45  NTuplePtr nt(ntupleSvc(), "/NTUPLES/FILE1/StepNtuple/10");
46  if ( !nt ) { // Check if already booked
47  nt = ntupleSvc()->book (ntdir.ptr(), 10,CLID_ColumnWiseTuple, "GEANT4 Step NTuple");
48  if ( nt ) {
49 
50  ATH_MSG_INFO("booked step ntuple ");
51 
52  if( nt->addItem ("nstep", m_nsteps,0 ,50000)!=StatusCode::SUCCESS // WARNING!! Force limit to 50k tracks
53  || nt->addItem ("pdg", m_nsteps, m_pdg)!=StatusCode::SUCCESS
54  || nt->addItem ("charge", m_nsteps, m_charge)!=StatusCode::SUCCESS
55  || nt->addItem ("mass", m_nsteps, m_mass)!=StatusCode::SUCCESS
56  || nt->addItem ("baryon", m_nsteps, m_baryon)!=StatusCode::SUCCESS
57  || nt->addItem ("x1", m_nsteps, m_x1)!=StatusCode::SUCCESS
58  || nt->addItem ("y1", m_nsteps, m_y1)!=StatusCode::SUCCESS
59  || nt->addItem ("z1", m_nsteps, m_z1)!=StatusCode::SUCCESS
60  || nt->addItem ("t1", m_nsteps, m_t1)!=StatusCode::SUCCESS
61  || nt->addItem ("x2", m_nsteps, m_x2)!=StatusCode::SUCCESS
62  || nt->addItem ("y2", m_nsteps, m_y2)!=StatusCode::SUCCESS
63  || nt->addItem ("z2", m_nsteps, m_z2)!=StatusCode::SUCCESS
64  || nt->addItem ("t2", m_nsteps, m_t2)!=StatusCode::SUCCESS
65  || nt->addItem ("dep", m_nsteps, m_dep)!=StatusCode::SUCCESS
66  || nt->addItem ("ke1", m_nsteps, m_ke1)!=StatusCode::SUCCESS
67  || nt->addItem ("ke2", m_nsteps, m_ke2)!=StatusCode::SUCCESS
68  || nt->addItem ("rh", m_nsteps, m_rh)!=StatusCode::SUCCESS
69  || nt->addItem ("rhid", m_nsteps, m_rhid)!=StatusCode::SUCCESS
70  || nt->addItem ("step", m_nsteps, m_step)!=StatusCode::SUCCESS
71  || nt->addItem ("pt1", m_nsteps, m_pt1)!=StatusCode::SUCCESS
72  || nt->addItem ("pt2", m_nsteps, m_pt2)!=StatusCode::SUCCESS
73  || nt->addItem ("minA",m_nsteps, m_minA)!=StatusCode::SUCCESS
74  || nt->addItem ("v2",m_nsteps, m_v2)!=StatusCode::SUCCESS
75  || nt->addItem ("vthresh",m_nsteps, m_vthresh)!=StatusCode::SUCCESS
76  || nt->addItem ("vbelowthresh",m_nsteps, m_vbelowthresh)!=StatusCode::SUCCESS
77  || nt->addItem ("evtid", m_evtid)!=StatusCode::SUCCESS)
78 
79  ATH_MSG_ERROR("Could not configure branches ");
80 
81  }
82 
83  else ATH_MSG_ERROR("Could not book step ntuple!! ");
84  }
85 
86  //set initial values
87  m_nevents=0;
88 
89  }
90 
91  void SG_StepNtuple::BeginOfEventAction(const G4Event*)
92  {
93  m_nsteps=0;
94  m_rhadronIndex=0;//the rhadron index (either the first or second rhadon, usually)
95  m_nevents++;
96  m_evtid=m_nevents;//since it gets cleared out after every fill...
97  }
98 
99  void SG_StepNtuple::EndOfEventAction(const G4Event*)
100  {
101  if(! ntupleSvc()->writeRecord("/NTUPLES/FILE1/StepNtuple/10").isSuccess()) {
102  ATH_MSG_ERROR( " failed to write record for this event" );
103  }
104 
105  //this also seems to zero out all the arrays... so beware!
106  }
107 
108  void SG_StepNtuple::UserSteppingAction(const G4Step* aStep)
109  {
110  if(m_nsteps<50000){
111  int pdg = aStep->GetTrack()->GetDefinition()->GetPDGEncoding();
112  bool rhad=false;
113  if (std::find(m_rhs.begin(),m_rhs.end(),std::abs(pdg))!=m_rhs.end()) {
114  rhad=true;
115  }
116 
117  //
118  if (!rhad && isSUSYParticle(std::abs(pdg))){
119  std::cout<<"ACH139: SG_StepNtuple: other code thinks "<<pdg<<" is an Rhadron!"<<std::endl;
120  rhad=true;
121  }
122  //
123 
124  if (rhad){
125 
126  //
127  G4Material * mat = aStep->GetTrack()->GetMaterial();
128  double minA=1500000.;
129  for (unsigned int i=0;i<mat->GetNumberOfElements();++i){
130  if (mat->GetElement(i) && minA>mat->GetElement(i)->GetN()){
131  minA=mat->GetElement(i)->GetN();
132  }
133  }
134  //
135 
136  bool firstslow = aStep->GetPostStepPoint()->GetVelocity()<0.15*std::pow(minA,-2./3.)*CLHEP::c_light;
137  //just save the first slow step for the rhadron
138  for (int i=0; i<m_nsteps; ++i){
139  if (m_rhid[i]==m_rhadronIndex && m_vbelowthresh[i]>0) firstslow=false;
140  }
141  if (firstslow || aStep->GetTrack()->GetCurrentStepNumber()<=1 || aStep->GetPostStepPoint()->GetKineticEnergy()==0.){
142 
143  //
144  //int id = aStep->GetTrack()->GetDynamicParticle()->GetDefinition()->GetPDGEncoding();
145  int id = std::abs(aStep->GetTrack()->GetDynamicParticle()->GetDefinition()->GetPDGEncoding());
146  if (id>=1000000 && id<=1100000 && isSUSYParticle(id)){
147  m_rh[m_nsteps] = 1;//other code agrees it's an Rhadron
148  }
149  else{
150  m_rh[m_nsteps] = 0;//other code doesn't agree it's an Rhadron
151  }
152  //
153 
154  if (aStep->GetPreStepPoint()->GetGlobalTime()==0) m_rhadronIndex++;
156 
157  m_pdg[m_nsteps]=aStep->GetTrack()->GetDefinition()->GetPDGEncoding();
158  m_charge[m_nsteps]=aStep->GetTrack()->GetDefinition()->GetPDGCharge();
159  m_dep[m_nsteps]=aStep->GetTotalEnergyDeposit();
160  m_mass[m_nsteps]=aStep->GetTrack()->GetDefinition()->GetPDGMass();
161  m_baryon[m_nsteps]=aStep->GetTrack()->GetDefinition()->GetBaryonNumber();
162  m_t1[m_nsteps]=aStep->GetPreStepPoint()->GetGlobalTime();
163  m_t2[m_nsteps]=aStep->GetPostStepPoint()->GetGlobalTime();
164  G4ThreeVector pos1=aStep->GetPreStepPoint()->GetPosition();
165  m_x1[m_nsteps]=pos1.x();
166  m_y1[m_nsteps]=pos1.y();
167  m_z1[m_nsteps]=pos1.z();
168  G4ThreeVector pos2=aStep->GetPostStepPoint()->GetPosition();
169  m_x2[m_nsteps]=pos2.x();
170  m_y2[m_nsteps]=pos2.y();
171  m_z2[m_nsteps]=pos2.z();
172  m_ke1[m_nsteps]=aStep->GetPreStepPoint()->GetKineticEnergy();
173  m_ke2[m_nsteps]=aStep->GetPostStepPoint()->GetKineticEnergy();
174  m_pt1[m_nsteps]=aStep->GetPreStepPoint()->GetMomentum().perp();
175  m_pt2[m_nsteps]=aStep->GetPostStepPoint()->GetMomentum().perp();
176 
177  //
178  m_minA[m_nsteps]=minA;
179  m_v2[m_nsteps]=aStep->GetPostStepPoint()->GetVelocity();
180  m_vthresh[m_nsteps]=0.15*std::pow(minA,-2./3.)*CLHEP::c_light;
181  m_vbelowthresh[m_nsteps]=(firstslow?1:0);
182  //
183 
185  ++m_nsteps;
186  //std::cout<<"stepping, size is "<<m_nsteps<<std::endl;
187  } // writing the step because it stopped or is the start of the "track"
188  } //rhad true
189  else {
190 
191  //KILL the particles here, so we don't waste time in GEANT tracking what happens to it!
192  if (std::abs(pdg)>1000000 && std::abs(pdg)<10000000){
193  std::cout<<"ACH129: SG_StepNtuple: Killing non-rh track with pdg "<<pdg<<std::endl;
194  }
195  aStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
196  const G4TrackVector *tv = aStep->GetSecondary();
197  //if ((*tv).size()>0) std::cout<<" ... and its "<<(*tv).size()<<" secondaries"<<std::endl;
198  for (unsigned int i=0;i<tv->size();i++){
199  G4Track *t = (*tv)[i];
200  t->SetTrackStatus(fKillTrackAndSecondaries);
201  }
202 
203  } // not an rhad
204 
205  } //m_nsteps<50000
206  }
207 
208  bool SG_StepNtuple::isSUSYParticle(const int id) const
209  {
210  if (id==1000021 || id==1000005 || id==1000006 || id==1000512 || id==1000522 || id==1000991 || id==1000993 ||
211  id==1000612 || id==1000622 || id==1000632 || id==1000642 || id==1000652 || id==1005211 ||
212  id==1006113 || id==1006211 || id==1006213 || id==1006223 || id==1006311 ||
213  id==1006313 || id==1006321 || id==1006323 || id==1006333 ||
214  id==1009111 || id==1009113 || id==1009211 || id==1009213 || id==1009311 ||
215  id==1009313 || id==1009321 || id==1009323 || id==1009223 || id==1009333 ||
216  id==1092112 || id==1091114 || id==1092114 || id==1092212 || id==1092214 || id==1092224 ||
217  id==1093114 || id==1093122 || id==1093214 || id==1093224 || id==1093314 || id==1093324 || id==1093334)
218  return true;
219  return false;
220  }
221 
222 } // namespace G4UA
G4UA::SG_StepNtuple::m_y1
NTuple::Array< float > m_y1
Definition: SG_StepNtuple.h:39
G4UA::SG_StepNtuple::m_evtid
NTuple::Item< long > m_evtid
Definition: SG_StepNtuple.h:37
G4UA::SG_StepNtuple::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override
Definition: SG_StepNtuple.cxx:108
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
G4UA::SG_StepNtuple::m_mass
NTuple::Array< float > m_mass
Definition: SG_StepNtuple.h:42
G4UA::SG_StepNtuple::m_x1
NTuple::Array< float > m_x1
Definition: SG_StepNtuple.h:39
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
G4UA::SG_StepNtuple::m_nevents
long m_nevents
Definition: SG_StepNtuple.h:46
G4UA::SG_StepNtuple::m_y2
NTuple::Array< float > m_y2
Definition: SG_StepNtuple.h:40
mat
GeoMaterial * mat
Definition: LArDetectorConstructionTBEC.cxx:53
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
G4UA::SG_StepNtuple::m_v2
NTuple::Array< float > m_v2
Definition: SG_StepNtuple.h:41
ServiceAccessor.h
G4UA::SG_StepNtuple::SG_StepNtuple
SG_StepNtuple(const std::vector< int > &)
Definition: SG_StepNtuple.cxx:26
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
G4UA::SG_StepNtuple::m_t2
NTuple::Array< float > m_t2
Definition: SG_StepNtuple.h:40
G4UA::SG_StepNtuple::EndOfEventAction
virtual void EndOfEventAction(const G4Event *) override
Definition: SG_StepNtuple.cxx:99
G4UA::SG_StepNtuple::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *) override
Definition: SG_StepNtuple.cxx:91
G4UA::SG_StepNtuple::m_nsteps
NTuple::Item< long > m_nsteps
Definition: SG_StepNtuple.h:37
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
G4UA::SG_StepNtuple::m_rhs
std::set< int > m_rhs
Definition: SG_StepNtuple.h:45
lumiFormat.i
int i
Definition: lumiFormat.py:92
G4UA::SG_StepNtuple::isSUSYParticle
bool isSUSYParticle(const int id) const
Definition: SG_StepNtuple.cxx:208
G4UA::SG_StepNtuple::m_rh
NTuple::Array< int > m_rh
Definition: SG_StepNtuple.h:44
G4UA::SG_StepNtuple::m_ke1
NTuple::Array< float > m_ke1
Definition: SG_StepNtuple.h:43
G4UA::SG_StepNtuple::m_t1
NTuple::Array< float > m_t1
Definition: SG_StepNtuple.h:39
G4UA::SG_StepNtuple::m_z2
NTuple::Array< float > m_z2
Definition: SG_StepNtuple.h:40
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
G4UA::SG_StepNtuple::m_z1
NTuple::Array< float > m_z1
Definition: SG_StepNtuple.h:39
G4UA::SG_StepNtuple::m_baryon
NTuple::Array< int > m_baryon
Definition: SG_StepNtuple.h:38
G4UA::SG_StepNtuple::m_step
NTuple::Array< int > m_step
Definition: SG_StepNtuple.h:44
G4UA::SG_StepNtuple::m_minA
NTuple::Array< float > m_minA
Definition: SG_StepNtuple.h:41
G4UA::SG_StepNtuple::m_rhid
NTuple::Array< int > m_rhid
Definition: SG_StepNtuple.h:44
python.PhysicalConstants.c_light
float c_light
Definition: PhysicalConstants.py:63
G4UA::SG_StepNtuple::BeginOfRunAction
virtual void BeginOfRunAction(const G4Run *) override
Definition: SG_StepNtuple.cxx:36
G4UA::SG_StepNtuple::m_charge
NTuple::Array< int > m_charge
Definition: SG_StepNtuple.h:38
G4UA::SG_StepNtuple::m_pdg
NTuple::Array< int > m_pdg
Definition: SG_StepNtuple.h:38
G4UA::SG_StepNtuple::m_dep
NTuple::Array< float > m_dep
Definition: SG_StepNtuple.h:42
G4UA::SG_StepNtuple::m_rhadronIndex
long m_rhadronIndex
Definition: SG_StepNtuple.h:47
G4UA::SG_StepNtuple::m_vbelowthresh
NTuple::Array< float > m_vbelowthresh
Definition: SG_StepNtuple.h:41
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
G4UA::SG_StepNtuple::m_ke2
NTuple::Array< float > m_ke2
Definition: SG_StepNtuple.h:43
beamspotnt.nt
def nt
Definition: bin/beamspotnt.py:1063
ntupleSvc
INTupleSvc * ntupleSvc()
Definition: ServiceAccessor.h:14
G4UA::SG_StepNtuple::m_x2
NTuple::Array< float > m_x2
Definition: SG_StepNtuple.h:40
G4UA::SG_StepNtuple::m_pt1
NTuple::Array< float > m_pt1
Definition: SG_StepNtuple.h:39
SG_StepNtuple.h
G4UA::SG_StepNtuple::m_pt2
NTuple::Array< float > m_pt2
Definition: SG_StepNtuple.h:40
G4UA::SG_StepNtuple::m_vthresh
NTuple::Array< float > m_vthresh
Definition: SG_StepNtuple.h:41