CMS 3D CMS Logo

DoCastorAnalysis.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Forward
4 // Class : DoCastorAnalysis
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: P. Katsas
10 // Created: 02/2007
11 //
12 
14 
17 
21 
29 
30 #include "G4SDManager.hh"
31 #include "G4Step.hh"
32 #include "G4Track.hh"
33 #include "G4Event.hh"
34 #include "G4PrimaryVertex.hh"
35 #include "G4VProcess.hh"
36 #include "G4HCofThisEvent.hh"
37 #include "G4UserEventAction.hh"
38 
39 #include <CLHEP/Units/GlobalSystemOfUnits.h>
40 #include <CLHEP/Units/GlobalPhysicalConstants.h>
41 #include <CLHEP/Random/Randomize.h>
42 
43 #include "TROOT.h"
44 #include "TFile.h"
45 #include "TH1.h"
46 #include "TH2.h"
47 #include "TProfile.h"
48 #include "TNtuple.h"
49 #include "TRandom.h"
50 #include "TLorentzVector.h"
51 #include "TUnixSystem.h"
52 #include "TSystem.h"
53 #include "TMath.h"
54 #include "TF1.h"
55 
56 #include <cassert>
57 #include <cmath>
58 #include <iostream>
59 #include <iomanip>
60 #include <map>
61 #include <memory>
62 #include <string>
63 #include <vector>
64 
65 //#define EDM_ML_DEBUG
66 
68  public Observer<const BeginOfJob *>,
69  public Observer<const BeginOfRun *>,
70  public Observer<const EndOfRun *>,
71  public Observer<const BeginOfEvent *>,
72  public Observer<const EndOfEvent *>,
73  public Observer<const G4Step *> {
74 public:
76  ~DoCastorAnalysis() override;
77 
78 private:
79  // observer classes
80  void update(const BeginOfJob *run) override;
81  void update(const BeginOfRun *run) override;
82  void update(const EndOfRun *run) override;
83  void update(const BeginOfEvent *evt) override;
84  void update(const EndOfEvent *evt) override;
85  void update(const G4Step *step) override;
86 
87 private:
88  int verbosity;
89 
91 
93  TTree *CastorTree;
94 
96 
97  std::vector<double> simhit_x, simhit_y, simhit_z;
98  std::vector<double> simhit_eta, simhit_phi, simhit_energy;
99  std::vector<int> simhit_sector, simhit_module;
100 
101  std::vector<double> *psimhit_x, *psimhit_y, *psimhit_z;
102  std::vector<double> *psimhit_eta, *psimhit_phi, *psimhit_energy;
103  std::vector<int> *psimhit_sector, *psimhit_module;
104 
105  double simhit_etot;
106 };
107 
109  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("DoCastorAnalysis");
110  verbosity = m_Anal.getParameter<int>("Verbosity");
111 
112  TreeFileName = m_Anal.getParameter<std::string>("CastorTreeFileName");
113 
114  if (verbosity > 0) {
115  edm::LogVerbatim("ForwardSim") << std::endl;
116  edm::LogVerbatim("ForwardSim") << "============================================================================";
117  edm::LogVerbatim("ForwardSim") << "DoCastorAnalysis:: Initialized as observer";
118 
119  edm::LogVerbatim("ForwardSim") << " Castor Tree will be created";
120  edm::LogVerbatim("ForwardSim") << " Castor Tree will be in file: " << TreeFileName;
121 #ifdef EDM_ML_DEBUG
122  getchar();
123 #endif
124  edm::LogVerbatim("ForwardSim") << "============================================================================";
125  edm::LogVerbatim("ForwardSim") << std::endl;
126  }
127 
128  edm::LogVerbatim("ForwardSim") << "DoCastorAnalysis: output event root file created";
129  TString treefilename = TreeFileName;
130  CastorOutputEventFile = new TFile(treefilename, "RECREATE");
131 
132  CastorTree = new TTree("Sim", "Sim");
133 
134  CastorTree->Branch("simhit_x", "std::vector<double>", &psimhit_x);
135  CastorTree->Branch("simhit_y", "std::vector<double>", &psimhit_y);
136  CastorTree->Branch("simhit_z", "std::vector<double>", &psimhit_z);
137 
138  CastorTree->Branch("simhit_eta", "std::vector<double>", &psimhit_eta);
139  CastorTree->Branch("simhit_phi", "std::vector<double>", &psimhit_phi);
140  CastorTree->Branch("simhit_energy", "std::vector<double>", &psimhit_energy);
141 
142  CastorTree->Branch("simhit_sector", "std::vector<int>", &psimhit_sector);
143  CastorTree->Branch("simhit_module", "std::vector<int>", &psimhit_module);
144 
145  CastorTree->Branch("simhit_etot", &simhit_etot, "simhit_etot/D");
146 }
147 
149  //destructor of DoCastorAnalysis
150 
151  CastorOutputEventFile->cd();
152  //-- CastorOutputEventFile->Write();
153  CastorTree->Write("", TObject::kOverwrite);
154  edm::LogVerbatim("ForwardSim") << "DoCastorAnalysis: Ntuple event written";
155 #ifdef EDM_ML_DEBUG
156  getchar();
157 #endif
158  CastorOutputEventFile->Close();
159  edm::LogVerbatim("ForwardSim") << "DoCastorAnalysis: Event file closed";
160 #ifdef EDM_ML_DEBUG
161  getchar();
162 #endif
163 
164  if (verbosity > 0) {
165  edm::LogVerbatim("ForwardSim") << std::endl << "DoCastorAnalysis: end of process";
166 #ifdef EDM_ML_DEBUG
167  getchar();
168 #endif
169  }
170 }
171 
172 //=================================================================== per EVENT
173 
174 void DoCastorAnalysis::update(const BeginOfJob *job) { edm::LogVerbatim("ForwardSim") << " Starting new job "; }
175 
176 //==================================================================== per RUN
177 
179  edm::LogVerbatim("ForwardSim") << std::endl << "DoCastorAnalysis: Starting Run";
180 
181  // edm::LogVerbatim("ForwardSim") << "DoCastorAnalysis: output event root file created";
182  // TString treefilename = TreeFileName;
183  // CastorOutputEventFile = new TFile(treefilename,"RECREATE");
184 
185  eventIndex = 1;
186 }
187 
189  edm::LogVerbatim("ForwardSim") << "DoCastorAnalysis: Processing Event Number: " << eventIndex;
190  eventIndex++;
191 }
192 
193 //================= End of EVENT ===============
194 
196  // Look for the Hit Collection
197 
198  // access to the G4 hit collections
199  G4HCofThisEvent *allHC = (*evt)()->GetHCofThisEvent();
200 
201  int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("CastorFI");
202  CaloG4HitCollection *theCAFI = (CaloG4HitCollection *)allHC->GetHC(CAFIid);
203 
204  CastorNumberingScheme *theCastorNumScheme = new CastorNumberingScheme();
205 
206  unsigned int volumeID = 0;
207  // std::map<int,float,std::less<int> > themap;
208 
209  int nentries = theCAFI->entries();
210 #ifdef EDM_ML_DEBUG
211  edm::LogVerbatim("ForwardSim") << "nentries in CAFI: " << nentries;
212  getchar();
213 #endif
214 
215  psimhit_x = &simhit_x;
216  psimhit_x->clear();
217  psimhit_x->reserve(nentries);
218 
219  psimhit_y = &simhit_y;
220  psimhit_y->clear();
221  psimhit_y->reserve(nentries);
222 
223  psimhit_z = &simhit_z;
224  psimhit_z->clear();
225  psimhit_z->reserve(nentries);
226 
228  psimhit_eta->clear();
229  psimhit_eta->reserve(nentries);
230 
232  psimhit_phi->clear();
233  psimhit_phi->reserve(nentries);
234 
236  psimhit_energy->clear();
237  psimhit_energy->reserve(nentries);
238 
240  psimhit_sector->clear();
241  psimhit_sector->reserve(nentries);
242 
244  psimhit_module->clear();
245  psimhit_module->reserve(nentries);
246 
247  simhit_etot = 0;
248 
249  if (nentries > 0) {
250  for (int ihit = 0; ihit < nentries; ihit++) {
251  CaloG4Hit *aHit = (*theCAFI)[ihit];
252  volumeID = aHit->getUnitID();
253 
254  //themap[volumeID] += aHit->getEnergyDeposit();
255  int zside, sector, zmodule;
256 
257  theCastorNumScheme->unpackIndex(volumeID, zside, sector, zmodule);
258 
259  double energy = aHit->getEnergyDeposit() / GeV;
260  //double time = aHit->getTimeSlice();
261 
262  math::XYZPoint pos = aHit->getPosition();
263  double theta = pos.theta();
264  double eta = -log(tan(theta / 2.));
265  double phi = pos.phi();
266 
267  psimhit_x->push_back(pos.x());
268  psimhit_y->push_back(pos.y());
269  psimhit_z->push_back(pos.z());
270 
271  psimhit_eta->push_back(eta);
272  psimhit_phi->push_back(phi);
273  psimhit_energy->push_back(energy);
274 
275  psimhit_sector->push_back(sector);
276  psimhit_module->push_back(zmodule);
277 
278  simhit_etot += energy;
279 
280 #ifdef EDM_ML_DEBUG
281  edm::LogVerbatim("ForwardSim") << "hit " << ihit + 1 << " : x = " << (*psimhit_x)[ihit]
282  << " , eta = " << (*psimhit_eta)[ihit] << " , phi = " << (*psimhit_phi)[ihit]
283  << " , energy = " << (*psimhit_energy)[ihit];
284 #endif
285  }
286 
287  //if (debug) edm::LogVerbatim("ForwardSim") << " total energy = " << simhit_etot;
288 #ifdef EDM_ML_DEBUG
289  getchar();
290 #endif
291  CastorTree->Fill();
292 
293  } // nentries > 0
294  delete theCastorNumScheme;
295 }
296 
298 
299 void DoCastorAnalysis::update(const G4Step *aStep) { ; }
300 
303 
Log< level::Info, true > LogVerbatim
std::vector< double > * psimhit_z
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::vector< double > simhit_eta
std::vector< double > * psimhit_x
std::vector< double > * psimhit_energy
std::vector< double > simhit_x
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
std::vector< double > * psimhit_phi
DoCastorAnalysis(const edm::ParameterSet &p)
int zside(DetId const &)
~DoCastorAnalysis() override
std::vector< double > simhit_phi
static void unpackIndex(const uint32_t &idx, int &z, int &sector, int &zmodule)
std::vector< double > * psimhit_y
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
std::vector< double > simhit_y
std::vector< double > * psimhit_eta
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
TFile * CastorOutputEventFile
std::vector< int > simhit_sector
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
std::vector< int > * psimhit_sector
double getEnergyDeposit() const
Definition: CaloG4Hit.h:79
uint32_t getUnitID() const
Definition: CaloG4Hit.h:66
std::string TreeFileName
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
std::vector< double > simhit_energy
step
Definition: StallMonitor.cc:98
std::vector< int > * psimhit_module
Geom::Theta< T > theta() const
std::vector< int > simhit_module
std::vector< double > simhit_z