CMS 3D CMS Logo

PixelClusterShapeExtractor.cc
Go to the documentation of this file.
1 // VI January 2012: needs to be migrated to use cluster directly
2 
10 
13 
16 
19 
22 
25 #
26 
27 #include "TROOT.h"
28 #include "TFile.h"
29 #include "TH1F.h"
30 #include "TH2F.h"
31 
32 #include <map>
33 #include <memory>
34 
35 #include <fstream>
36 #include <vector>
37 
38 #include <mutex>
39 
40 using namespace std;
41 
42 // pixel
43 #define exMax 10
44 #define eyMax 15
45 
46 namespace {
47 
48  const std::set<unsigned> RelevantProcesses = {0};
49  //const std::set<unsigned> RelevantProcesses = { 2, 7, 9, 11, 13, 15 };
50 
51 } // namespace
52 
53 /*****************************************************************************/
55 public:
57  void analyze(edm::StreamID, const edm::Event& evt, const edm::EventSetup&) const override;
58  void endJob() override;
59 
60 private:
61  void init();
62 
63  bool isSuitable(const PSimHit& simHit, const GeomDetUnit& gdu) const;
64 
65  // Sim
66  void processSim(const SiPixelRecHit& recHit,
67  ClusterShapeHitFilter const& theClusterFilter,
68  const PSimHit& simHit,
70  const vector<TH2F*>& histo) const;
71 
72  // Rec
73  void processRec(const SiPixelRecHit& recHit,
74  ClusterShapeHitFilter const& theFilter,
75  LocalVector ldir,
77  const vector<TH2F*>& histo) const;
78 
79  bool checkSimHits(const TrackingRecHit& recHit,
80  TrackerHitAssociator const& theAssociator,
81  PSimHit& simHit,
82  pair<unsigned int, float>& key,
83  unsigned int& ss) const;
84 
85  void processPixelRecHits(SiPixelRecHitCollection::DataContainer const& recHits,
86  TrackerHitAssociator const& theAssociator,
87  ClusterShapeHitFilter const& theFilter,
89  const TrackerTopology& tkTpl) const;
90 
91  void analyzeSimHits(const edm::Event& ev, const edm::EventSetup& es) const;
92  void analyzeRecTracks(const edm::Event& ev, const edm::EventSetup& es) const;
93 
97 
98  TFile* file;
99 
100  const bool hasSimHits;
101  const bool hasRecTracks;
102  const bool noBPIX1;
103 
108 
109  using Lock = std::unique_lock<std::mutex>;
110  std::unique_ptr<std::mutex[]> theMutex;
111  std::vector<TH2F*> hspc; // simulated pixel cluster
112  std::vector<TH2F*> hrpc; // reconstructed pixel cluster
113 };
114 
115 /*****************************************************************************/
117  // Declare histograms
118  char histName[256];
119 
120  // pixel
121  for (int subdet = 0; subdet <= 1; subdet++) {
122  for (int ex = 0; ex <= exMax; ex++)
123  for (int ey = 0; ey <= eyMax; ey++) {
124  sprintf(histName, "hspc_%d_%d_%d", subdet, ex, ey);
125  hspc.push_back(new TH2F(histName,
126  histName,
127  10 * 2 * (exMax + 2),
128  -(exMax + 2),
129  (exMax + 2),
130  10 * 2 * (eyMax + 2),
131  -(eyMax + 2),
132  (eyMax + 2)));
133 
134  sprintf(histName, "hrpc_%d_%d_%d", subdet, ex, ey);
135  hrpc.push_back(new TH2F(histName,
136  histName,
137  10 * 2 * (exMax + 2),
138  -(exMax + 2),
139  (exMax + 2),
140  10 * 2 * (eyMax + 2),
141  -(eyMax + 2),
142  (eyMax + 2)));
143  }
144  }
145  theMutex = std::make_unique<std::mutex[]>(hspc.size());
146 }
147 
148 /*****************************************************************************/
150  : topoToken_(esConsumes()),
151  csfToken_(esConsumes(edm::ESInputTag("", "ClusterShapeHitFilter"))),
152  hasSimHits(pset.getParameter<bool>("hasSimHits")),
153  hasRecTracks(pset.getParameter<bool>("hasRecTracks")),
154  noBPIX1(pset.getParameter<bool>("noBPIX1")),
155  tracks_token(hasRecTracks ? consumes<reco::TrackCollection>(pset.getParameter<edm::InputTag>("tracks"))
156  : edm::EDGetTokenT<reco::TrackCollection>()),
157  pixelRecHits_token(consumes<edmNew::DetSetVector<SiPixelRecHit>>(edm::InputTag("siPixelRecHits"))),
158  clusterShapeCache_token(
159  consumes<SiPixelClusterShapeCache>(pset.getParameter<edm::InputTag>("clusterShapeCacheSrc"))),
160  trackerHitAssociatorConfig_(pset, consumesCollector()) {
161  file = new TFile("clusterShape.root", "RECREATE");
162  file->cd();
163  init();
164 }
165 
166 /*****************************************************************************/
168  file->cd();
169 
170  // simulated
171  for (auto h = hspc.begin(); h != hspc.end(); h++)
172  (*h)->Write();
173 
174  // reconstructed
175  for (auto h = hrpc.begin(); h != hrpc.end(); h++)
176  (*h)->Write();
177 
178  file->Close();
179 }
180 
181 /*****************************************************************************/
183  // Outgoing?
184  // very expensive....
185  GlobalVector gvec = gdu.position() - GlobalPoint(0, 0, 0);
186  LocalVector lvec = gdu.toLocal(gvec);
187  LocalVector ldir = simHit.exitPoint() - simHit.entryPoint();
188 
189  // cut on size as well (pixel is 285um thick...
190  bool isOutgoing = std::abs(ldir.z()) > 0.01f && (lvec.z() * ldir.z() > 0);
191 
193  const bool isRelevant = RelevantProcesses.count(simHit.processType());
194  // From a relevant process? primary or decay
195  //bool isRelevant = (simHit.processType() == 2 ||
196  // simHit.processType() == 4);
197 
198  constexpr float ptCut2 = 0.2 * 0.2; // 0.050*0.050;
199  // Fast enough? pt > 50 MeV/c FIXME (at least 200MeV....
200  bool isFast = (simHit.momentumAtEntry().perp2() > ptCut2);
201 
202  //std::cout << "isOutgoing = " << isOutgoing << ", isRelevant = " << simHit.processType() << ", isFast = " << isFast << std::endl;
203  return (isOutgoing && isRelevant && isFast);
204 }
205 
206 /*****************************************************************************/
208  ClusterShapeHitFilter const& theClusterShape,
209  LocalVector ldir,
211  const vector<TH2F*>& histo) const {
212  int part;
214  pair<float, float> pred;
215 
216  if (theClusterShape.getSizes(recHit, ldir, clusterShapeCache, part, meas, pred))
217  if (meas.size() == 1)
218  if (meas.front().first <= exMax && meas.front().second <= eyMax) {
219  int i = (part * (exMax + 1) + meas.front().first) * (eyMax + 1) + meas.front().second;
220 #ifdef DO_DEBUG
221  if (meas.front().second == 0 && std::abs(pred.second) > 3) {
222  Lock lock(theMutex[0]);
223  int id = recHit.geographicalId();
224  std::cout << id << " bigpred " << meas.front().first << '/' << meas.front().second << ' ' << pred.first << '/'
225  << pred.second << ' ' << ldir << ' ' << ldir.mag() << std::endl;
226  }
227 #endif
228  Lock lock(theMutex[i]);
229  histo[i]->Fill(pred.first, pred.second);
230  }
231 }
232 
233 /*****************************************************************************/
235  ClusterShapeHitFilter const& theClusterFilter,
236  const PSimHit& simHit,
238  const vector<TH2F*>& histo) const {
239  LocalVector ldir = simHit.exitPoint() - simHit.entryPoint();
240  processRec(recHit, theClusterFilter, ldir, clusterShapeCache, histo);
241 }
242 
243 /*****************************************************************************/
245  TrackerHitAssociator const& theHitAssociator,
246  PSimHit& simHit,
247  pair<unsigned int, float>& key,
248  unsigned int& ss) const {
249  auto const& simHits = theHitAssociator.associateHit(recHit);
250 
251  //std::cout << "simHits.size() = " << simHits.size() << std::endl;
252  for (auto const& sh : simHits) {
253  if (isSuitable(sh, *recHit.detUnit())) {
254  simHit = sh;
255  key = {simHit.trackId(), simHit.timeOfFlight()};
256  ss = simHits.size();
257  return true;
258  }
259  }
260 
261  return false;
262 }
263 
264 /*****************************************************************************/
266  TrackerHitAssociator const& theHitAssociator,
267  ClusterShapeHitFilter const& theFilter,
269  const TrackerTopology& tkTpl) const {
270  struct Elem {
271  const SiPixelRecHit* rhit;
272  PSimHit shit;
273  unsigned int size;
274  };
275  std::map<pair<unsigned int, float>, Elem> simHitMap;
276 
277  PSimHit simHit;
278  pair<unsigned int, float> key;
279  unsigned int ss;
280 
281  for (auto const& recHit : recHits) {
282  if (noBPIX1 && tkTpl.pxbLayer(recHit.geographicalId()) == 1)
283  continue;
284  if (!checkSimHits(recHit, theHitAssociator, simHit, key, ss))
285  continue;
286  // Fill map
287  if (simHitMap.count(key) == 0) {
288  simHitMap[key] = {&recHit, simHit, ss};
289  } else if (recHit.cluster()->size() > simHitMap[key].rhit->cluster()->size())
290  simHitMap[key] = {&recHit, simHit, std::max(ss, simHitMap[key].size)};
291  }
292  for (auto const& elem : simHitMap) {
293  /* irrelevant
294  auto const rh = *elem.second.rhit;
295  auto const& topol = reinterpret_cast<const PixelGeomDetUnit*>(rh.detUnit())->specificTopology();
296  auto const & cl = *rh.cluster();
297  if (cl.minPixelCol()==0) continue;
298  if (cl.maxPixelCol()+1==topol.ncolumns()) continue;
299  if (cl.minPixelRow()==0) continue;
300  if (cl.maxPixelRow()+1==topol.nrows()) continue;
301  */
302  if (elem.second.size == 1)
303  processSim(*elem.second.rhit, theFilter, elem.second.shit, clusterShapeCache, hspc);
304  }
305 }
306 
307 /*****************************************************************************/
309  auto const& theClusterShape = es.getData(csfToken_);
310  auto const& tkTpl = es.getData(topoToken_);
311 
314 
315  // Get associator
316  auto theHitAssociator = std::make_unique<TrackerHitAssociator>(ev, trackerHitAssociatorConfig_);
317 
318  // Pixel hits
319  {
321  ev.getByToken(pixelRecHits_token, coll);
322 
325 
326  auto const& recHits = coll.product()->data();
327  processPixelRecHits(recHits, *theHitAssociator, theClusterShape, *clusterShapeCache, tkTpl);
328  }
329 }
330 
331 /*****************************************************************************/
333  auto const& theClusterShape = es.getData(csfToken_);
334  auto const& tkTpl = es.getData(topoToken_);
335 
336  // Get tracks
338  ev.getByToken(tracks_token, tracks);
339 
342 
343  for (auto const& track : *tracks) {
344  if (!track.quality(reco::Track::highPurity))
345  continue;
346  if (track.numberOfValidHits() < 8)
347  continue;
348  auto const& trajParams = track.extra()->trajParams();
349  assert(trajParams.size() == track.recHitsSize());
350  auto hb = track.recHitsBegin();
351  for (unsigned int h = 0; h < track.recHitsSize(); h++) {
352  auto recHit = *(hb + h);
353  if (!recHit->isValid())
354  continue;
355  auto id = recHit->geographicalId();
356  if (noBPIX1 && tkTpl.pxbLayer(id) == 1)
357  continue;
358 
359  // check that we are in the pixel
360  auto subdetid = (id.subdetId());
361  bool isPixel = subdetid == PixelSubdetector::PixelBarrel || subdetid == PixelSubdetector::PixelEndcap;
362 
363  auto const& ltp = trajParams[h];
364  auto ldir = ltp.momentum() / ltp.momentum().mag();
365 
366  if (isPixel) {
367  // Pixel
368  const SiPixelRecHit* pixelRecHit = dynamic_cast<const SiPixelRecHit*>(recHit);
369 
370  if (pixelRecHit != nullptr)
371  processRec(*pixelRecHit, theClusterShape, ldir, *clusterShapeCache, hrpc);
372  }
373  }
374  }
375 }
376 
377 /*****************************************************************************/
379  if (hasSimHits) {
380  LogTrace("MinBiasTracking") << " [ClusterShape] analyze simHits, recHits";
381  analyzeSimHits(ev, es);
382  }
383 
384  if (hasRecTracks) {
385  LogTrace("MinBiasTracking") << " [ClusterShape] analyze recHits on recTracks";
386  analyzeRecTracks(ev, es);
387  }
388 }
389 
size
Write out results.
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
unsigned int pxbLayer(const DetId &id) const
PixelClusterShapeExtractor(const edm::ParameterSet &pset)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
#define eyMax
std::vector< data_type > DataContainer
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:58
T z() const
Definition: PV3DBase.h:61
const edm::EDGetTokenT< SiPixelClusterShapeCache > clusterShapeCache_token
T const * product() const
Definition: Handle.h:70
int init
Definition: HydjetWrapper.h:66
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
data_type const * data(size_t cell) const
void processPixelRecHits(SiPixelRecHitCollection::DataContainer const &recHits, TrackerHitAssociator const &theAssociator, ClusterShapeHitFilter const &theFilter, SiPixelClusterShapeCache const &clusterShapeCache, const TrackerTopology &tkTpl) const
assert(be >=bs)
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
#define LogTrace(id)
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > topoToken_
Tokens for ESconsumes.
std::vector< PSimHit > associateHit(const TrackingRecHit &thit) const
const TrackerHitAssociator::Config trackerHitAssociatorConfig_
const edm::EDGetTokenT< edmNew::DetSetVector< SiPixelRecHit > > pixelRecHits_token
void analyzeSimHits(const edm::Event &ev, const edm::EventSetup &es) const
T mag() const
Definition: PV3DBase.h:64
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
reference front()
Definition: VecArray.h:50
double f[11][100]
void analyzeRecTracks(const edm::Event &ev, const edm::EventSetup &es) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::ESGetToken< ClusterShapeHitFilter, CkfComponentsRecord > csfToken_
bool isFast(TrackingRecHit const &hit)
bool isSuitable(const PSimHit &simHit, const GeomDetUnit &gdu) const
bool getSizes(const SiPixelRecHit &recHit, const LocalVector &ldir, const SiPixelClusterShapeCache &clusterShapeCache, int &part, ClusterData::ArrayType &meas, std::pair< float, float > &predr, PixelData const *pd=nullptr) const
part
Definition: HCALResponse.h:20
const edm::EDGetTokenT< reco::TrackCollection > tracks_token
void analyze(edm::StreamID, const edm::Event &evt, const edm::EventSetup &) const override
void processRec(const SiPixelRecHit &recHit, ClusterShapeHitFilter const &theFilter, LocalVector ldir, const SiPixelClusterShapeCache &clusterShapeCache, const vector< TH2F *> &histo) const
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:43
fixed size matrix
HLT enums.
std::unique_lock< std::mutex > Lock
bool isPixel(HitType hitType)
bool checkSimHits(const TrackingRecHit &recHit, TrackerHitAssociator const &theAssociator, PSimHit &simHit, pair< unsigned int, float > &key, unsigned int &ss) const
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
constexpr size_type size() const noexcept
Definition: VecArray.h:70
#define exMax
void processSim(const SiPixelRecHit &recHit, ClusterShapeHitFilter const &theClusterFilter, const PSimHit &simHit, const SiPixelClusterShapeCache &clusterShapeCache, const vector< TH2F *> &histo) const
Our base class.
Definition: SiPixelRecHit.h:23
std::unique_ptr< std::mutex[]> theMutex