CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GroupedDAFHitCollector.cc
Go to the documentation of this file.
12 
13 #include <vector>
14 #include <map>
15 
16 using namespace std;
17 
18 vector<TrajectoryMeasurement> GroupedDAFHitCollector::recHits(const Trajectory& traj,
19  const MeasurementTrackerEvent *theMTE) const
20 {
21 
22  LayerMeasurements theLM (theMTE->measurementTracker(), *theMTE);
23 
24  //WARNING: At the moment the trajectories has the measurements with reversed sorting after the track smoothing
25  const vector<TrajectoryMeasurement>& meas = traj.measurements();
26  const Propagator* forwardPropagator = getPropagator();
27  const Propagator* backwardPropagator = getReversePropagator();
28  if (traj.direction() == alongMomentum){
29  forwardPropagator = getReversePropagator();
30  backwardPropagator = getPropagator();
31  }
32  if (meas.empty()) //return TransientTrackingRecHit::ConstRecHitContainer();
33  return vector<TrajectoryMeasurement>();
34 
35  //groups the TrajectoryMeasurements on a layer by layer
36  vector<pair<const DetLayer*, vector<TrajectoryMeasurement> > > mol;
37  mol = MeasurementByLayerGrouper(getMeasurementTracker()->geometricSearchTracker())(meas);
38 
39  vector<TrajectoryMeasurement> result;
40 
41  //add a protection if all the measurement are on the same layer
42  if(mol.size()<2)return vector<TrajectoryMeasurement>();
43 
44  //it assumes that the measurements are sorted in the smoothing direction
45  //TrajectoryStateOnSurface current = (*(mol.begin()+1)).second.front().updatedState();
46  TrajectoryStateOnSurface current = (*(mol.rbegin()+1)).second.back().updatedState();
47  //if (current.isValid()) current.rescaleError(10);
48 
49 
50  //protection for layers with invalid meas with no id associated
51  //to be fixed
52  //for the moment no hit are lookerd for in these layers
53  //remind that:
54  //groupedMeasurements will return at least a measurement with an invalid hit with no detid
55  LogDebug("MultiRecHitCollector") << "Layer " << mol.back().first << " has " << mol.back().second.size() << " measurements";
56  LogTrace("MultiRecHitCollector") << "Original measurements are:";
57  for( unsigned int iLay = 0; iLay < mol.size(); iLay++){
58  LogTrace("MultiRecHitCollector") << " Layer " << mol.at(iLay).first << " has " << mol.at(iLay).second.size() << " measurements:";
59  vector<TrajectoryMeasurement>::const_iterator ibeg = (mol.at(iLay)).second.begin();
60  vector<TrajectoryMeasurement>::const_iterator iend = (mol.at(iLay)).second.end();
61  for (vector<TrajectoryMeasurement>::const_iterator imeas = ibeg; imeas != iend; ++imeas){
62  if (imeas->recHit()->isValid()){
63  LogTrace("MultiRecHitCollector") << " Valid Hit with DetId " << imeas->recHit()->geographicalId().rawId()
64  << " local position " << imeas->recHit()->hit()->localPosition()
65  << " global position " << imeas->recHit()->hit()->globalPosition() ;
66  } else {
67  LogTrace("MultiRecHitCollector") << " Invalid Hit with DetId " << imeas->recHit()->geographicalId().rawId();
68  }
69  }
70  }
71 
72  //ERICA: I have to understand how are set the TM now. REPLACE THIS PART!!
73  vector<TrajectoryMeasurementGroup> groupedMeas;
74  if (mol.back().first)
75  groupedMeas = theLM.groupedMeasurements(*(mol.back().first), current,
76  *backwardPropagator, *(getEstimator()));
77 
78  //Since we have passed the backwardPropagator, we have to sort the detGroups in the opposite way
79  //(according the forward propagator, not the backward one)
80  vector<TrajectoryMeasurementGroup> sortedgroupedMeas;
81  for (vector<TrajectoryMeasurementGroup>::reverse_iterator iter = groupedMeas.rbegin();
82  iter != groupedMeas.rend(); iter++){
83 
84  sortedgroupedMeas.push_back(*iter);
85 
86  }
87 
88  //for the first layer
89  buildMultiRecHits(sortedgroupedMeas, result);
90 
91 
92  //for other layers
93  current = mol.back().second.front().updatedState();
94  //if (current.isValid()) current.rescaleError(10);
95 
96  for( vector<pair<const DetLayer*, vector<TrajectoryMeasurement> > >::reverse_iterator imol =
97  mol.rbegin() + 1; imol != mol.rend(); imol++) {
98 
99  const DetLayer* lay = (*imol).first;
100  LogDebug("MultiRecHitCollector") << "Layer " << lay << " has " << (*imol).second.size() << " measurements";
101  //debug
102  vector<TrajectoryMeasurementGroup> currentLayerMeas;
103  if (lay) {
104  currentLayerMeas = theLM.groupedMeasurements(*lay, current, *forwardPropagator, *(getEstimator()));
105  }
106 
107  buildMultiRecHits(currentLayerMeas, result);
108  current = (*imol).second.front().updatedState();
109  //if (current.isValid()) current.rescaleError(10);
110  }
111 
112  LogTrace("MultiRecHitCollector") << " Ending GroupedDAFHitCollector::recHits >> Original Measurement size " << meas.size()
113  << "\n >> GroupedDAFHitCollector returned " << result.size() << " measurements";
114  //results are sorted in the fitting direction
115 
116  // adding a protection against too few hits and invalid hits (due to failed propagation on the same surface of the original hits)
117  if (result.size()>2)
118  {
119  int hitcounter=0;
120  //check if the vector result has more than 3 valid hits
121  for (vector<TrajectoryMeasurement>::const_iterator iimeas = result.begin(); iimeas != result.end(); ++iimeas)
122  {
123  if(iimeas->recHit()->isValid()) hitcounter++;
124  }
125 
126  if(hitcounter>2)
127  {
128  return result;
129  }
130 
131  else return vector<TrajectoryMeasurement>();
132  }
133 
134  else{return vector<TrajectoryMeasurement>();}
135 
136 }
137 
138 void GroupedDAFHitCollector::buildMultiRecHits(const vector<TrajectoryMeasurementGroup>& measgroup, vector<TrajectoryMeasurement>& result) const {
139 
140  unsigned int initial_size = result.size();
141 
142  //TransientTrackingRecHit::ConstRecHitContainer rhits;
143  if (measgroup.empty()) {
144  LogTrace("MultiRecHitCollector") << "No TrajectoryMeasurementGroups found for this layer\n" ;
145  //should we do something?
146  //result.push_back(InvalidTransientRecHit::build(0,TrackingRecHit::missing));
147  return;
148  }
149 
150  //we build a MultiRecHit out of each group
151  //groups are sorted along momentum or opposite to momentum,
152  //measurements in groups are sorted with increating chi2
153  LogTrace("MultiRecHitCollector") << "Found " << measgroup.size() << " groups for this layer";
154 
155  //trajectory state to store the last valid TrajectoryState (if present) to be used
156  //to add an invalid Measurement in case no valid state or no valid hits are found in any group
157  for ( vector<TrajectoryMeasurementGroup>::const_iterator igroup = measgroup.begin();
158  igroup != measgroup.end(); igroup++ ){
159 
160  //the TrajectoryState is the first one
161  TrajectoryStateOnSurface state = igroup->measurements().front().predictedState();
162  if (!state.isValid()){
163  LogTrace("MultiRecHitCollector") << "Something wrong! no valid TSOS found in current group ";
164  continue;
165  }
166 
167  LogTrace("MultiRecHitCollector") << "This group has " << igroup->measurements().size() << " measurements";
168  LogTrace("MultiRecHitCollector") << "This group has the following " << igroup->detGroup().size()
169  << " detector ids: " << endl;
170  for (DetGroup::const_iterator idet = igroup->detGroup().begin(); idet != igroup->detGroup().end(); ++idet){
171  LogTrace("MultiRecHitCollector") << idet->det()->geographicalId().rawId();
172  }
173 
174  vector<const TrackingRecHit*> hits;
175  for (vector<TrajectoryMeasurement>::const_iterator imeas = igroup->measurements().begin();
176  imeas != igroup->measurements().end(); imeas++){
177 
178  //collect the non missing hits to build the MultiRecHits
179  //we use the recHits method; anyway only simple hits, not MultiHits should be present
180  if (imeas->recHit()->getType() != TrackingRecHit::missing) {
181  LogTrace("MultiRecHitCollector") << "This hit is valid ";
182  hits.push_back(imeas->recHit()->hit());
183  }
184  else{
185  LogTrace("MultiRecHitCollector") << " This hit is not valid and will not enter in the MRH. " ;
186  }
187  }
188 
189  if (hits.empty()){
190  LogTrace("MultiRecHitCollector") << "No valid hits found in current group ";
191  continue;
192  }
193 
194  LogTrace("MultiRecHitCollector") << "The best TSOS in this group is " << state << " it lays on surface located at " << state.surface().position();
195 
196  LogTrace("MultiRecHitCollector") << "For the MRH on this group the following hits will be used";
197  for (vector<const TrackingRecHit*>::iterator iter = hits.begin(); iter != hits.end(); iter++){
198  string validity = "valid";
199  if ((*iter)->getType() == TrackingRecHit::missing ) validity = "missing !should not happen!";
200  else if ((*iter)->getType() == TrackingRecHit::inactive) validity = "inactive";
201  else if ((*iter)->getType() == TrackingRecHit::bad) validity = "bad";
202  LogTrace("MultiRecHitCollector") << "DetId " << (*iter)->geographicalId().rawId()
203  << " validity: " << validity
204  << " surface position " << getMeasurementTracker()->geomTracker()->idToDet((*iter)->geographicalId())->position()
205  << " hit local position " << (*iter)->localPosition();
206  }
207 
208  result.push_back(TrajectoryMeasurement(state,theUpdator->buildMultiRecHit(hits, state)));
209  }
210  //can this happen? it means that the measgroup was not empty but no valid measurement was found inside
211  //in this case we add an invalid measuremnt for this layer
212  if (result.size() == initial_size){
213  LogTrace("MultiRecHitCollector") << "no valid measuremnt or no valid TSOS in none of the groups";
214  //measgroup has been already checked for size != 0
215  if (measgroup.back().measurements().size() != 0){
216  result.push_back(measgroup.back().measurements().back());
217  }
218  }
219 }
220 
#define LogDebug(id)
void buildMultiRecHits(const std::vector< TrajectoryMeasurementGroup > &measgroup, std::vector< TrajectoryMeasurement > &result) const
virtual std::vector< TrajectoryMeasurement > recHits(const Trajectory &, const MeasurementTrackerEvent *theMT) const
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
PropagationDirection const & direction() const
Definition: Trajectory.cc:118
U second(std::pair< T, U > const &p)
DataContainer const & measurements() const
Definition: Trajectory.h:203
const SurfaceType & surface() const
tuple result
Definition: query.py:137
#define LogTrace(id)
const MeasurementTracker & measurementTracker() const
const PositionType & position() const