CMS 3D CMS Logo

DTClusterer.cc
Go to the documentation of this file.
1 /******* \class DTClusterer *******
2  *
3  * Description:
4  *
5  * detailed description
6  *
7  * \author : Stefano Lacaprara - INFN LNL <stefano.lacaprara@pd.infn.it>
8  *
9  * Modification:
10  *
11  *********************************/
12 
13 /* This Class Header */
15 
16 /* Collaborating Class Header */
21 using namespace edm;
22 
27 
31 
32 /* C++ Headers */
33 #include <iostream>
34 using namespace std;
35 
36 /* ====================================================================== */
37 
38 /* Constructor */
40  // Set verbose output
41  debug = pset.getUntrackedParameter<bool>("debug");
42 
43  // the name of the 1D rec hits collection
44  recHits1DToken_ = consumes<DTRecHitCollection>(pset.getParameter<InputTag>("recHits1DLabel"));
45  // min number of hits to build a cluster
46  theMinHits = pset.getParameter<unsigned int>("minHits");
47  // min number of hits to build a cluster
48  theMinLayers = pset.getParameter<unsigned int>("minLayers");
49 
50  dtGeomToken_ = esConsumes();
51  if (debug)
52  cout << "[DTClusterer] Constructor called" << endl;
53 
54  produces<DTRecClusterCollection>();
55 }
56 
57 /* Destructor */
59 
60 /* Operations */
62  if (debug)
63  cout << "[DTClusterer] produce called" << endl;
64  // Get the DT Geometry
65  ESHandle<DTGeometry> dtGeom = setup.getHandle(dtGeomToken_);
66 
67  // Get the 1D rechits from the event
69  event.getByToken(recHits1DToken_, allHits);
70 
71  // Create the pointer to the collection which will store the rechits
72  auto clusters = std::make_unique<DTRecClusterCollection>();
73 
74  // Iterate through all hit collections ordered by LayerId
76  DTSuperLayerId oldSlId;
77  for (dtLayerIt = allHits->id_begin(); dtLayerIt != allHits->id_end(); ++dtLayerIt) {
78  // The layerId
79  DTLayerId layerId = (*dtLayerIt);
80  const DTSuperLayerId SLId = layerId.superlayerId();
81  if (SLId == oldSlId)
82  continue; // I'm on the same SL as before
83  oldSlId = SLId;
84 
85  if (debug)
86  cout << "Reconstructing the clusters in " << SLId << endl;
87 
88  const DTSuperLayer* sl = dtGeom->superLayer(SLId);
89 
90  // Get all the rec hit in the same superLayer in which layerId relies
92 
93  // Fill the vector with the 1D RecHit
94  vector<DTRecHit1DPair> pairs(range.first, range.second);
95  if (debug)
96  cout << "Number of 1D-RecHit pairs " << pairs.size() << endl;
97  vector<DTSLRecCluster> clus = buildClusters(sl, pairs);
98  if (debug)
99  cout << "Number of clusters build " << clus.size() << endl;
100  if (!clus.empty())
101  clusters->put(sl->id(), clus.begin(), clus.end());
102  }
103  event.put(std::move(clusters));
104 }
105 
106 vector<DTSLRecCluster> DTClusterer::buildClusters(const DTSuperLayer* sl, vector<DTRecHit1DPair>& pairs) const {
107  // create a vector of hits with wire position in SL frame
108  vector<pair<float, DTRecHit1DPair> > hits = initHits(sl, pairs);
109 
110  vector<DTSLRecCluster> result;
111  // loop over pairs
112  vector<DTRecHit1DPair> adiacentPairs;
113  float lastPos = hits.front().first;
114  const float cellWidth = 4.2; // cm
115  float sum = 0.;
116  float sum2 = 0.;
117 
118  for (vector<pair<float, DTRecHit1DPair> >::const_iterator hit = hits.begin(); hit != hits.end(); ++hit) {
119  if (debug)
120  cout << "Hit: " << (*hit).first << " lastPos: " << lastPos << endl;
121  // start from first hits
122  // two cells are adiacente if their position is closer than cell width
123  if (abs((*hit).first - lastPos) > cellWidth) {
124  if (adiacentPairs.size() >= theMinHits && differentLayers(adiacentPairs) >= theMinLayers) {
125  // if not, build the cluster with so far collection hits and go on
126  float mean = sum / adiacentPairs.size();
127  float err2 = sum2 / adiacentPairs.size() - mean * mean;
128  DTSLRecCluster cluster(sl->id(), LocalPoint(mean, 0., 0.), LocalError(err2, 0., 0.), adiacentPairs);
129  if (debug)
130  cout << "Cluster " << cluster << endl;
131  result.push_back(cluster);
132  }
133  // clean the vector
134  adiacentPairs.clear();
135  sum = 0.;
136  sum2 = 0.;
137  }
138  // if adiacente, add them to a vector
139  adiacentPairs.push_back((*hit).second);
140  if (debug)
141  cout << "adiacentPairs " << adiacentPairs.size() << endl;
142  sum += (*hit).first;
143  sum2 += (*hit).first * (*hit).first;
144 
145  lastPos = (*hit).first;
146  }
147  // build the last cluster
148  if (adiacentPairs.size() >= theMinHits && differentLayers(adiacentPairs) >= theMinLayers) {
149  float mean = sum / adiacentPairs.size();
150  float err2 = sum2 / adiacentPairs.size() - mean * mean;
151  DTSLRecCluster cluster(sl->id(), LocalPoint(mean, 0., 0.), LocalError(err2, 0., 0.), adiacentPairs);
152  if (debug)
153  cout << "Cluster " << cluster << endl;
154  result.push_back(cluster);
155  }
156 
157  return result;
158 }
159 
160 vector<pair<float, DTRecHit1DPair> > DTClusterer::initHits(const DTSuperLayer* sl,
161  vector<DTRecHit1DPair>& pairs) const {
162  vector<pair<float, DTRecHit1DPair> > result;
163  for (vector<DTRecHit1DPair>::const_iterator pair = pairs.begin(); pair != pairs.end(); ++pair) {
164  // get wire
165  DTWireId wid = (*pair).wireId();
166  // get Layer
167  const DTLayer* lay = sl->layer(wid.layer());
168  // get wire position in SL (only x)
169  LocalPoint posInLayer(lay->specificTopology().wirePosition(wid.wire()), 0., 0.);
170  LocalPoint posInSL = sl->toLocal(lay->toGlobal(posInLayer));
171  // put the pair into result
172  result.push_back(make_pair(posInSL.x(), *pair));
173  }
174  // sorted by x
175  sort(result.begin(), result.end(), sortClusterByX());
176 
177  return result;
178 }
179 
180 unsigned int DTClusterer::differentLayers(vector<DTRecHit1DPair>& hits) const {
181  // Count the number of different layers
182  int layers = 0;
183  unsigned int result = 0;
184  for (vector<DTRecHit1DPair>::const_iterator hit = hits.begin(); hit != hits.end(); ++hit) {
185  int pos = (1 << ((*hit).wireId().layer() - 1));
186  if (!(pos & layers)) {
187  result++;
188  layers |= pos;
189  }
190  }
191  return result;
192 }
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
unsigned int differentLayers(std::vector< DTRecHit1DPair > &hits) const
Definition: DTClusterer.cc:180
int wire() const
Return the wire number.
Definition: DTWireId.h:42
~DTClusterer() override
Definition: DTClusterer.cc:58
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:58
void produce(edm::StreamID, edm::Event &event, const edm::EventSetup &setup) const override
Definition: DTClusterer.cc:61
static std::pair< DTLayerId, DTSuperLayerIdComparator > layersBySuperLayer(DTSuperLayerId slId)
Access by SL objects written into a RangeMap by layer.
identifier iterator
Definition: RangeMap.h:130
T x() const
Definition: PV3DBase.h:59
const DTSuperLayer * superLayer(const DTSuperLayerId &id) const
Return a DTSuperLayer given its id.
Definition: DTGeometry.cc:92
const DTLayer * layer(const DTLayerId &id) const
Return the layer corresponding to the given id.
Definition: DTSuperLayer.cc:54
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const DTTopology & specificTopology() const
Definition: DTLayer.cc:37
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
DTClusterer(const edm::ParameterSet &)
Definition: DTClusterer.cc:39
#define debug
Definition: HDRShower.cc:19
int layer() const
Return the layer number.
Definition: DTLayerId.h:42
HLT enums.
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:45
std::vector< std::pair< float, DTRecHit1DPair > > initHits(const DTSuperLayer *sl, std::vector< DTRecHit1DPair > &pairs) const
Definition: DTClusterer.cc:160
std::vector< DTSLRecCluster > buildClusters(const DTSuperLayer *sl, std::vector< DTRecHit1DPair > &pairs) const
Definition: DTClusterer.cc:106
def move(src, dest)
Definition: eostools.py:511
DTSuperLayerId id() const
Return the DetId of this SL.
Definition: DTSuperLayer.cc:34
Definition: event.py:1
float wirePosition(int wireNumber) const
Returns the x position in the layer of a given wire number.
Definition: DTTopology.cc:63