CMS 3D CMS Logo

SeedingLayerSetsBuilder.cc
Go to the documentation of this file.
10 
14 
17 
19 
20 #include "HitExtractorPIX.h"
21 #include "HitExtractorSTRP.h"
22 
23 #include <iostream>
24 #include <sstream>
25 #include <ostream>
26 #include <fstream>
27 #include <map>
28 
29 
30 using namespace ctfseeding;
31 using namespace std;
32 
36  int idLayer = 0;
37 
38  size_t index;
39  //
40  // BPIX
41  //
42  if ((index = name.find("BPix")) != string::npos) {
44  side = SeedingLayer::Barrel;
45  idLayer = atoi(name.substr(index+4,1).c_str());
46  }
47  //
48  // FPIX
49  //
50  else if ((index = name.find("FPix")) != string::npos) {
52  idLayer = atoi(name.substr(index+4).c_str());
53  if ( name.find("pos") != string::npos ) {
55  } else {
57  }
58  }
59  //
60  // TIB
61  //
62  else if ((index = name.find("TIB")) != string::npos) {
63  subdet = GeomDetEnumerators::TIB;
64  side = SeedingLayer::Barrel;
65  idLayer = atoi(name.substr(index+3,1).c_str());
66  }
67  //
68  // TID
69  //
70  else if ((index = name.find("TID")) != string::npos) {
71  subdet = GeomDetEnumerators::TID;
72  idLayer = atoi(name.substr(index+3,1).c_str());
73  if ( name.find("pos") != string::npos ) {
75  } else {
77  }
78  }
79  //
80  // TOB
81  //
82  else if ((index = name.find("TOB")) != string::npos) {
83  subdet = GeomDetEnumerators::TOB;
84  side = SeedingLayer::Barrel;
85  idLayer = atoi(name.substr(index+3,1).c_str());
86  }
87  //
88  // TEC
89  //
90  else if ((index = name.find("TEC")) != string::npos) {
91  subdet = GeomDetEnumerators::TEC;
92  idLayer = atoi(name.substr(index+3,1).c_str());
93  if ( name.find("pos") != string::npos ) {
95  } else {
97  }
98  }
99  return std::make_tuple(subdet, side, idLayer);
100 }
101 
103  nameIndex(index),
104  hitBuilder(cfgLayer.getParameter<string>("TTRHBuilder"))
105 {
106  usePixelHitProducer = false;
107  if (cfgLayer.exists("HitProducer")) {
108  pixelHitProducer = cfgLayer.getParameter<string>("HitProducer");
109  usePixelHitProducer = true;
110  }
111 
112  bool skipClusters = cfgLayer.exists("skipClusters");
113  if (skipClusters) {
114  LogDebug("SeedingLayerSetsBuilder")<<layerName<<" ready for skipping";
115  }
116  else{
117  LogDebug("SeedingLayerSetsBuilder")<<layerName<<" not skipping ";
118  }
119 
120  auto subdetData = nameToEnumId(layerName);
121  subdet = std::get<0>(subdetData);
122  side = std::get<1>(subdetData);
123  idLayer = std::get<2>(subdetData);
126  extractor = std::make_shared<HitExtractorPIX>(side, idLayer, pixelHitProducer, iC);
127  }
128  else if(subdet != GeomDetEnumerators::invalidDet) { // strip
129  std::shared_ptr<HitExtractorSTRP> extr = std::make_shared<HitExtractorSTRP>(subdet, side, idLayer, clusterChargeCut(cfgLayer) );
130  if (cfgLayer.exists("matchedRecHits")) {
131  extr->useMatchedHits(cfgLayer.getParameter<edm::InputTag>("matchedRecHits"), iC);
132  }
133  if (cfgLayer.exists("rphiRecHits")) {
134  extr->useRPhiHits(cfgLayer.getParameter<edm::InputTag>("rphiRecHits"), iC);
135  }
136  if (cfgLayer.exists("stereoRecHits")) {
137  extr->useStereoHits(cfgLayer.getParameter<edm::InputTag>("stereoRecHits"), iC);
138  }
139  if (cfgLayer.exists("useRingSlector") && cfgLayer.getParameter<bool>("useRingSlector")) {
140  extr->useRingSelector(cfgLayer.getParameter<int>("minRing"),
141  cfgLayer.getParameter<int>("maxRing"));
142  }
143  bool useSimpleRphiHitsCleaner = cfgLayer.exists("useSimpleRphiHitsCleaner") ? cfgLayer.getParameter<bool>("useSimpleRphiHitsCleaner") : true;
144  extr->useSimpleRphiHitsCleaner(useSimpleRphiHitsCleaner);
145 
146  double minAbsZ = cfgLayer.exists("MinAbsZ") ? cfgLayer.getParameter<double>("MinAbsZ") : 0.;
147  if(minAbsZ > 0.) {
148  extr->setMinAbsZ(minAbsZ);
149  }
150  if(skipClusters) {
151  bool useProjection = cfgLayer.exists("useProjection") ? cfgLayer.getParameter<bool>("useProjection") : false;
152  if(useProjection) {
153  LogDebug("SeedingLayerSetsBuilder")<<layerName<<" will project partially masked matched rechit";
154  }
155  else {
156  extr->setNoProjection();
157  }
158  }
159  extractor = std::move(extr);
160  }
161  if(extractor && skipClusters) {
162  extractor->useSkipClusters(cfgLayer.getParameter<edm::InputTag>("skipClusters"), iC);
163  }
164 }
166 
167 std::string SeedingLayerSetsBuilder::LayerSpec::print(const std::vector<std::string>& names) const
168 {
169  std::ostringstream str;
170  str << "Layer="<<names[nameIndex]<<", hitBldr: "<<hitBuilder;
171 
172  str << ", useRingSelector: ";
173  HitExtractorSTRP *ext = nullptr;
174  if((ext = dynamic_cast<HitExtractorSTRP *>(extractor.get())) &&
175  ext->useRingSelector()) {
176  auto minMaxRing = ext->getMinMaxRing();
177  str <<"true,"<<" Rings: ("<< std::get<0>(minMaxRing) <<","<< std::get<1>(minMaxRing) <<")";
178  } else str<<"false";
179 
180  return str.str();
181 }
182 
183 //FastSim specific constructor
185  SeedingLayerSetsBuilder(cfg, iC)
186 {
188 }
189 
191  SeedingLayerSetsBuilder(cfg, iC)
192 {}
194 {
195  std::vector<std::string> namesPset = cfg.getParameter<std::vector<std::string> >("layerList");
196  std::vector<std::vector<std::string> > layerNamesInSets = this->layerNamesInSets(namesPset);
197  // debug printout of layers
198  typedef std::vector<std::string>::const_iterator IS;
199  typedef std::vector<std::vector<std::string> >::const_iterator IT;
200  std::ostringstream str;
201  // The following should not be set to cout
202 // for (IT it = layerNamesInSets.begin(); it != layerNamesInSets.end(); it++) {
203 // str << "SET: ";
204 // for (IS is = it->begin(); is != it->end(); is++) str << *is <<" ";
205 // str << std::endl;
206 // }
207 // std::cout << str.str() << std::endl;
208  if(layerNamesInSets.empty())
210  else
211  theNumberOfLayersInSet = layerNamesInSets[0].size();
212 
213 
214  for (IT it = layerNamesInSets.begin(); it != layerNamesInSets.end(); it++) {
215  if(it->size() != theNumberOfLayersInSet)
216  throw cms::Exception("Configuration") << "Assuming all SeedingLayerSets to have same number of layers. LayerSet " << (it-layerNamesInSets.begin()) << " has " << it->size() << " while 0th has " << theNumberOfLayersInSet;
217  for(const std::string& layerName: *it) {
218  auto found = std::find(theLayerNames.begin(), theLayerNames.end(), layerName);
219  unsigned short layerIndex = 0;
220  if(found != theLayerNames.end()) {
221  layerIndex = found-theLayerNames.begin();
222  }
223  else {
225  throw cms::Exception("Assert") << "Too many layers in " << __FILE__ << ":" << __LINE__ << ", we may have to enlarge the index type from unsigned short to unsigned int";
226  }
227 
228  layerIndex = theLayers.size();
229  theLayers.emplace_back(theLayerNames.size(), layerName, layerConfig(layerName, cfg), iC);
230  theLayerNames.push_back(layerName);
231  }
232  theLayerSetIndices.push_back(layerIndex);
233  }
234  }
235  theLayerDets.resize(theLayers.size());
236  theTTRHBuilders.resize(theLayers.size());
237 
238  // debug printout
239  // The following should not be set to cout
240  //for(const LayerSpec& layer: theLayers) {
241  // std::cout << layer.print(theLayerNames) << std::endl;
242  //}
243 }
244 
246 
248 {
250 
251  for (string::size_type iEnd=nameLayer.size(); iEnd > 0; --iEnd) {
252  string name = nameLayer.substr(0,iEnd);
253  if (cfg.exists(name)) return cfg.getParameter<edm::ParameterSet>(name);
254  }
255  edm::LogError("SeedingLayerSetsBuilder") <<"configuration for layer: "<<nameLayer<<" not found, job will probably crash!";
256  return result;
257 }
258 
259 vector<vector<string> > SeedingLayerSetsBuilder::layerNamesInSets( const vector<string> & namesPSet)
260 {
261  std::vector<std::vector<std::string> > result;
262  for (std::vector<std::string>::const_iterator is=namesPSet.begin(); is < namesPSet.end(); ++is) {
263  vector<std::string> layersInSet;
264  string line = *is;
266  while (pos != string::npos ) {
267  pos=line.find("+");
268  string layer = line.substr(0,pos);
269  layersInSet.push_back(layer);
270  line=line.substr(pos+1,string::npos);
271  }
272  result.push_back(layersInSet);
273  }
274  return result;
275 }
276 
279  es.get<TrackerRecoGeometryRecord>().get( htracker );
280  const GeometricSearchTracker& tracker = *htracker;
281 
282  const std::vector<BarrelDetLayer const*>& bpx = tracker.barrelLayers();
283  const std::vector<BarrelDetLayer const*>& tib = tracker.tibLayers();
284  const std::vector<BarrelDetLayer const*>& tob = tracker.tobLayers();
285 
286  const std::vector<ForwardDetLayer const*>& fpx_pos = tracker.posForwardLayers();
287  const std::vector<ForwardDetLayer const*>& tid_pos = tracker.posTidLayers();
288  const std::vector<ForwardDetLayer const*>& tec_pos = tracker.posTecLayers();
289 
290  const std::vector<ForwardDetLayer const*>& fpx_neg = tracker.negForwardLayers();
291  const std::vector<ForwardDetLayer const*>& tid_neg = tracker.negTidLayers();
292  const std::vector<ForwardDetLayer const*>& tec_neg = tracker.negTecLayers();
293 
294  for(size_t i=0, n=theLayers.size(); i<n; ++i) {
295  const LayerSpec& layer = theLayers[i];
296  const DetLayer * detLayer = nullptr;
297  int index = layer.idLayer-1;
298 
300  detLayer = bpx[index];
301  }
302  else if (layer.subdet == GeomDetEnumerators::PixelEndcap) {
303  if (layer.side == SeedingLayer::PosEndcap) {
304  detLayer = fpx_pos[index];
305  } else {
306  detLayer = fpx_neg[index];
307  }
308  }
309  else if (layer.subdet == GeomDetEnumerators::TIB) {
310  detLayer = tib[index];
311  }
312  else if (layer.subdet == GeomDetEnumerators::TID) {
313  if (layer.side == SeedingLayer::PosEndcap) {
314  detLayer = tid_pos[index];
315  } else {
316  detLayer = tid_neg[index];
317  }
318  }
319  else if (layer.subdet == GeomDetEnumerators::TOB) {
320  detLayer = tob[index];
321  }
322  else if (layer.subdet == GeomDetEnumerators::TEC) {
323  if (layer.side == SeedingLayer::PosEndcap) {
324  detLayer = tec_pos[index];
325  } else {
326  detLayer = tec_neg[index];
327  }
328  }
329  else {
330  throw cms::Exception("Configuration") << "Did not find DetLayer for layer " << theLayerNames[layer.nameIndex];
331  }
332 
334  es.get<TransientRecHitRecord>().get(layer.hitBuilder, builder);
335 
336  theLayerDets[i] = detLayer;
337  theTTRHBuilders[i] = builder.product();
338  }
339 }
340 
342 {
343  updateEventSetup(es);
344 
345  typedef std::vector<SeedingLayer> Set;
347 
348  for(size_t i=0, n=theLayerSetIndices.size(); i<n; i += theNumberOfLayersInSet) {
349  Set set;
350  for(size_t j=0; j<theNumberOfLayersInSet; ++j) {
351  const unsigned short layerIndex = theLayerSetIndices[i+j];
352  const LayerSpec& layer = theLayers[layerIndex];
353  const DetLayer *detLayer = theLayerDets[layerIndex];
354 
355  set.push_back( SeedingLayer( theLayerNames[layerIndex], layerIndex, detLayer, theTTRHBuilders[layerIndex], layer.extractor.get()));
356  }
357  result.push_back(set);
358  }
359  return result;
360 }
361 
363  // We want to evaluate both in the first invocation (to properly
364  // initialize ESWatcher), and this way we avoid one branch compared
365  // to || (should be tiny effect)
366  return geometryWatcher_.check(es) | trhWatcher_.check(es);
367 }
368 
370 void
372  std::vector<unsigned int> & indices, ctfseeding::SeedingLayer::Hits & hits) const {
373  indices.reserve(theLayers.size());
374  for(unsigned int i=0; i<theLayers.size(); ++i) {
375  // The index of the first hit of this layer
376  indices.push_back(hits.size());
377 
378  // Obtain and copy the hits
380  std::move(tmp.begin(), tmp.end(), std::back_inserter(hits));
381  }
382 }
383 
384 //new function for FastSim only
385 std::unique_ptr<SeedingLayerSetsHits> SeedingLayerSetsBuilder::makeSeedingLayerSetsHitsforFastSim(const edm::Event& ev, const edm::EventSetup& es) {
386  updateEventSetup(es);
387 
389  ev.getByToken(fastSimrecHitsToken_,fastSimrechits_); //using FastSim RecHits
390  edm::ESHandle<TrackerTopology> trackerTopology;
391  es.get<TrackerTopologyRcd>().get(trackerTopology);
392  const TrackerTopology* const tTopo = trackerTopology.product();
394 
395  auto ret = std::make_unique<SeedingLayerSetsHits>(theNumberOfLayersInSet,
397  &theLayerNames,
398  theLayerDets);
399 
400  std::vector<unsigned int> indices;
401  indices.reserve(theLayers.size());
402 
403  for(auto& layer: theLayers) {
404  indices.push_back(layerhits_.size());
405  for(auto &rh : *fastSimrechits_){
408  int idLayer = 0;
409  if( (rh.det()->geographicalId()).subdetId() == PixelSubdetector::PixelBarrel){
411  side = SeedingLayer::Barrel;
412  idLayer = tTopo->pxbLayer(rh.det()->geographicalId());
413  }
414  else if ((rh.det()->geographicalId()).subdetId() == PixelSubdetector::PixelEndcap){
416  idLayer = tTopo->pxfDisk(rh.det()->geographicalId());
417  if(tTopo->pxfSide(rh.det()->geographicalId())==1)
418  side = SeedingLayer::NegEndcap;
419  else
420  side = SeedingLayer::PosEndcap;
421  }
422 
423  if(layer.subdet == subdet && layer.side == side && layer.idLayer == idLayer){
424  BaseTrackerRecHit const & b(rh);
425  auto ptrHit = (BaseTrackerRecHit *)(b.clone());
426  layerhits_.emplace_back(ptrHit);
427  }
428  else continue;
429  }
430  }
431  layerhits_.shrink_to_fit();
432  ret->swapHits(indices, layerhits_);
433  return ret;
434 }
#define LogDebug(id)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
ctfseeding::SeedingLayerSets layers(const edm::EventSetup &es)
static const HistoName names[]
std::vector< ForwardDetLayer const * > const & posForwardLayers() const
std::vector< LayerSpec > theLayers
SeedingLayerSetsBuilder()=default
edm::ESWatcher< TrackerRecoGeometryRecord > geometryWatcher_
LayerSpec(unsigned short index, const std::string &layerName, const edm::ParameterSet &cfgLayer, edm::ConsumesCollector &iC)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
edm::EDGetTokenT< FastTrackerRecHitCollection > fastSimrecHitsToken_
std::vector< std::string > theLayerNames
unsigned int pxfDisk(const DetId &id) const
std::unique_ptr< SeedingLayerSetsHits > makeSeedingLayerSetsHitsforFastSim(const edm::Event &ev, const edm::EventSetup &es)
bool exists(std::string const &parameterName) const
checks if a parameter exists
float clusterChargeCut(const edm::ParameterSet &conf, const char *name="clusterChargeCut")
std::vector< HitPointer > Hits
Definition: SeedingLayer.h:28
std::vector< BarrelDetLayer const * > const & tobLayers() const
bool ev
bool check(const edm::EventSetup &es)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
uint16_t size_type
std::vector< const TransientTrackingRecHitBuilder * > theTTRHBuilders
GeomDetEnumerators::SubDetector subdet
void updateEventSetup(const edm::EventSetup &es)
virtual TrackingRecHit * clone() const =0
std::vector< ForwardDetLayer const * > const & negForwardLayers() const
std::shared_ptr< ctfseeding::HitExtractor > extractor
std::vector< LinkConnSpec >::const_iterator IT
edm::ESWatcher< TransientRecHitRecord > trhWatcher_
unsigned int pxbLayer(const DetId &id) const
std::vector< LayerSetIndex > theLayerSetIndices
static SeedingLayerId nameToEnumId(const std::string &name)
void useRingSelector(int minRing, int maxRing)
std::vector< BarrelDetLayer const * > const & tibLayers() const
std::vector< ForwardDetLayer const * > const & posTecLayers() const
const T & get() const
Definition: EventSetup.h:55
double b
Definition: hdecay.h:120
std::vector< ForwardDetLayer const * > const & negTidLayers() const
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::vector< ForwardDetLayer const * > const & posTidLayers() const
std::string print(const std::vector< std::string > &names) const
unsigned int pxfSide(const DetId &id) const
void hits(const edm::Event &ev, const edm::EventSetup &es, std::vector< unsigned int > &indices, ctfseeding::SeedingLayer::Hits &hits) const
std::tuple< GeomDetEnumerators::SubDetector, ctfseeding::SeedingLayer::Side, int > SeedingLayerId
Definition: memstream.h:15
std::vector< ForwardDetLayer const * > const & negTecLayers() const
T const * product() const
Definition: ESHandle.h:86
std::tuple< int, int > getMinMaxRing() const
edm::ParameterSet layerConfig(const std::string &nameLayer, const edm::ParameterSet &cfg) const
def move(src, dest)
Definition: eostools.py:510
std::vector< const DetLayer * > theLayerDets
std::vector< BarrelDetLayer const * > const & barrelLayers() const
ctfseeding::SeedingLayer::Side side
std::vector< std::vector< SeedingLayer > > SeedingLayerSets
static std::vector< std::vector< std::string > > layerNamesInSets(const std::vector< std::string > &namesPSet)