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 //
15 
18 
19 #include "TFile.h"
20 #include <cmath>
21 #include <iostream>
22 #include <iomanip>
23 #include <memory>
24 #include <vector>
25 
26 #define debug 0
27 
29  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("DoCastorAnalysis");
30  verbosity = m_Anal.getParameter<int>("Verbosity");
31 
32  TreeFileName = m_Anal.getParameter<std::string>("CastorTreeFileName");
33 
34  if (verbosity > 0) {
35  std::cout << std::endl;
36  std::cout << "============================================================================" << std::endl;
37  std::cout << "DoCastorAnalysis:: Initialized as observer" << std::endl;
38 
39  std::cout << " Castor Tree will be created" << std::endl;
40  std::cout << " Castor Tree will be in file: " << TreeFileName << std::endl;
41  if (debug)
42  getchar();
43 
44  std::cout << "============================================================================" << std::endl;
45  std::cout << std::endl;
46  }
47 
48  std::cout << "DoCastorAnalysis: output event root file created" << std::endl;
49  TString treefilename = TreeFileName;
50  CastorOutputEventFile = new TFile(treefilename, "RECREATE");
51 
52  CastorTree = new TTree("Sim", "Sim");
53 
54  CastorTree->Branch("simhit_x", "std::vector<double>", &psimhit_x);
55  CastorTree->Branch("simhit_y", "std::vector<double>", &psimhit_y);
56  CastorTree->Branch("simhit_z", "std::vector<double>", &psimhit_z);
57 
58  CastorTree->Branch("simhit_eta", "std::vector<double>", &psimhit_eta);
59  CastorTree->Branch("simhit_phi", "std::vector<double>", &psimhit_phi);
60  CastorTree->Branch("simhit_energy", "std::vector<double>", &psimhit_energy);
61 
62  // CastorTree->Branch("simhit_time","std::vector<double>",&psimhit_time);
63  CastorTree->Branch("simhit_sector", "std::vector<int>", &psimhit_sector);
64  CastorTree->Branch("simhit_module", "std::vector<int>", &psimhit_module);
65 
66  CastorTree->Branch("simhit_etot", &simhit_etot, "simhit_etot/D");
67 }
68 
70  //destructor of DoCastorAnalysis
71 
73  //-- CastorOutputEventFile->Write();
74  CastorTree->Write("", TObject::kOverwrite);
75  std::cout << "DoCastorAnalysis: Ntuple event written" << std::endl;
76  if (debug)
77  getchar();
78  CastorOutputEventFile->Close();
79  std::cout << "DoCastorAnalysis: Event file closed" << std::endl;
80  if (debug)
81  getchar();
82 
83  if (verbosity > 0) {
84  std::cout << std::endl << "DoCastorAnalysis: end of process" << std::endl;
85  if (debug)
86  getchar();
87  }
88 }
89 
90 //=================================================================== per EVENT
91 
92 void DoCastorAnalysis::update(const BeginOfJob* job) { std::cout << " Starting new job " << std::endl; }
93 
94 //==================================================================== per RUN
95 
97  std::cout << std::endl << "DoCastorAnalysis: Starting Run" << std::endl;
98 
99  // std::cout << "DoCastorAnalysis: output event root file created"<< std::endl;
100  // TString treefilename = TreeFileName;
101  // CastorOutputEventFile = new TFile(treefilename,"RECREATE");
102 
103  eventIndex = 1;
104 }
105 
107  std::cout << "DoCastorAnalysis: Processing Event Number: " << eventIndex << std::endl;
108  eventIndex++;
109 }
110 
111 //================= End of EVENT ===============
112 
114  // Look for the Hit Collection
115 
116  // access to the G4 hit collections
117  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
118 
119  int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("CastorFI");
120  CaloG4HitCollection* theCAFI = (CaloG4HitCollection*)allHC->GetHC(CAFIid);
121 
122  CastorNumberingScheme* theCastorNumScheme = new CastorNumberingScheme();
123 
124  unsigned int volumeID = 0;
125  // std::map<int,float,std::less<int> > themap;
126 
127  int nentries = theCAFI->entries();
128  if (debug)
129  std::cout << "nentries in CAFI: " << nentries << std::endl;
130  if (debug)
131  getchar();
132 
133  psimhit_x = &simhit_x;
134  psimhit_x->clear();
135  psimhit_x->reserve(nentries);
136 
137  psimhit_y = &simhit_y;
138  psimhit_y->clear();
139  psimhit_y->reserve(nentries);
140 
141  psimhit_z = &simhit_z;
142  psimhit_z->clear();
143  psimhit_z->reserve(nentries);
144 
146  psimhit_eta->clear();
147  psimhit_eta->reserve(nentries);
148 
150  psimhit_phi->clear();
151  psimhit_phi->reserve(nentries);
152 
154  psimhit_energy->clear();
155  psimhit_energy->reserve(nentries);
156 
157  //psimhit_time=&simhit_time;
158  //psimhit_time->clear();
159  //psimhit_time->reserve(nentries);
160 
162  psimhit_sector->clear();
163  psimhit_sector->reserve(nentries);
164 
166  psimhit_module->clear();
167  psimhit_module->reserve(nentries);
168 
169  simhit_etot = 0;
170 
171  if (nentries > 0) {
172  for (int ihit = 0; ihit < nentries; ihit++) {
173  CaloG4Hit* aHit = (*theCAFI)[ihit];
174  volumeID = aHit->getUnitID();
175 
176  //themap[volumeID] += aHit->getEnergyDeposit();
177  int zside, sector, zmodule;
178 
179  theCastorNumScheme->unpackIndex(volumeID, zside, sector, zmodule);
180 
181  double energy = aHit->getEnergyDeposit() / GeV;
182  //double time = aHit->getTimeSlice();
183 
184  math::XYZPoint pos = aHit->getPosition();
185  double theta = pos.theta();
186  double eta = -log(tan(theta / 2.));
187  double phi = pos.phi();
188 
189  psimhit_x->push_back(pos.x());
190  psimhit_y->push_back(pos.y());
191  psimhit_z->push_back(pos.z());
192 
193  psimhit_eta->push_back(eta);
194  psimhit_phi->push_back(phi);
195  psimhit_energy->push_back(energy);
196 
197  // psimhit_time->push_back(time);
198  psimhit_sector->push_back(sector);
199  psimhit_module->push_back(zmodule);
200 
201  simhit_etot += energy;
202 
203  if (debug)
204  std::cout << "hit " << ihit + 1 << " : x = " << (*psimhit_x)[ihit] << " , eta = " << (*psimhit_eta)[ihit]
205  << " , phi = " << (*psimhit_phi)[ihit] << " , energy = " << (*psimhit_energy)[ihit] << std::endl;
206  }
207 
208  //if(debug) std::cout<<" total energy = "<<simhit_etot<<std::endl;
209  if (debug)
210  getchar();
211  CastorTree->Fill();
212 
213  } // nentries > 0
214  delete theCastorNumScheme;
215 }
216 
218 
219 void DoCastorAnalysis::update(const G4Step* aStep) { ; }
std::vector< double > * psimhit_z
T getParameter(std::string const &) const
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
#define debug
const double GeV
Definition: MathUtil.h:16
std::vector< double > simhit_eta
std::vector< double > * psimhit_x
std::vector< double > simhit_x
std::vector< double > * psimhit_energy
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
std::vector< double > * psimhit_phi
Geom::Theta< T > theta() const
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
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
std::string TreeFileName
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
std::vector< double > simhit_energy
uint32_t getUnitID() const
Definition: CaloG4Hit.h:65
std::vector< int > * psimhit_module
double getEnergyDeposit() const
Definition: CaloG4Hit.h:77
std::vector< int > simhit_module
std::vector< double > simhit_z