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