CMS 3D CMS Logo

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