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