CMS 3D CMS Logo

XtalDedxAnalysis.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 #include <string>
4 #include <vector>
5 
6 // user include files
9 
13 
18 
20 
23 
30 
31 #include <TH1F.h>
32 #include <TH1I.h>
33 
34 //#define EDM_ML_DEBUG
35 
36 class XtalDedxAnalysis : public edm::one::EDAnalyzer<edm::one::SharedResources> {
37 public:
38  explicit XtalDedxAnalysis(const edm::ParameterSet &);
39  ~XtalDedxAnalysis() override {}
40 
41  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
42 
43 protected:
44  void beginJob() override {}
45  void analyze(const edm::Event &, const edm::EventSetup &) override;
46  void endJob() override {}
47 
48  void analyzeHits(std::vector<PCaloHit> &,
51 
52 private:
55  const double energyMax_;
59 
60  TH1F *meNHit_[4], *meE1T0_[4], *meE9T0_[4], *meE1T1_[4], *meE9T1_[4];
61  TH1I *mType_;
62 };
63 
65  : caloHitSource_(ps.getParameter<edm::InputTag>("caloHitSource")),
66  simTkLabel_(ps.getParameter<std::string>("moduleLabelTk")),
67  energyMax_(ps.getParameter<double>("energyMax")),
68  tok_calo_(consumes<edm::PCaloHitContainer>(caloHitSource_)),
69  tok_tk_(consumes<edm::SimTrackContainer>(edm::InputTag(simTkLabel_))),
70  tok_vtx_(consumes<edm::SimVertexContainer>(edm::InputTag(simTkLabel_))) {
71  usesResource(TFileService::kSharedResource);
72  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis::Source " << caloHitSource_ << " Track Label "
73  << simTkLabel_ << " Energy Max " << energyMax_;
74 
75  // Book histograms
77 
78  if (!tfile.isAvailable())
79  throw cms::Exception("BadConfig") << "TFileService unavailable: "
80  << "please add it to config file";
81  // Histograms for Hits
82  std::string types[4] = {"total", "by dE/dx", "by delta-ray", "by bremms"};
83  char name[20], title[80];
84  for (int i = 0; i < 4; i++) {
85  sprintf(name, "Hits%d", i);
86  sprintf(title, "Number of hits (%s)", types[i].c_str());
87  meNHit_[i] = tfile->make<TH1F>(name, title, 5000, 0., 5000.);
88  meNHit_[i]->GetXaxis()->SetTitle(title);
89  meNHit_[i]->GetYaxis()->SetTitle("Events");
90  sprintf(name, "E1T0%d", i);
91  sprintf(title, "E1 (Loss %s) in GeV", types[i].c_str());
92  meE1T0_[i] = tfile->make<TH1F>(name, title, 5000, 0, energyMax_);
93  meE1T0_[i]->GetXaxis()->SetTitle(title);
94  meE1T0_[i]->GetYaxis()->SetTitle("Events");
95  sprintf(name, "E9T0%d", i);
96  sprintf(title, "E9 (Loss %s) in GeV", types[i].c_str());
97  meE9T0_[i] = tfile->make<TH1F>(name, title, 5000, 0, energyMax_);
98  meE9T0_[i]->GetXaxis()->SetTitle(title);
99  meE9T0_[i]->GetYaxis()->SetTitle("Events");
100  sprintf(name, "E1T1%d", i);
101  sprintf(title, "E1 (Loss %s with t < 400 ns) in GeV", types[i].c_str());
102  meE1T1_[i] = tfile->make<TH1F>(name, title, 5000, 0, energyMax_);
103  meE1T1_[i]->GetXaxis()->SetTitle(title);
104  meE1T1_[i]->GetYaxis()->SetTitle("Events");
105  sprintf(name, "E9T1%d", i);
106  sprintf(title, "E9 (Loss %s with t < 400 ns) in GeV", types[i].c_str());
107  meE9T1_[i] = tfile->make<TH1F>(name, title, 5000, 0, energyMax_);
108  meE9T1_[i]->GetXaxis()->SetTitle(title);
109  meE9T1_[i]->GetYaxis()->SetTitle("Events");
110  }
111  sprintf(name, "PDGType");
112  sprintf(title, "PDG ID of first level secondary");
113  mType_ = tfile->make<TH1I>(name, title, 5000, -2500, 2500);
114  mType_->GetXaxis()->SetTitle(title);
115  mType_->GetYaxis()->SetTitle("Tracks");
116 }
117 
120  desc.add<edm::InputTag>("caloHitSource", edm::InputTag("g4SimHits", "EcalHitsEB"));
121  desc.add<std::string>("moduleLabelTk", "g4SimHits");
122  desc.add<double>("energyMax", 2.0);
123  descriptions.add("xtalDedxAnalysis", desc);
124 }
125 
127 #ifdef EDM_ML_DEBUG
128  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis::Run = " << e.id().run() << " Event = " << e.id().event();
129 #endif
130  std::vector<PCaloHit> caloHits;
131  const edm::Handle<edm::PCaloHitContainer> &pCaloHits = e.getHandle(tok_calo_);
132 
133  const edm::Handle<edm::SimTrackContainer> &simTk = e.getHandle(tok_tk_);
134  const edm::Handle<edm::SimVertexContainer> &simVtx = e.getHandle(tok_vtx_);
135 
136  if (pCaloHits.isValid()) {
137  caloHits.insert(caloHits.end(), pCaloHits->begin(), pCaloHits->end());
138 #ifdef EDM_ML_DEBUG
139  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis: Hit buffer " << caloHits.size();
140 #endif
141  analyzeHits(caloHits, simTk, simVtx);
142  }
143 }
144 
145 void XtalDedxAnalysis::analyzeHits(std::vector<PCaloHit> &hits,
147  const edm::Handle<edm::SimVertexContainer> &SimVtx) {
148  edm::SimTrackContainer::const_iterator simTrkItr;
149  int nHit = hits.size();
150  double e10[4], e90[4], e11[4], e91[4], hit[4];
151  for (int i = 0; i < 4; i++)
152  e10[i] = e90[i] = e11[i] = e91[i] = hit[i] = 0;
153  for (int i = 0; i < nHit; i++) {
154  double energy = hits[i].energy();
155  double time = hits[i].time();
156  unsigned int id_ = hits[i].id();
157  int trackID = hits[i].geantTrackId();
158  int type = 1;
159  for (simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
160  if (trackID == (int)(simTrkItr->trackId())) {
161  int thePID = simTrkItr->type();
162  if (thePID == 11)
163  type = 2;
164  else if (thePID != -13 && thePID != 13)
165  type = 3;
166  break;
167  }
168  }
169  hit[0]++;
170  hit[type]++;
171  e90[0] += energy;
172  e90[type] += energy;
173  if (time < 400) {
174  e91[0] += energy;
175  e91[type] += energy;
176  }
177  if (id_ == 22) {
178  e10[0] += energy;
179  e10[type] += energy;
180  if (time < 400) {
181  e11[0] += energy;
182  e11[type] += energy;
183  }
184  }
185 #ifdef EDM_ML_DEBUG
186  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis:Hit[" << i << "] ID " << id_ << " E " << energy
187  << " time " << time << " track " << trackID << " type " << type;
188 #endif
189  }
190  for (int i = 0; i < 4; i++) {
191 #ifdef EDM_ML_DEBUG
192  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis:Type(" << i << ") Hit " << hit[i] << " E10 " << e10[i]
193  << " E11 " << e11[i] << " E90 " << e90[i] << " E91 " << e91[i];
194 #endif
195  meNHit_[i]->Fill(hit[i]);
196  meE1T0_[i]->Fill(e10[i]);
197  meE9T0_[i]->Fill(e90[i]);
198  meE1T1_[i]->Fill(e11[i]);
199  meE9T1_[i]->Fill(e91[i]);
200  }
201 
202  // Type of the secondary (coming directly from a generator level track)
203  int nvtx = 0, k1 = 0;
204  edm::SimVertexContainer::const_iterator simVtxItr;
205  for (simVtxItr = SimVtx->begin(); simVtxItr != SimVtx->end(); simVtxItr++)
206  nvtx++;
207 #ifdef EDM_ML_DEBUG
208  int ntrk = 0;
209  for (simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++)
210  ntrk++;
211  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis: " << ntrk << " tracks and " << nvtx << " vertices";
212 #endif
213  for (simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++, ++k1) {
214 #ifdef EDM_ML_DEBUG
215  edm::LogVerbatim("CherenkovAnalysis") << "Track " << k1 << " PDGId " << simTrkItr->type() << " Vertex ID "
216  << simTrkItr->vertIndex() << " Generator " << simTrkItr->noGenpart();
217 #endif
218  if (simTrkItr->noGenpart()) { // This is a secondary
219  int vertIndex = simTrkItr->vertIndex(); // Vertex index of origin
220  if (vertIndex >= 0 && vertIndex < nvtx) {
221  simVtxItr = SimVtx->begin();
222  for (int iv = 0; iv < vertIndex; iv++)
223  simVtxItr++;
224  int parent = simVtxItr->parentIndex(), k2 = 0;
225  for (edm::SimTrackContainer::const_iterator trkItr = SimTk->begin(); trkItr != SimTk->end(); trkItr++, ++k2) {
226 #ifdef EDM_ML_DEBUG
227  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis::Track " << k2 << " ID " << trkItr->trackId()
228  << " (" << parent << ") Generator " << trkItr->noGenpart();
229 #endif
230  if ((int)trkItr->trackId() == parent) { // Parent track
231  if (!trkItr->noGenpart()) { // Generator level
232 #ifdef EDM_ML_DEBUG
233  edm::LogVerbatim("CherenkovAnalysis") << "XtalDedxAnalysis::Track found with ID " << simTrkItr->type();
234 #endif
235  mType_->Fill(simTrkItr->type());
236  }
237  break;
238  }
239  }
240  }
241  }
242  }
243 }
244 
245 // define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
Log< level::Info, true > LogVerbatim
const edm::EDGetTokenT< edm::SimVertexContainer > tok_vtx_
void endJob() override
std::vector< PCaloHit > PCaloHitContainer
void analyze(const edm::Event &, const edm::EventSetup &) override
const edm::InputTag caloHitSource_
const edm::EDGetTokenT< edm::SimTrackContainer > tok_tk_
const double energyMax_
void beginJob() override
void analyzeHits(std::vector< PCaloHit > &, const edm::Handle< edm::SimTrackContainer > &, const edm::Handle< edm::SimVertexContainer > &)
XtalDedxAnalysis(const edm::ParameterSet &)
Definition: tfile.py:1
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< SimVertex > SimVertexContainer
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool isValid() const
Definition: HandleBase.h:70
~XtalDedxAnalysis() override
HLT enums.
const edm::EDGetTokenT< edm::PCaloHitContainer > tok_calo_
std::vector< SimTrack > SimTrackContainer
const std::string simTkLabel_