CMS 3D CMS Logo

HcalSimHitAnalysis.cc
Go to the documentation of this file.
3 
7 
13 
16 
21 
25 
32 
33 #include <TH2F.h>
34 #include <cmath>
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 class HcalSimHitAnalysis : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
41 public:
43  ~HcalSimHitAnalysis() override = default;
44  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
45 
46 protected:
47  void beginJob() override;
48  void analyze(edm::Event const&, edm::EventSetup const&) override;
49  void beginRun(edm::Run const&, edm::EventSetup const&) override {}
50  void endRun(edm::Run const&, edm::EventSetup const&) override {}
51 
52 private:
53  static const int ndets_ = 4;
55  const bool verbose_, testNumber_;
60 };
61 
63  : g4Label_(ps.getUntrackedParameter<std::string>("moduleLabel", "g4SimHits")),
64  hcalHits_(ps.getUntrackedParameter<std::string>("HitCollection", "HcalHits")),
65  verbose_(ps.getUntrackedParameter<bool>("Verbose", false)),
66  testNumber_(ps.getUntrackedParameter<bool>("TestNumber", true)),
67  tok_calo_(consumes<edm::PCaloHitContainer>(edm::InputTag(g4Label_, hcalHits_))),
70  usesResource(TFileService::kSharedResource);
71 
72  edm::LogVerbatim("HitStudy") << "Module Label: " << g4Label_ << " Hits: " << hcalHits_ << " testNumber "
73  << testNumber_;
74 }
75 
78  desc.addUntracked<std::string>("ModuleLabel", "g4SimHits");
79  desc.addUntracked<std::string>("HitCollection", "HcalHits");
80  desc.addUntracked<bool>("Verbose", false);
81  desc.addUntracked<bool>("TestNumber", true);
82  descriptions.add("hcalSimHitAnalysis", desc);
83 }
84 
87  if (!tfile.isAvailable())
88  throw cms::Exception("BadConfig") << "TFileService unavailable: "
89  << "please add it to config file";
90  char name[20], title[120];
91  std::string dets[ndets_] = {"HB", "HE", "HO", "HF"};
92  int nx[ndets_] = {100, 100, 350, 160};
93  double xlo[ndets_] = {0, -300, 0, -160};
94  double xhi[ndets_] = {500, 300, 3500, 160};
95  int ny[ndets_] = {100, 100, 50, 160};
96  double ylo[ndets_] = {170, -300, 375, -160};
97  double yhi[ndets_] = {370, 300, 425, 160};
98  std::string xttl[ndets_] = {"|z| (cm)", "x (cm)", "|z| (cm)", "x (cm)"};
99  std::string yttl[ndets_] = {"#rho (cm)", "y (cm)", "#rho (cm)", "y (cm)"};
100  for (int i = 0; i < ndets_; i++) {
101  sprintf(name, "poszp%d", i);
102  sprintf(title, "%s+", dets[i].c_str());
103  poszp_[i] = tfile->make<TH2F>(name, title, nx[i], xlo[i], xhi[i], ny[i], ylo[i], yhi[i]);
104  poszp_[i]->GetXaxis()->SetTitle(xttl[i].c_str());
105  poszp_[i]->GetYaxis()->SetTitle(yttl[i].c_str());
106  sprintf(title, "%s-", dets[i].c_str());
107  poszp_[i]->GetYaxis()->SetTitleOffset(1.2);
108  poszp_[i]->Sumw2();
109  sprintf(name, "poszn%d", i);
110  poszn_[i] = tfile->make<TH2F>(name, title, nx[i], xlo[i], xhi[i], ny[i], ylo[i], yhi[i]);
111  poszn_[i]->GetXaxis()->SetTitle(xttl[i].c_str());
112  poszn_[i]->GetYaxis()->SetTitle(yttl[i].c_str());
113  poszn_[i]->GetYaxis()->SetTitleOffset(1.2);
114  poszn_[i]->Sumw2();
115  }
116 }
117 
119  if (verbose_)
120  edm::LogVerbatim("HitStudy") << "Run = " << e.id().run() << " Event = " << e.id().event();
121 
122  // get hcalGeometry
123  const CaloGeometry* geo = &iS.getData(tok_geom_);
124  const HcalGeometry* hgeom = static_cast<const HcalGeometry*>(geo->getSubdetectorGeometry(DetId::Hcal, HcalBarrel));
125  const auto& pHRNDC = iS.getData(tok_HRNDC_);
126  const HcalDDDRecConstants* hcons = &pHRNDC;
127 
128  const edm::Handle<edm::PCaloHitContainer>& hitsCalo = e.getHandle(tok_calo_);
129  bool getHits = (hitsCalo.isValid());
130  uint32_t nhits = (getHits) ? hitsCalo->size() : 0;
131  if (verbose_)
132  edm::LogVerbatim("HitStudy") << "HcalSimHitAnalysis: Input flags Hits " << getHits << " with " << nhits << " hits";
133  if (getHits) {
134  std::vector<PCaloHit> hits;
135  hits.insert(hits.end(), hitsCalo->begin(), hitsCalo->end());
136  if (!hits.empty()) {
137  std::map<HcalDetId, double> hitMap;
138  for (auto hit : hits) {
139  double edep = hit.energy();
140  uint32_t id = hit.id();
141  HcalDetId hid = (testNumber_) ? HcalHitRelabeller::relabel(id, hcons) : HcalDetId(id);
142  auto it = hitMap.find(hid);
143  if (it == hitMap.end()) {
144  hitMap[hid] = edep;
145  } else {
146  (it->second) += edep;
147  }
148  }
149 
150  for (auto it : hitMap) {
151  HcalDetId id(it.first);
152  GlobalPoint gpos = hgeom->getPosition(id);
153  HcalSubdetector subdet = (id).subdet();
154  int indx =
155  ((subdet == HcalBarrel)
156  ? 0
157  : ((subdet == HcalEndcap) ? 1 : ((subdet == HcalOuter) ? 2 : ((subdet == HcalForward) ? 3 : -1))));
158  if (verbose_)
159  edm::LogVerbatim("HitStudy") << "HcalSimHitAnalysis: " << id << ":" << it.second << " at " << gpos
160  << " subdet " << subdet << ":" << indx;
161  if (indx >= 0) {
162  double x = ((indx == 0) || (indx == 2)) ? std::abs(gpos.z()) : gpos.x();
163  double y = ((indx == 0) || (indx == 2)) ? (gpos.perp()) : gpos.y();
164  if (id.zside() >= 0)
165  poszp_[indx]->Fill(x, y);
166  else
167  poszn_[indx]->Fill(x, y);
168  }
169  }
170  }
171  }
172 }
173 
174 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
Log< level::Info, true > LogVerbatim
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::vector< PCaloHit > PCaloHitContainer
HcalSimHitAnalysis(const edm::ParameterSet &ps)
T perp() const
Definition: PV3DBase.h:69
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > tok_geom_
void endRun(edm::Run const &, edm::EventSetup const &) override
const std::string hcalHits_
T z() const
Definition: PV3DBase.h:61
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~HcalSimHitAnalysis() override=default
int zside(DetId const &)
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
void beginRun(edm::Run const &, edm::EventSetup const &) override
Definition: tfile.py:1
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool getData(T &iHolder) const
Definition: EventSetup.h:122
DetId relabel(const uint32_t testId) const
static const int ndets_
const std::string g4Label_
const edm::ESGetToken< HcalDDDRecConstants, HcalRecNumberingRecord > tok_HRNDC_
unsigned int id
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void analyze(edm::Event const &, edm::EventSetup const &) override
bool isValid() const
Definition: HandleBase.h:70
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HLT enums.
GlobalPoint getPosition(const DetId &id) const
void beginJob() override
const edm::EDGetTokenT< edm::PCaloHitContainer > tok_calo_
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:34
Definition: Run.h:45