CMS 3D CMS Logo

PPSAlignmentWorker.cc
Go to the documentation of this file.
1 /****************************************************************************
2 * Authors:
3 * Jan Kašpar (jan.kaspar@gmail.com)
4 * Mateusz Kocot (mateuszkocot99@gmail.com)
5 ****************************************************************************/
6 
10 
17 
21 
24 
25 #include <map>
26 #include <string>
27 #include <cmath>
28 #include <memory>
29 
30 #include "TH2D.h"
31 #include "TGraph.h"
32 
33 //----------------------------------------------------------------------------------------------------
34 
36 public:
37  PPSAlignmentWorker(const edm::ParameterSet& iConfig);
38 
39  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
40 
41 private:
42  void bookHistograms(DQMStore::IBooker& iBooker, edm::Run const&, edm::EventSetup const& iSetup) override;
43  void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
44 
45  // ------------ structures ------------
46  struct SectorData {
48 
49  // hit distributions
50  std::map<unsigned int, MonitorElement*> m_h2_y_vs_x_bef_sel;
51  std::map<unsigned int, MonitorElement*> m_h2_y_vs_x_mlt_sel;
52  std::map<unsigned int, MonitorElement*> m_h2_y_vs_x_aft_sel;
53 
54  // cut plots
59 
64 
65  // near-far plots
68 
69  struct SlicePlots {
73 
74  SlicePlots();
76  void fill(const double y, const double yDiff, const bool debug);
77  };
78 
79  std::map<unsigned int, SlicePlots> x_slice_plots_N, x_slice_plots_F;
80 
81  void init(DQMStore::IBooker& iBooker,
84  const std::string& rootDir,
85  bool debug);
86 
88  };
89 
90  // ------------ member data ------------
93 
94  const std::vector<edm::InputTag> tracksTags_;
95  std::vector<edm::EDGetTokenT<CTPPSLocalTrackLiteCollection>> tracksTokens_;
96 
99 
101  const bool debug_;
102 };
103 
104 // -------------------------------- DQMEDAnalyzer methods --------------------------------
105 
107  : esTokenBookHistograms_(
109  edm::ESInputTag("", iConfig.getParameter<std::string>("label")))),
111  edm::ESInputTag("", iConfig.getParameter<std::string>("label")))),
112  tracksTags_(iConfig.getParameter<std::vector<edm::InputTag>>("tracksTags")),
113  dqmDir_(iConfig.getParameter<std::string>("dqm_dir")),
114  debug_(iConfig.getParameter<bool>("debug")) {
115  edm::LogInfo("PPSAlignmentWorker").log([&](auto& li) {
116  li << "parameters:\n";
117  li << "* label: " << iConfig.getParameter<std::string>("label") << "\n";
118  li << "* tracksTags:\n";
119  for (auto& tag : tracksTags_) {
120  li << " " << tag << ",\n";
121  }
122  li << "* dqm_dir: " << dqmDir_ << "\n";
123  li << "* debug: " << std::boolalpha << debug_;
124  });
125 
126  for (auto& tag : tracksTags_) {
127  tracksTokens_.emplace_back(consumes<CTPPSLocalTrackLiteCollection>(tag));
128  }
129 }
130 
132  const auto& cfg = iSetup.getData(esTokenBookHistograms_);
133 
134  sectorData45_.init(iBooker, cfg, cfg.sectorConfig45(), dqmDir_ + "/worker", debug_);
135  sectorData56_.init(iBooker, cfg, cfg.sectorConfig56(), dqmDir_ + "/worker", debug_);
136 }
137 
140  bool foundProduct = false;
141 
142  for (unsigned int i = 0; i < tracksTokens_.size(); i++) {
143  if (auto handle = iEvent.getHandle(tracksTokens_[i])) {
144  tracks = *handle;
145  foundProduct = true;
146  edm::LogInfo("PPSAlignmentWorker") << "Found a product with " << tracksTags_[i];
147  break;
148  }
149  }
150  if (!foundProduct) {
151  throw edm::Exception(edm::errors::ProductNotFound) << "Could not find a product with any of the selected labels.";
152  }
153 
154  const auto& cfg = iSetup.getData(esTokenAnalyze_);
155 
158 }
159 
162 
163  desc.add<std::string>("label", "");
164  desc.add<std::vector<edm::InputTag>>("tracksTags", {edm::InputTag("ctppsLocalTrackLiteProducer")});
165  desc.add<std::string>("dqm_dir", "AlCaReco/PPSAlignment");
166  desc.add<bool>("debug", false);
167 
168  descriptions.addWithDefaultLabel(desc);
169 }
170 
171 // -------------------------------- SectorData and SlicePlots methods --------------------------------
172 
174 
177  bool debug) {
178  h_y = iBooker.book1DD(
179  "h_y", ";y", cfg.binning().slice_n_bins_x_, cfg.binning().slice_x_min_, cfg.binning().slice_x_max_);
180 
181  auto profilePtr = std::make_unique<TProfile>(
182  "", ";y;y_{F} - y_{N}", cfg.binning().slice_n_bins_x_, cfg.binning().slice_x_min_, cfg.binning().slice_x_max_);
183  p_y_diffFN_vs_y = iBooker.bookProfile("p_y_diffFN_vs_y", profilePtr.get());
184 
185  if (debug)
186  h2_y_diffFN_vs_y = iBooker.book2DD("h2_y_diffFN_vs_y",
187  ";y;y_{F} - y_{N}",
188  cfg.binning().slice_n_bins_x_,
189  cfg.binning().slice_x_min_,
190  cfg.binning().slice_x_max_,
191  cfg.binning().slice_n_bins_y_,
192  cfg.binning().slice_y_min_,
193  cfg.binning().slice_y_max_);
194 }
195 
196 void PPSAlignmentWorker::SectorData::SlicePlots::fill(const double y, const double yDiff, const bool debug) {
197  h_y->Fill(y);
198  p_y_diffFN_vs_y->Fill(y, yDiff);
199  if (debug)
200  h2_y_diffFN_vs_y->Fill(y, yDiff);
201 }
202 
206  const std::string& rootDir,
207  bool debug) {
208  scfg_ = scfg;
209 
210  // binning
211  const double bin_size_x = cfg.binning().bin_size_x_;
212  const unsigned int n_bins_x = cfg.binning().n_bins_x_;
213 
214  const double pixel_x_offset = cfg.binning().pixel_x_offset_;
215 
216  const double x_min_pix = pixel_x_offset, x_max_pix = pixel_x_offset + n_bins_x * bin_size_x;
217  const double x_min_str = 0., x_max_str = n_bins_x * bin_size_x;
218 
219  const unsigned int n_bins_y = cfg.binning().n_bins_y_;
220  const double y_min = cfg.binning().y_min_, y_max = cfg.binning().y_max_;
221 
222  // hit distributions
223  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/before selection/" + scfg_.rp_N_.name_);
225  iBooker.book2DD("h2_y_vs_x", ";x;y", n_bins_x, x_min_str, x_max_str, n_bins_y, y_min, y_max);
226  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/before selection/" + scfg_.rp_F_.name_);
228  iBooker.book2DD("h2_y_vs_x", ";x;y", n_bins_x, x_min_pix, x_max_pix, n_bins_y, y_min, y_max);
229 
230  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/multiplicity selection/" + scfg_.rp_N_.name_);
232  iBooker.book2DD("h2_y_vs_x", ";x;y", n_bins_x, x_min_str, x_max_str, n_bins_y, y_min, y_max);
233  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/multiplicity selection/" + scfg_.rp_F_.name_);
235  iBooker.book2DD("h2_y_vs_x", ";x;y", n_bins_x, x_min_pix, x_max_pix, n_bins_y, y_min, y_max);
236 
237  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/after selection/" + scfg_.rp_N_.name_);
239  iBooker.book2DD("h2_y_vs_x", ";x;y", n_bins_x, x_min_str, x_max_str, n_bins_y, y_min, y_max);
240  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/after selection/" + scfg_.rp_F_.name_);
242  iBooker.book2DD("h2_y_vs_x", ";x;y", n_bins_x, x_min_pix, x_max_pix, n_bins_y, y_min, y_max);
243 
244  // cut plots
245  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/cuts/cut_h");
246  h_q_cut_h_bef = iBooker.book1DD("h_q_cut_h_bef", ";cq_h", 400, -2., 2.);
247  h_q_cut_h_aft = iBooker.book1DD("h_q_cut_h_aft", ";cq_h", 400, -2., 2.);
248  h2_cut_h_bef =
249  iBooker.book2DD("h2_cut_h_bef", ";x_up;x_dw", n_bins_x, x_min_str, x_max_str, n_bins_x, x_min_pix, x_max_pix);
250  h2_cut_h_aft =
251  iBooker.book2DD("h2_cut_h_aft", ";x_up;x_dw", n_bins_x, x_min_str, x_max_str, n_bins_x, x_min_pix, x_max_pix);
252 
253  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/cuts/cut_v");
254  h_q_cut_v_bef = iBooker.book1DD("h_q_cut_v_bef", ";cq_v", 400, -2., 2.);
255  h_q_cut_v_aft = iBooker.book1DD("h_q_cut_v_aft", ";cq_v", 400, -2., 2.);
256  h2_cut_v_bef = iBooker.book2DD("h2_cut_v_bef", ";y_up;y_dw", n_bins_y, y_min, y_max, n_bins_y, y_min, y_max);
257  h2_cut_v_aft = iBooker.book2DD("h2_cut_v_aft", ";y_up;y_dw", n_bins_y, y_min, y_max, n_bins_y, y_min, y_max);
258 
259  // near-far plots
260  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/near_far");
261 
262  auto profilePtr = std::make_unique<TProfile>("",
263  ";x_{N};x_{F} - x_{N}",
264  cfg.binning().diffFN_n_bins_x_,
265  cfg.binning().diffFN_x_min_,
266  cfg.binning().diffFN_x_max_);
267  p_x_diffFN_vs_x_N = iBooker.bookProfile("p_x_diffFN_vs_x_N", profilePtr.get());
268 
269  // slice plots
270  for (int i = 0; i < scfg_.rp_N_.x_slice_n_; i++) {
271  const double x_min = scfg_.rp_N_.x_slice_min_ + i * scfg_.rp_N_.x_slice_w_;
272  const double x_max = scfg_.rp_N_.x_slice_min_ + (i + 1) * scfg_.rp_N_.x_slice_w_;
273 
274  char buf[100];
275  sprintf(buf, "%.1f-%.1f", x_min, x_max);
276  std::replace(buf, buf + strlen(buf), '.', '_'); // replace . with _
277  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/near_far/x slices N/" + buf);
278  x_slice_plots_N.insert({i, SlicePlots(iBooker, cfg, debug)});
279  }
280 
281  for (int i = 0; i < scfg_.rp_F_.x_slice_n_; i++) {
282  const double x_min = scfg_.rp_F_.x_slice_min_ + i * scfg_.rp_F_.x_slice_w_;
283  const double x_max = scfg_.rp_F_.x_slice_min_ + (i + 1) * scfg_.rp_F_.x_slice_w_;
284 
285  char buf[100];
286  sprintf(buf, "%.1f-%.1f", x_min, x_max);
287  std::replace(buf, buf + strlen(buf), '.', '_'); // replace . with _
288  iBooker.setCurrentFolder(rootDir + "/" + scfg_.name_ + "/near_far/x slices F/" + buf);
289  x_slice_plots_F.insert({i, SlicePlots(iBooker, cfg, debug)});
290  }
291 }
292 
295  bool debug) {
296  CTPPSLocalTrackLiteCollection tracksUp, tracksDw;
297 
298  for (const auto& tr : tracks) {
299  CTPPSDetId rpId(tr.rpId());
300  unsigned int rpDecId = rpId.arm() * 100 + rpId.station() * 10 + rpId.rp();
301 
302  if (rpDecId != scfg_.rp_N_.id_ && rpDecId != scfg_.rp_F_.id_)
303  continue;
304 
305  // store the track in the right collection
306  if (rpDecId == scfg_.rp_N_.id_)
307  tracksUp.push_back(tr);
308  if (rpDecId == scfg_.rp_F_.id_)
309  tracksDw.push_back(tr);
310  }
311 
312  // update plots before selection
313  for (const auto& tr : tracksUp)
314  m_h2_y_vs_x_bef_sel[scfg_.rp_N_.id_]->Fill(tr.x(), tr.y());
315 
316  for (const auto& tr : tracksDw)
317  m_h2_y_vs_x_bef_sel[scfg_.rp_F_.id_]->Fill(tr.x(), tr.y());
318 
319  // skip crowded events (multiplicity selection)
320  if (tracksUp.size() < cfg.minRPTracksSize() || tracksUp.size() > cfg.maxRPTracksSize())
321  return 0;
322 
323  if (tracksDw.size() < cfg.minRPTracksSize() || tracksDw.size() > cfg.maxRPTracksSize())
324  return 0;
325 
326  // update plots with multiplicity selection
327  for (const auto& tr : tracksUp)
328  m_h2_y_vs_x_mlt_sel[scfg_.rp_N_.id_]->Fill(tr.x(), tr.y());
329 
330  for (const auto& tr : tracksDw)
331  m_h2_y_vs_x_mlt_sel[scfg_.rp_F_.id_]->Fill(tr.x(), tr.y());
332 
333  // do the selection
334  unsigned int pairsSelected = 0;
335  for (const auto& trUp : tracksUp) {
336  for (const auto& trDw : tracksDw) {
337  h2_cut_h_bef->Fill(trUp.x(), trDw.x());
338  h2_cut_v_bef->Fill(trUp.y(), trDw.y());
339 
340  // horizontal cut
341  const double cq_h = trDw.x() + scfg_.cut_h_a_ * trUp.x() + scfg_.cut_h_c_;
342  h_q_cut_h_bef->Fill(cq_h);
343  const bool cv_h = (std::fabs(cq_h) < cfg.n_si() * scfg_.cut_h_si_);
344 
345  // vertical cut
346  const double cq_v = trDw.y() + scfg_.cut_v_a_ * trUp.y() + scfg_.cut_v_c_;
347  h_q_cut_v_bef->Fill(cq_v);
348  const bool cv_v = (std::fabs(cq_v) < cfg.n_si() * scfg_.cut_v_si_);
349 
350  bool cutsPassed = true;
351  if (scfg_.cut_h_apply_)
352  cutsPassed &= cv_h;
353  if (scfg_.cut_v_apply_)
354  cutsPassed &= cv_v;
355 
356  if (cutsPassed) {
357  pairsSelected++;
358 
359  h_q_cut_h_aft->Fill(cq_h);
360  h_q_cut_v_aft->Fill(cq_v);
361 
362  h2_cut_h_aft->Fill(trUp.x(), trDw.x());
363  h2_cut_v_aft->Fill(trUp.y(), trDw.y());
364 
365  m_h2_y_vs_x_aft_sel[scfg_.rp_N_.id_]->Fill(trUp.x(), trUp.y());
366  m_h2_y_vs_x_aft_sel[scfg_.rp_F_.id_]->Fill(trDw.x(), trDw.y());
367 
368  p_x_diffFN_vs_x_N->Fill(trUp.x(), trDw.x() - trUp.x());
369 
370  int idx = (trUp.x() - scfg_.rp_N_.x_slice_min_) / scfg_.rp_N_.x_slice_w_;
371  if (idx >= 0 && idx < scfg_.rp_N_.x_slice_n_) {
372  x_slice_plots_N[idx].fill(trUp.y(), trDw.y() - trUp.y(), debug);
373  }
374 
375  idx = (trDw.x() - scfg_.rp_F_.x_slice_min_) / scfg_.rp_F_.x_slice_w_;
376  if (idx >= 0 && idx < scfg_.rp_F_.x_slice_n_) {
377  x_slice_plots_F[idx].fill(trDw.y(), trDw.y() - trUp.y(), debug);
378  }
379  }
380  }
381  }
382 
383  return pairsSelected;
384 }
385 
std::map< unsigned int, SlicePlots > x_slice_plots_F
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::map< unsigned int, MonitorElement * > m_h2_y_vs_x_bef_sel
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void bookHistograms(DQMStore::IBooker &iBooker, edm::Run const &, edm::EventSetup const &iSetup) override
unsigned int process(const CTPPSLocalTrackLiteCollection &tracks, const PPSAlignmentConfiguration &cfg, bool debug)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
uint32_t arm() const
Definition: CTPPSDetId.h:57
def replace(string, replacements)
const std::vector< edm::InputTag > tracksTags_
std::map< unsigned int, MonitorElement * > m_h2_y_vs_x_aft_sel
void Fill(long long x)
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
MonitorElement * book1DD(TString const &name, TString const &title, int nchX, double lowX, double highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:155
int iEvent
Definition: GenABIO.cc:224
const edm::ESGetToken< PPSAlignmentConfiguration, PPSAlignmentConfigurationRcd > esTokenAnalyze_
MonitorElement * bookProfile(TString const &name, TString const &title, int nchX, double lowX, double highX, int, double lowY, double highY, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:399
const std::string dqmDir_
PPSAlignmentWorker(const edm::ParameterSet &iConfig)
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
PPSAlignmentConfiguration::SectorConfig scfg_
void init(DQMStore::IBooker &iBooker, const PPSAlignmentConfiguration &cfg, const PPSAlignmentConfiguration::SectorConfig &scfg, const std::string &rootDir, bool debug)
Log< level::Info, false > LogInfo
void fill(const double y, const double yDiff, const bool debug)
#define debug
Definition: HDRShower.cc:19
std::vector< CTPPSLocalTrackLite > CTPPSLocalTrackLiteCollection
Collection of CTPPSLocalTrackLite objects.
const edm::ESGetToken< PPSAlignmentConfiguration, PPSAlignmentConfigurationRcd > esTokenBookHistograms_
MonitorElement * book2DD(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:338
Base class for CTPPS detector IDs.
Definition: CTPPSDetId.h:32
HLT enums.
std::map< unsigned int, SlicePlots > x_slice_plots_N
std::map< unsigned int, MonitorElement * > m_h2_y_vs_x_mlt_sel
std::vector< edm::EDGetTokenT< CTPPSLocalTrackLiteCollection > > tracksTokens_
if(threadIdxLocalY==0 &&threadIdxLocalX==0)
Definition: Run.h:45