CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CheckSecondary.cc
Go to the documentation of this file.
2 
5 
9 
11 
12 #include "G4Step.hh"
13 #include "G4Track.hh"
14 #include "G4VProcess.hh"
15 #include "G4HCofThisEvent.hh"
16 #include "CLHEP/Units/GlobalSystemOfUnits.h"
17 #include "CLHEP/Units/GlobalPhysicalConstants.h"
18 
19 #include <cmath>
20 #include <iostream>
21 #include <iomanip>
22 
24  typeEnumerator(0),
25  nsec(0),procids(0),
26  px(0),py(0),pz(0),
27  mass(0),deltae(0),
28  procs(0),file(0),
29  tree(0) {
30 
31  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("CheckSecondary");
32  std::string saveFile = m_p.getUntrackedParameter<std::string>("SaveInFile", "None");
33  treatSecondary = new TreatSecondary(m_p);
35 
36  nsec = new std::vector<int>();
37  px = new std::vector<double>();
38  py = new std::vector<double>();
39  pz = new std::vector<double>();
40  mass = new std::vector<double>();
41  deltae = new std::vector<double>();
42  procids = new std::vector<int>();
43  procs = new std::vector<std::string>();
44 
45  if (saveFile != "None") {
46  saveToTree = true;
47  tree = bookTree (saveFile);
48  edm::LogInfo("CheckSecondary") << "Instantiate CheckSecondary with first"
49  << " hadronic interaction information"
50  << " to be saved in file " << saveFile;
51  } else {
52  saveToTree = false;
53  edm::LogInfo("CheckSecondary") << "Instantiate CheckSecondary with first"
54  << " hadronic interaction information"
55  << " not saved";
56  }
57 }
58 
60  if (saveToTree) endTree();
61  if (nsec) delete nsec;
62  if (px) delete px;
63  if (py) delete py;
64  if (pz) delete pz;
65  if (mass) delete mass;
66  if (deltae) delete deltae;
67  if (procs) delete procs;
68  if (procids) delete procids;
69  if (typeEnumerator) delete typeEnumerator;
70  if (treatSecondary) delete treatSecondary;
71 }
72 
74 
75  file = new TFile (fileName.c_str(), "RECREATE");
76  file->cd();
77 
78  TTree * t1 = new TTree("T1", "Secondary Particle Information");
79  t1->Branch("SecondaryPx", "std::vector<double>", &px);
80  t1->Branch("SecondaryPy", "std::vector<double>", &py);
81  t1->Branch("SecondaryPz", "std::vector<double>", &pz);
82  t1->Branch("SecondaryMass", "std::vector<double>", &mass);
83  t1->Branch("NumberSecondaries", "std::vector<int>", &nsec);
84  t1->Branch("DeltaEinInteract", "std::vector<double>", &deltae);
85  t1->Branch("ProcessID", "std::vector<int>", &procids);
86  t1->Branch("ProcessNames", "std::vector<std::string>", &procs);
87  return t1;
88 }
89 
91 
92  edm::LogInfo("CheckSecondary") << "Save the Secondary Tree "
93  << tree->GetName() << " (" << tree
94  << ") in file " << file->GetName() << " ("
95  << file << ")";
96  file->cd();
97  tree->Write();
98  file->Close();
99  delete file;
100 }
101 
103 
104  int iev = (*evt)()->GetEventID();
105  LogDebug("CheckSecondary") << "CheckSecondary::=====> Begin of event = "
106  << iev;
107 
108  (*nsec).clear();
109  (*procs).clear();
110  (*procids).clear();
111  (*deltae).clear();
112  (*px).clear();
113  (*py).clear();
114  (*pz).clear();
115  (*mass).clear();
116 }
117 
119 
120  const G4Track * thTk = (*trk)();
121  treatSecondary->initTrack(thTk);
122  if (thTk->GetParentID() <= 0) storeIt = true;
123  else storeIt = false;
124  nHad = 0;
125  LogDebug("CheckSecondary") << "CheckSecondary:: Track " << thTk->GetTrackID()
126  << " Parent " << thTk->GetParentID() << " Flag "
127  << storeIt;
128 }
129 
130 void CheckSecondary::update(const G4Step * aStep) {
131 
133  int procID;
134  bool hadrInt;
135  double deltaE;
136  std::vector<int> charge;
137  std::vector<math::XYZTLorentzVector> tracks = treatSecondary->tracks(aStep,
138  name,
139  procID,
140  hadrInt,
141  deltaE,
142  charge);
143  if (storeIt && hadrInt) {
144  double pInit = (aStep->GetPreStepPoint()->GetMomentum()).mag();
145  double pEnd = (aStep->GetPostStepPoint()->GetMomentum()).mag();
146  nHad++;
147  int sec = (int)(tracks.size());
148  LogDebug("CheckSecondary") << "CheckSecondary:: Hadronic Interaction "
149  << nHad << " of type " << name << " ID "
150  << procID << " with " << sec << " secondaries "
151  << " and Momentum (MeV/c) at start " << pInit
152  << " and at end " << pEnd;
153  (*nsec).push_back(sec);
154  (*procs).push_back(name);
155  (*procids).push_back(procID);
156  (*deltae).push_back(deltaE);
157  if (nHad == 1) {
158  for (int i=0; i<sec; i++) {
159  double m = tracks[i].M();
160  if (charge[i]<0) m = -m;
161  (*px).push_back(tracks[i].Px());
162  (*py).push_back(tracks[i].Py());
163  (*pz).push_back(tracks[i].Pz());
164  (*mass).push_back(m);
165  }
166  }
167  }
168 }
169 
171 
172  LogDebug("CheckSecondary") << "CheckSecondary::EndofEvent =====> Event "
173  << (*evt)()->GetEventID() << " with "
174  << (*nsec).size() << " hadronic collisions with"
175  << " secondaries produced in each step";
176  for (unsigned int i= 0; i < (*nsec).size(); i++)
177  LogDebug("CheckSecondary") << " " << (*nsec)[i] << " from " << (*procs)[i]
178  << " ID " << (*procids)[i] << " ("
180  << ") deltaE = " << (*deltae)[i] << " MeV";
181  LogDebug("CheckSecondary") << "And " << (*mass).size() << " secondaries "
182  << "produced in the first interactions";
183  for (unsigned int i= 0; i < (*mass).size(); i++)
184  LogDebug("CheckSecondary") << "Secondary " << i << " (" << (*px)[i] << ", "
185  << (*py)[i] << ", " << (*pz)[i] << ", "
186  << (*mass)[i] << ")";
187 
188  if (saveToTree) tree->Fill();
189 }
#define LogDebug(id)
T getParameter(std::string const &) const
std::vector< std::string > * procs
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< double > * pz
std::vector< double > * deltae
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
std::vector< math::XYZTLorentzVector > tracks(const G4Step *step, std::string &procName, int &procID, bool &intr, double &deltaE, std::vector< int > &charges)
std::vector< int > * nsec
G4ProcessTypeEnumerator * typeEnumerator
TTree * bookTree(std::string)
tuple tracks
Definition: testEve_cfg.py:39
void initTrack(const G4Track *trk)
void update(const BeginOfEvent *evt)
This routine will be called when the appropriate signal arrives.
virtual ~CheckSecondary()
std::vector< double > * py
CheckSecondary(const edm::ParameterSet &p)
std::vector< int > * procids
std::vector< double > * px
TreatSecondary * treatSecondary
std::vector< double > * mass