CMS 3D CMS Logo

HIPixelMedianVtxProducer.cc
Go to the documentation of this file.
2 
6 
9 
10 #include <fstream>
11 #include <iostream>
12 #include <vector>
13 #include <algorithm>
14 
15 #include "TROOT.h"
16 #include "TH1F.h"
17 #include "TF1.h"
18 #include "TMinuitMinimizer.h"
19 
20 /*****************************************************************************/
22  : theTrackCollection(consumes<reco::TrackCollection>(ps.getParameter<edm::InputTag>("TrackCollection"))),
23  thePtMin(ps.getParameter<double>("PtMin")),
24  thePeakFindThresh(ps.getParameter<unsigned int>("PeakFindThreshold")),
25  thePeakFindMaxZ(ps.getParameter<double>("PeakFindMaxZ")),
26  thePeakFindBinning(ps.getParameter<int>("PeakFindBinsPerCm")),
27  theFitThreshold(ps.getParameter<int>("FitThreshold")),
28  theFitMaxZ(ps.getParameter<double>("FitMaxZ")),
29  theFitBinning(ps.getParameter<int>("FitBinsPerCm")) {
30  produces<reco::VertexCollection>();
31 
32  //In order to make fitting ROOT histograms thread safe
33  // one must call this undocumented function
34  TMinuitMinimizer::UseStaticMinuit(false);
35 }
36 
37 /*****************************************************************************/
39  // Get pixel tracks
42  const reco::TrackCollection tracks_ = *(trackCollection.product());
43 
44  // Select tracks above minimum pt
45  std::vector<const reco::Track*> tracks;
46  for (unsigned int i = 0; i < tracks_.size(); i++) {
47  if (tracks_[i].pt() < thePtMin && std::fabs(tracks_[i].vz()) < 100000.)
48  continue;
49  reco::TrackRef recTrack(trackCollection, i);
50  tracks.push_back(&(*recTrack));
51  }
52 
53  LogTrace("MinBiasTracking") << " [VertexProducer] selected tracks: " << tracks.size() << " (out of " << tracks_.size()
54  << ") above pt = " << thePtMin;
55 
56  // Output vertex collection
57  auto vertices = std::make_unique<reco::VertexCollection>();
58 
59  // No tracks -> return empty collection
60  if (tracks.empty()) {
61  ev.put(std::move(vertices));
62  return;
63  }
64 
65  // Sort tracks according to vertex z position
66  std::sort(tracks.begin(), tracks.end(), ComparePairs());
67 
68  // Calculate median vz
69  float med;
70  if (tracks.size() % 2 == 0)
71  med = (tracks[tracks.size() / 2 - 1]->vz() + tracks[tracks.size() / 2]->vz()) / 2;
72  else
73  med = tracks[tracks.size() / 2]->vz();
74 
75  LogTrace("MinBiasTracking") << " [vertex position] median = " << med << " cm";
76 
77  // In high multiplicity events, fit around most probable position
78  if (tracks.size() > thePeakFindThresh) {
79  // Find maximum bin
80  TH1F hmax("hmax", "hmax", thePeakFindBinning * 2.0 * thePeakFindMaxZ, -1.0 * thePeakFindMaxZ, thePeakFindMaxZ);
81 
82  for (std::vector<const reco::Track*>::const_iterator track = tracks.begin(); track != tracks.end(); track++)
83  if (fabs((*track)->vz()) < thePeakFindMaxZ)
84  hmax.Fill((*track)->vz());
85 
86  int maxBin = hmax.GetMaximumBin();
87 
88  LogTrace("MinBiasTracking") << " [vertex position] most prob = " << hmax.GetBinCenter(maxBin) << " cm";
89 
90  // Find 3-bin weighted average
91  float num = 0.0, denom = 0.0;
92  for (int i = -1; i <= 1; i++) {
93  num += hmax.GetBinContent(maxBin + i) * hmax.GetBinCenter(maxBin + i);
94  denom += hmax.GetBinContent(maxBin + i);
95  }
96 
97  if (denom == 0) {
99  err(2, 2) = 0.1 * 0.1;
100  reco::Vertex ver(reco::Vertex::Point(0, 0, 99999.), err, 0, 0, 1);
101  vertices->push_back(ver);
102  ev.put(std::move(vertices));
103  return;
104  }
105 
106  float nBinAvg = num / denom;
107 
108  // Center fit at 3-bin weighted average around max bin
109  med = nBinAvg;
110 
111  LogTrace("MinBiasTracking") << " [vertex position] 3-bin weighted average = " << nBinAvg << " cm";
112  }
113 
114  // Bin vz-values around most probable value (or median) for fitting
115  TH1F histo("histo", "histo", theFitBinning * 2.0 * theFitMaxZ, -1.0 * theFitMaxZ, theFitMaxZ);
116  histo.Sumw2();
117 
118  for (std::vector<const reco::Track*>::const_iterator track = tracks.begin(); track != tracks.end(); track++)
119  if (fabs((*track)->vz() - med) < theFitMaxZ)
120  histo.Fill((*track)->vz() - med);
121 
122  LogTrace("MinBiasTracking") << " [vertex position] most prob for fit = "
123  << med + histo.GetBinCenter(histo.GetMaximumBin()) << " cm";
124 
125  // If there are very few entries, don't do the fit
126  if (histo.GetEntries() <= theFitThreshold) {
127  LogTrace("MinBiasTracking") << " [vertex position] Fewer than" << theFitThreshold
128  << " entries in fit histogram. Returning median.";
129 
131  err(2, 2) = 0.1 * 0.1;
132  reco::Vertex ver(reco::Vertex::Point(0, 0, med), err, 0, 1, 1);
133  vertices->push_back(ver);
134  ev.put(std::move(vertices));
135  return;
136  }
137 
138  // Otherwise, there are enough entries to refine the estimate with a fit
139  TF1 f1("f1", "[0]*exp(-0.5 * ((x-[1])/[2])^2) + [3]");
140  f1.SetParameters(10., 0., 0.02, 0.002 * tracks.size());
141  f1.SetParLimits(1, -0.1, 0.1);
142  f1.SetParLimits(2, 0.001, 0.05);
143  f1.SetParLimits(3, 0.0, 0.005 * tracks.size());
144 
145  histo.Fit(&f1, "QN SERIAL");
146 
147  LogTrace("MinBiasTracking") << " [vertex position] fitted = " << med + f1.GetParameter(1) << " +- "
148  << f1.GetParError(1) << " cm";
149 
151  err(2, 2) = f1.GetParError(1) * f1.GetParError(1);
152  reco::Vertex ver(reco::Vertex::Point(0, 0, med + f1.GetParameter(1)), err, 0, 1, 1);
153  vertices->push_back(ver);
154 
155  ev.put(std::move(vertices));
156  return;
157 }
constexpr unsigned int maxBin
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
math::Error< dimension >::type Error
covariance error matrix (3x3)
Definition: Vertex.h:45
#define LogTrace(id)
math::XYZPoint Point
point in the space
Definition: Vertex.h:40
trackCollection
Definition: JetHT_cfg.py:51
void produce(edm::Event &ev, const edm::EventSetup &es) override
HIPixelMedianVtxProducer(const edm::ParameterSet &ps)
fixed size matrix
HLT enums.
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< reco::TrackCollection > theTrackCollection