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 
72  PlotGroup()
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 
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;
223  if (rpId.subdetId() == CTPPSDetId::sdTrackingStrip)
224  needed_rec_hits = 6;
225  if (rpId.subdetId() == CTPPSDetId::sdTrackingPixel)
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 
TotemRPRecHit
Reconstructed hit in TOTEM RP.
Definition: TotemRPRecHit.h:17
edm::DetSetVector
Definition: DetSetVector.h:61
counter
Definition: counter.py:1
CTPPSLocalTrackLiteCollection
std::vector< CTPPSLocalTrackLite > CTPPSLocalTrackLiteCollection
Collection of CTPPSLocalTrackLite objects.
Definition: CTPPSLocalTrackLiteFwd.h:18
EDAnalyzer.h
edm::ESInputTag
Definition: ESInputTag.h:87
CTPPSPixelRecHit
Definition: CTPPSPixelRecHit.h:17
CTPPSProtonReconstructionEfficiencyEstimatorMC::tokenHepMCAfterSmearing_
edm::EDGetTokenT< edm::HepMCProduct > tokenHepMCAfterSmearing_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:48
CTPPSProtonReconstructionEfficiencyEstimatorMC::PlotGroup::h_n_part_acc_fr
std::unique_ptr< TH1D > h_n_part_acc_fr
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:71
ESHandle.h
edm::EDGetTokenT< edm::HepMCProduct >
edm
HLT enums.
Definition: AlignableModifier.h:19
np
int np
Definition: AMPTWrapper.h:43
LHCInfo.h
if
if(0==first)
Definition: CAHitNtupletGeneratorKernelsImpl.h:58
CTPPSProtonReconstructionEfficiencyEstimatorMC::PlotGroup
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:68
info
static const TGPicture * info(bool iBackgroundIsBlack)
Definition: FWCollectionSummaryWidget.cc:153
CTPPSLocalTrackLite.h
CTPPSProtonReconstructionEfficiencyEstimatorMC::endJob
void endJob() override
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:338
TotemRPRecHit.h
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
CTPPSProtonReconstructionEfficiencyEstimatorMC::rpDecId_far_
std::map< unsigned int, unsigned int > rpDecId_far_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:62
edm::Handle< edm::HepMCProduct >
CTPPSPixelRecHit.h
CTPPSProtonReconstructionEfficiencyEstimatorMC::rpId_56_N_
unsigned int rpId_56_N_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:60
HepMC::GenEvent
Definition: hepmc_rootio.cc:9
CTPPSProtonReconstructionEfficiencyEstimatorMC::lhcInfoESToken_
edm::ESGetToken< LHCInfo, LHCInfoRcd > lhcInfoESToken_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:57
CTPPSDetId::sdTrackingStrip
Definition: CTPPSDetId.h:44
MakerMacros.h
part
part
Definition: HCALResponse.h:20
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
CTPPSProtonReconstructionEfficiencyEstimatorMC::PlotGroup::p_eff_vs_xi
std::unique_ptr< TProfile > p_eff_vs_xi
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:69
ForwardProtonFwd.h
CTPPSDetId::sdTrackingPixel
Definition: CTPPSDetId.h:44
CTPPSProtonReconstructionEfficiencyEstimatorMC::rpId_45_F_
unsigned int rpId_45_F_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:59
CTPPSProtonReconstructionEfficiencyEstimatorMC::verbosity_
unsigned int verbosity_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:66
CTPPSProtonReconstructionEfficiencyEstimatorMC::tokenStripRecHitsPerParticle_
edm::EDGetTokenT< std::map< int, edm::DetSetVector< TotemRPRecHit > > > tokenStripRecHitsPerParticle_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:50
reco::ForwardProtonCollection
std::vector< ForwardProton > ForwardProtonCollection
Collection of ForwardProton objects.
Definition: ForwardProtonFwd.h:25
edm::ParameterSet
Definition: ParameterSet.h:47
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CTPPSProtonReconstructionEfficiencyEstimatorMC::rpId_56_F_
unsigned int rpId_56_F_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:60
sipixeldigitoraw
Definition: SiPixelDigiToRaw.cc:32
Event.h
tracks
const uint32_t *__restrict__ const HitContainer *__restrict__ TkSoA *__restrict__ tracks
Definition: CAHitNtupletGeneratorKernelsImpl.h:176
CTPPSProtonReconstructionEfficiencyEstimatorMC::rpId_45_N_
unsigned int rpId_45_N_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:59
CTPPSProtonReconstructionEfficiencyEstimatorMC::PlotGroup::h_n_part_acc_nr
std::unique_ptr< TH1D > h_n_part_acc_nr
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:71
ForwardProton.h
reco::ForwardProton::LHCSector::sector56
CTPPSDetId
Base class for CTPPS detector IDs.
Definition: CTPPSDetId.h:31
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
CTPPSProtonReconstructionEfficiencyEstimatorMC
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:38
CTPPSProtonReconstructionEfficiencyEstimatorMC::outputFile_
std::string outputFile_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:64
CTPPSProtonReconstructionEfficiencyEstimatorMC::CTPPSProtonReconstructionEfficiencyEstimatorMC
CTPPSProtonReconstructionEfficiencyEstimatorMC(const edm::ParameterSet &)
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:94
edm::EventSetup
Definition: EventSetup.h:58
CTPPSProtonReconstructionEfficiencyEstimatorMC::tracksToken_
edm::EDGetTokenT< CTPPSLocalTrackLiteCollection > tracksToken_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:53
edm::HepMCProduct::GetEvent
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
DetSetVector.h
visDQMUpload.buf
buf
Definition: visDQMUpload.py:160
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::ESGetToken< LHCInfo, LHCInfoRcd >
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
CTPPSProtonReconstructionEfficiencyEstimatorMC::rpDecId_near_
std::map< unsigned int, unsigned int > rpDecId_near_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:62
CTPPSProtonReconstructionEfficiencyEstimatorMC::PlotGroup::PlotGroup
PlotGroup()
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:73
protons_cff.arm
arm
Definition: protons_cff.py:39
profile_2016_postTS2_cff.rpId
rpId
Definition: profile_2016_postTS2_cff.py:21
protons_cff.xi
xi
Definition: protons_cff.py:31
CTPPSProtonReconstructionEfficiencyEstimatorMC::tokenPixelRecHitsPerParticle_
edm::EDGetTokenT< std::map< int, edm::DetSetVector< CTPPSPixelRecHit > > > tokenPixelRecHitsPerParticle_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:51
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
CTPPSProtonReconstructionEfficiencyEstimatorMC::PlotGroup::write
void write() const
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:78
Frameworkfwd.h
reco::ForwardProton::LHCSector::sector45
HepMC
Definition: GenParticle.h:15
CTPPSProtonReconstructionEfficiencyEstimatorMC::tokenRecoProtonsMultiRP_
edm::EDGetTokenT< reco::ForwardProtonCollection > tokenRecoProtonsMultiRP_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:55
CTPPSLocalTrackLiteFwd.h
EventSetup.h
CTPPSDetId.h
CTPPSProtonReconstructionEfficiencyEstimatorMC::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:130
CTPPSProtonReconstructionEfficiencyEstimatorMC::plots_
std::map< unsigned int, std::map< unsigned int, PlotGroup > > plots_
Definition: CTPPSProtonReconstructionEfficiencyEstimatorMC.cc:85
genParticles_cff.map
map
Definition: genParticles_cff.py:11
edm::HepMCProduct
Definition: HepMCProduct.h:21
LHCInfoRcd.h
createTree.pp
pp
Definition: createTree.py:17
HepMCProduct.h
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::Event
Definition: Event.h:73
ParticleInfo
Definition: ParticleTable.h:15
edm::InputTag
Definition: InputTag.h:15