CMS 3D CMS Logo

CTPPSProtonReconstructionEfficiencyEstimatorMC.cc
Go to the documentation of this file.
1 /****************************************************************************
2  * Authors:
3  * Jan Kašpar
4  ****************************************************************************/
5 
12 
14 
17 
19 
27 
28 #include "TFile.h"
29 #include "TH1D.h"
30 #include "TH2D.h"
31 #include "TProfile.h"
32 #include "TGraphErrors.h"
33 
34 #include <map>
35 #include <string>
36 
37 //----------------------------------------------------------------------------------------------------
38 
40 public:
42 
43 private:
44  void analyze(const edm::Event &, const edm::EventSetup &) override;
45  void endJob() override;
46 
48 
51 
53 
55 
57 
58  unsigned int rpId_45_N_, rpId_45_F_;
59  unsigned int rpId_56_N_, rpId_56_F_;
60 
61  std::map<unsigned int, unsigned int> rpDecId_near_, rpDecId_far_;
62 
64 
65  unsigned int verbosity_;
66 
67  struct PlotGroup {
68  std::unique_ptr<TProfile> p_eff_vs_xi;
69 
70  std::unique_ptr<TH1D> h_n_part_acc_nr, h_n_part_acc_fr;
71 
73  : p_eff_vs_xi(new TProfile("", ";#xi_{simu};efficiency", 19, 0.015, 0.205)),
74  h_n_part_acc_nr(new TH1D("", ";n particles in acceptance", 6, -0.5, +5.5)),
75  h_n_part_acc_fr(new TH1D("", ";n particles in acceptance", 6, -0.5, +5.5)) {}
76 
77  void write() const {
78  p_eff_vs_xi->Write("p_eff_vs_xi");
79  h_n_part_acc_nr->Write("h_n_part_acc_nr");
80  h_n_part_acc_fr->Write("h_n_part_acc_fr");
81  }
82  };
83 
84  std::map<unsigned int, std::map<unsigned int, PlotGroup>> plots_; // map: arm, n(particles in acceptance) --> plots
85 };
86 
87 //----------------------------------------------------------------------------------------------------
88 
89 using namespace std;
90 using namespace edm;
91 using namespace HepMC;
92 
93 //----------------------------------------------------------------------------------------------------
94 
96  const edm::ParameterSet &iConfig)
97  : tokenHepMCAfterSmearing_(
98  consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("tagHepMCAfterSmearing"))),
99  tokenStripRecHitsPerParticle_(consumes<std::map<int, edm::DetSetVector<TotemRPRecHit>>>(
100  iConfig.getParameter<edm::InputTag>("tagStripRecHitsPerParticle"))),
101  tokenPixelRecHitsPerParticle_(consumes<std::map<int, edm::DetSetVector<CTPPSPixelRecHit>>>(
102  iConfig.getParameter<edm::InputTag>("tagPixelRecHitsPerParticle"))),
103  tracksToken_(consumes<CTPPSLocalTrackLiteCollection>(iConfig.getParameter<edm::InputTag>("tagTracks"))),
104  tokenRecoProtonsMultiRP_(
105  consumes<reco::ForwardProtonCollection>(iConfig.getParameter<InputTag>("tagRecoProtonsMultiRP"))),
106  lhcInfoESToken_(esConsumes(ESInputTag("", iConfig.getParameter<std::string>("lhcInfoLabel")))),
107 
108  rpId_45_N_(iConfig.getParameter<unsigned int>("rpId_45_N")),
109  rpId_45_F_(iConfig.getParameter<unsigned int>("rpId_45_F")),
110  rpId_56_N_(iConfig.getParameter<unsigned int>("rpId_56_N")),
111  rpId_56_F_(iConfig.getParameter<unsigned int>("rpId_56_F")),
112 
113  outputFile_(iConfig.getParameter<string>("outputFile")),
114 
115  verbosity_(iConfig.getUntrackedParameter<unsigned int>("verbosity", 0)) {
118 
121 
122  // book plots
123  for (unsigned int arm = 0; arm < 2; ++arm) {
124  for (unsigned int np = 1; np <= 5; ++np)
125  plots_[arm][np] = PlotGroup();
126  }
127 }
128 
129 //----------------------------------------------------------------------------------------------------
130 
132  std::ostringstream os;
133 
134  // get conditions
135  const auto &lhcInfo = iSetup.getData(lhcInfoESToken_);
136 
137  // get input
138  edm::Handle<edm::HepMCProduct> hHepMCAfterSmearing;
139  iEvent.getByToken(tokenHepMCAfterSmearing_, hHepMCAfterSmearing);
140  HepMC::GenEvent *hepMCEventAfterSmearing = (HepMC::GenEvent *)hHepMCAfterSmearing->GetEvent();
141 
143  iEvent.getByToken(tokenStripRecHitsPerParticle_, hStripRecHitsPerParticle);
144 
146  iEvent.getByToken(tokenPixelRecHitsPerParticle_, hPixelRecHitsPerParticle);
147 
149  iEvent.getByToken(tracksToken_, tracks);
150 
151  Handle<reco::ForwardProtonCollection> hRecoProtonsMultiRP;
152  iEvent.getByToken(tokenRecoProtonsMultiRP_, hRecoProtonsMultiRP);
153 
154  // buffer for particle information
155  struct ParticleInfo {
156  unsigned int arm = 2;
157  double xi = 0.;
158  std::map<unsigned int, unsigned int> recHitsPerRP;
159  bool inAcceptanceNear = false, inAcceptanceFar = false, inAcceptance = false;
160  };
161 
162  std::map<int, ParticleInfo> particleInfo; // barcode --> info
163 
164  // process HepMC
165  for (auto it = hepMCEventAfterSmearing->particles_begin(); it != hepMCEventAfterSmearing->particles_end(); ++it) {
166  const auto &part = *it;
167 
168  // accept only stable non-beam protons
169  if (part->pdg_id() != 2212)
170  continue;
171 
172  if (part->status() != 1)
173  continue;
174 
175  if (part->is_beam())
176  continue;
177 
178  const auto &mom = part->momentum();
179 
180  if (mom.e() < 4500.)
181  continue;
182 
183  ParticleInfo info;
184 
185  info.arm = (mom.z() > 0.) ? 0 : 1;
186 
187  const double p_nom = lhcInfo.energy();
188  info.xi = (p_nom - mom.rho()) / p_nom;
189 
190  particleInfo[part->barcode()] = std::move(info);
191  }
192 
193  // check acceptance
194  for (const auto &pp : *hStripRecHitsPerParticle) {
195  const auto barcode = pp.first;
196 
197  for (const auto &ds : pp.second) {
198  CTPPSDetId detId(ds.detId());
199  CTPPSDetId rpId = detId.rpId();
200  particleInfo[barcode].recHitsPerRP[rpId] += ds.size();
201  }
202  }
203 
204  for (const auto &pp : *hPixelRecHitsPerParticle) {
205  const auto barcode = pp.first;
206 
207  for (const auto &ds : pp.second) {
208  CTPPSDetId detId(ds.detId());
209  CTPPSDetId rpId = detId.rpId();
210  particleInfo[barcode].recHitsPerRP[rpId] += ds.size();
211  }
212  }
213 
214  std::map<unsigned int, bool> isStripRPNear, isStripRPFar;
215 
216  for (auto &pp : particleInfo) {
217  if (verbosity_)
218  os << "* barcode=" << pp.first << ", arm=" << pp.second.arm << ", xi=" << pp.second.xi << std::endl;
219 
220  for (const auto &rpp : pp.second.recHitsPerRP) {
221  CTPPSDetId rpId(rpp.first);
222  unsigned int needed_rec_hits = 1000;
224  needed_rec_hits = 6;
226  needed_rec_hits = 3;
227 
228  unsigned int rpDecId = rpId.arm() * 100 + rpId.station() * 10 + rpId.rp();
229 
230  if (rpId.subdetId() == CTPPSDetId::sdTrackingStrip) {
231  if (rpDecId == rpDecId_near_[rpId.arm()])
232  isStripRPNear[rpId.arm()] = true;
233  if (rpDecId == rpDecId_far_[rpId.arm()])
234  isStripRPFar[rpId.arm()] = true;
235  }
236 
237  if (rpp.second >= needed_rec_hits) {
238  if (rpDecId == rpDecId_near_[rpId.arm()])
239  pp.second.inAcceptanceNear = true;
240  if (rpDecId == rpDecId_far_[rpId.arm()])
241  pp.second.inAcceptanceFar = true;
242  }
243 
244  if (verbosity_)
245  os << " RP " << rpDecId << ": " << rpp.second << " hits" << std::endl;
246  }
247 
248  pp.second.inAcceptance = pp.second.inAcceptanceNear && pp.second.inAcceptanceFar;
249 
250  if (verbosity_)
251  os << " inAcceptance: near=" << pp.second.inAcceptanceNear << ", far=" << pp.second.inAcceptanceFar
252  << ", global=" << pp.second.inAcceptance << std::endl;
253  }
254 
255  // count particles in acceptance
256  struct ArmCounter {
257  unsigned int near = 0, far = 0, global = 0;
258  };
259  std::map<unsigned int, ArmCounter> nParticlesInAcceptance;
260  for (auto &pp : particleInfo) {
261  auto &counter = nParticlesInAcceptance[pp.second.arm];
262  if (pp.second.inAcceptanceNear)
263  counter.near++;
264  if (pp.second.inAcceptanceFar)
265  counter.far++;
266  if (pp.second.inAcceptance)
267  counter.global++;
268  }
269 
270  // count reconstructed tracks
271  std::map<unsigned int, ArmCounter> nReconstructedTracks;
272  for (const auto &tr : *tracks) {
273  CTPPSDetId rpId(tr.rpId());
274  unsigned int rpDecId = rpId.arm() * 100 + rpId.station() * 10 + rpId.rp();
275 
276  if (rpDecId == rpDecId_near_[rpId.arm()])
277  nReconstructedTracks[rpId.arm()].near++;
278  if (rpDecId == rpDecId_far_[rpId.arm()])
279  nReconstructedTracks[rpId.arm()].far++;
280  }
281 
282  // count reconstructed protons
283  std::map<unsigned int, unsigned int> nReconstructedProtons;
284  for (const auto &pr : *hRecoProtonsMultiRP) {
285  if (!pr.validFit())
286  continue;
287 
288  unsigned int arm = 2;
289  if (pr.lhcSector() == reco::ForwardProton::LHCSector::sector45)
290  arm = 0;
291  if (pr.lhcSector() == reco::ForwardProton::LHCSector::sector56)
292  arm = 1;
293 
294  nReconstructedProtons[arm]++;
295  }
296 
297  // fill plots
298  for (unsigned int arm = 0; arm < 2; arm++) {
299  const auto &npa = nParticlesInAcceptance[arm];
300  const auto &nrt = nReconstructedTracks[arm];
301 
302  if (verbosity_)
303  os << "* arm " << arm << ": nRecoProtons=" << nReconstructedProtons[arm]
304  << " (tracks near=" << nReconstructedTracks[arm].near << ", far=" << nReconstructedTracks[arm].far
305  << "), nAcc=" << npa.global << " (near=" << npa.near << ", far=" << npa.far << ")" << std::endl;
306 
307  // skip event if no track in global acceptance
308  if (npa.global < 1)
309  continue;
310 
311  const auto &p = plots_[arm][npa.global];
312 
313  p.h_n_part_acc_nr->Fill(npa.near);
314  p.h_n_part_acc_fr->Fill(npa.far);
315 
316  // skip events with some local reconstruction inefficiency
317  if (nrt.near != npa.near || nrt.far != npa.far)
318  continue;
319 
320  const double eff = double(nReconstructedProtons[arm]) / npa.global;
321 
322  if (verbosity_)
323  os << " eff=" << eff << std::endl;
324 
325  for (auto &pp : particleInfo) {
326  if (pp.second.arm != arm || !pp.second.inAcceptance)
327  continue;
328 
329  p.p_eff_vs_xi->Fill(pp.second.xi, eff);
330  }
331  }
332 
333  if (verbosity_)
334  edm::LogInfo("CTPPSProtonReconstructionEfficiencyEstimatorMC") << os.str();
335 }
336 
337 //----------------------------------------------------------------------------------------------------
338 
340  auto f_out = std::make_unique<TFile>(outputFile_.c_str(), "recreate");
341 
342  for (const auto &ait : plots_) {
343  char buf[100];
344  sprintf(buf, "arm%u", ait.first);
345  TDirectory *d_arm = f_out->mkdir(buf);
346 
347  for (const auto &npit : ait.second) {
348  sprintf(buf, "%u", npit.first);
349  TDirectory *d_np = d_arm->mkdir(buf);
350  gDirectory = d_np;
351 
352  npit.second.write();
353  }
354  }
355 }
356 
357 //----------------------------------------------------------------------------------------------------
358 
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
static const TGPicture * info(bool iBackgroundIsBlack)
uint32_t arm() const
Definition: CTPPSDetId.h:51
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::map< unsigned int, std::map< unsigned int, PlotGroup > > plots_
edm::EDGetTokenT< reco::ForwardProtonCollection > tokenRecoProtonsMultiRP_
edm::EDGetTokenT< std::map< int, edm::DetSetVector< CTPPSPixelRecHit > > > tokenPixelRecHitsPerParticle_
int iEvent
Definition: GenABIO.cc:224
int np
Definition: AMPTWrapper.h:43
CTPPSDetId rpId() const
Definition: CTPPSDetId.h:78
void analyze(const edm::Event &, const edm::EventSetup &) override
Reconstructed hit in TOTEM RP.
Definition: TotemRPRecHit.h:18
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
bool getData(T &iHolder) const
Definition: EventSetup.h:122
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
Log< level::Info, false > LogInfo
uint32_t rp() const
Definition: CTPPSDetId.h:65
auto const & tracks
cannot be loose
std::vector< CTPPSLocalTrackLite > CTPPSLocalTrackLiteCollection
Collection of CTPPSLocalTrackLite objects.
part
Definition: HCALResponse.h:20
uint32_t station() const
Definition: CTPPSDetId.h:58
Base class for CTPPS detector IDs.
Definition: CTPPSDetId.h:32
fixed size matrix
HLT enums.
edm::EDGetTokenT< std::map< int, edm::DetSetVector< TotemRPRecHit > > > tokenStripRecHitsPerParticle_
std::vector< ForwardProton > ForwardProtonCollection
Collection of ForwardProton objects.
def move(src, dest)
Definition: eostools.py:511