CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
StandAloneTrajectoryBuilder.cc
Go to the documentation of this file.
1 
12 
18 
22 
26 
28 
36 
37 
38 using namespace edm;
39 using namespace std;
40 
42  const MuonServiceProxy* service):theService(service){
43  const std::string metname = "Muon|RecoMuon|StandAloneMuonTrajectoryBuilder";
44 
45  LogTrace(metname) << "constructor called" << endl;
46 
47  // The navigation type:
48  // "Direct","Standard"
49  theNavigationType = par.getParameter<string>("NavigationType");
50 
51  // The inward-outward fitter (starts from seed state)
52  ParameterSet filterPSet = par.getParameter<ParameterSet>("FilterParameters");
53  filterPSet.addParameter<string>("NavigationType",theNavigationType);
54  theFilter = new StandAloneMuonFilter(filterPSet,theService);
55 
56  // Fit direction
57  string seedPosition = par.getParameter<string>("SeedPosition");
58 
59  if (seedPosition == "in" ) theSeedPosition = recoMuon::in;
60  else if (seedPosition == "out" ) theSeedPosition = recoMuon::out;
61  else
62  throw cms::Exception("StandAloneMuonFilter constructor")
63  <<"Wrong seed position chosen in StandAloneMuonFilter::StandAloneMuonFilter ParameterSet"
64  << "\n"
65  << "Possible choices are:"
66  << "\n"
67  << "SeedPosition = in or SeedPosition = out";
68 
69  // Propagator for the seed extrapolation
70  theSeedPropagatorName = par.getParameter<string>("SeedPropagator");
71 
72  // Disable/Enable the backward filter
73  doBackwardFilter = par.getParameter<bool>("DoBackwardFilter");
74 
75  // Disable/Enable the refit of the trajectory
76  doRefit = par.getParameter<bool>("DoRefit");
77 
78  // Disable/Enable the refit of the trajectory seed
79  doSeedRefit = par.getParameter<bool>("DoSeedRefit");
80 
81  if(doBackwardFilter){
82  // The outward-inward fitter (starts from theFilter outermost state)
83  ParameterSet bwFilterPSet = par.getParameter<ParameterSet>("BWFilterParameters");
84  // theBWFilter = new StandAloneMuonBackwardFilter(bwFilterPSet,theService); // FIXME
85  bwFilterPSet.addParameter<string>("NavigationType",theNavigationType);
86  theBWFilter = new StandAloneMuonFilter(bwFilterPSet,theService);
87 
88  theBWSeedType = bwFilterPSet.getParameter<string>("BWSeedType");
89  }
90 
91  if(doRefit){
92  // The outward-inward fitter (starts from theBWFilter innermost state)
93  ParameterSet refitterPSet = par.getParameter<ParameterSet>("RefitterParameters");
94  theRefitter = new StandAloneMuonRefitter(refitterPSet, theService);
95  }
96 
97  // The seed transformer (used to refit the seed and get the seed transient state)
98  // ParameterSet seedTransformerPSet = par.getParameter<ParameterSet>("SeedTransformerParameters");
99  ParameterSet seedTransformerParameters = par.getParameter<ParameterSet>("SeedTransformerParameters");
100  theSeedTransformer = new SeedTransformer(seedTransformerParameters);
101 
102 }
103 
105  theFilter->setEvent(event);
107 }
108 
110 
111  LogTrace("Muon|RecoMuon|StandAloneMuonTrajectoryBuilder")
112  << "StandAloneMuonTrajectoryBuilder destructor called" << endl;
113 
114  if(theFilter) delete theFilter;
116  if(doRefit && theRefitter) delete theRefitter;
118 }
119 
120 
121 namespace {
122  struct Resetter {
124  explicit Resetter(StandAloneMuonFilter * imf) : mf(imf){}
125  ~Resetter() { if(mf) mf->reset();}
126  };
127 }
128 
131  Resetter fwReset(filter());
132  Resetter bwReset(bwfilter());
133 
134  const std::string metname = "Muon|RecoMuon|StandAloneMuonTrajectoryBuilder";
136 
137  // Set the services for the seed transformer
138  theSeedTransformer->setServices(theService->eventSetup());
139 
140  // the trajectory container. In principle starting from one seed we can
141  // obtain more than one trajectory. TODO: this feature is not yet implemented!
142  TrajectoryContainer trajectoryContainer;
143 
145  Trajectory trajectoryFW(seed,fwDirection);
146 
147  TrajectoryStateOnSurface lastTSOS;
148  DetId lastDetId;
149 
150  vector<Trajectory> seedTrajectories;
151 
152  if(doSeedRefit) {
153  seedTrajectories = theSeedTransformer->seedTransform(seed);
154  if(!seedTrajectories.empty()) {
155  TrajectoryMeasurement lastTM(seedTrajectories.front().lastMeasurement());
156  lastTSOS = lastTM.updatedState();
157  lastDetId = lastTM.recHit()->geographicalId();
158  }
159  }
160 
161  if(!doSeedRefit || seedTrajectories.empty()) {
162  lastTSOS = theSeedTransformer->seedTransientState(seed);
163  lastDetId = seed.startingState().detId();
164  }
165 
166  LogTrace(metname) << "Trajectory State on Surface before the extrapolation"<<endl;
167  LogTrace(metname) << debug.dumpTSOS(lastTSOS);
168 
169  // Segment layer
170  LogTrace(metname) << "The RecSegment relies on: "<<endl;
171  LogTrace(metname) << debug.dumpMuonId(lastDetId);
172 
173  DetLayerWithState inputFromSeed = propagateTheSeedTSOS(lastTSOS, lastDetId);
174 
175  // refine the FTS given by the seed
176 
177  // the trajectory is filled in the refitter::refit
178  filter()->refit(inputFromSeed.second,inputFromSeed.first,trajectoryFW);
179 
180  // "0th order" check...
181  if( trajectoryFW.empty() ) {
182  LogTrace(metname) << "Forward trajectory EMPTY! No trajectory will be loaded!" << endl;
183  return trajectoryContainer;
184  }
185 
186  // Get the last TSOS
187  // TrajectoryStateOnSurface tsosAfterRefit = filter()->lastUpdatedTSOS(); // this is the last UPDATED TSOS
188  TrajectoryStateOnSurface tsosAfterRefit = filter()->lastCompatibleTSOS(); // this is the last COMPATIBLE TSOS
189 
190  LogTrace(metname) << "StandAloneMuonTrajectoryBuilder filter output " << endl;
191  LogTrace(metname) << debug.dumpTSOS(tsosAfterRefit);
192 
193 
194  /*
195  // -- 1st attempt
196  if( filter()->isCompatibilitySatisfied() ) {
197  if( filter()->layers().size() ) // OR if( filter()->goodState() ) ??? Maybe when only RPC hits are used...
198  LogTrace(metname) << debug.dumpLayer( filter()->lastDetLayer() );
199  else {
200  LogTrace(metname) << "Compatibility satisfied, but all RecHits are invalid! A trajectory with only invalid hits will be loaded!" << endl;
201  trajectoryContainer.push_back(new Trajectory(trajectoryFW));
202  return trajectoryContainer;
203  }
204  }
205  else {
206  LogTrace(metname) << "Compatibility NOT satisfied after Forward filter! No trajectory will be loaded!" << endl;
207  LogTrace(metname) << "Total chambers: " << filter()->getTotalCompatibleChambers() << "; DT: "
208  << filter()->getDTCompatibleChambers() << "; CSC: " << filter()->getCSCCompatibleChambers() << endl;
209  return trajectoryContainer;
210  }
211  // -- end 1st attempt
212  */
213 
214  // -- 2nd attempt
215  if( filter()->goodState() ) {
216  LogTrace(metname) << debug.dumpLayer( filter()->lastDetLayer() );
217  }
218  else if( filter()->isCompatibilitySatisfied() ) {
219  int foundValidRh = trajectoryFW.foundHits(); // number of valid hits
220  LogTrace(metname) << "Compatibility satisfied after Forward filter, but too few valid RecHits ("
221  << foundValidRh << ")." << endl
222  << "A trajectory with only invalid RecHits will be loaded!" << endl;
223  if(!foundValidRh) {
224  trajectoryContainer.push_back(new Trajectory(trajectoryFW));
225  return trajectoryContainer;
226  }
227  Trajectory defaultTraj(seed,fwDirection);
228  filter()->createDefaultTrajectory(trajectoryFW, defaultTraj);
229  trajectoryContainer.push_back(new Trajectory(defaultTraj));
230  return trajectoryContainer;
231  }
232  else {
233  LogTrace(metname) << "Compatibility NOT satisfied after Forward filter! No trajectory will be loaded!" << endl;
234  LogTrace(metname) << "Total compatible chambers: " << filter()->getTotalCompatibleChambers() << "; DT: "
235  << filter()->getDTCompatibleChambers() << "; CSC: " << filter()->getCSCCompatibleChambers()
236  << "; RPC: " << filter()->getRPCCompatibleChambers() << endl;
237  return trajectoryContainer;
238  }
239  // -- end 2nd attempt
240 
241  LogTrace(metname) << "Number of DT/CSC/RPC chamber used (fw): "
242  << filter()->getDTChamberUsed() << "/"
243  << filter()->getCSCChamberUsed() << "/"
244  << filter()->getRPCChamberUsed() <<endl;
245  LogTrace(metname) << "Momentum: " <<tsosAfterRefit.freeState()->momentum();
246 
247 
248  if(!doBackwardFilter) {
249  LogTrace(metname) << "Only forward refit requested. No backward refit will be performed!"<<endl;
250 
251  // ***** Refit of fwd step *****
252  // if (doRefit && !trajectoryFW.empty() && filter()->goodState()){ // CHECK!!! Can trajectoryFW really be empty at this point??? And goodState...?
253  if(doRefit) {
254  pair<bool,Trajectory> refitResult = refitter()->refit(trajectoryFW);
255  if(refitResult.first) {
256  trajectoryContainer.push_back(new Trajectory(refitResult.second));
257  LogTrace(metname) << "StandAloneMuonTrajectoryBuilder refit output " << endl ;
258  LogTrace(metname) << debug.dumpTSOS(refitResult.second.lastMeasurement().updatedState());
259  }
260  else
261  trajectoryContainer.push_back(new Trajectory(trajectoryFW));
262  }
263  else
264  trajectoryContainer.push_back(new Trajectory(trajectoryFW));
265 
266  LogTrace(metname) << "Trajectory saved" << endl;
267  return trajectoryContainer;
268  }
269 
270 
271  // ***** Backward filtering *****
272 
273  TrajectorySeed seedForBW;
274 
275  if(theBWSeedType == "noSeed") {
276  TrajectorySeed seedVoid;
277  seedForBW = seedVoid;
278  }
279  else if(theBWSeedType == "fromFWFit") {
280 
281 
282 
283  PTrajectoryStateOnDet seedTSOS =
284  trajectoryStateTransform::persistentState( tsosAfterRefit, trajectoryFW.lastMeasurement().recHit()->geographicalId().rawId());
285 
286  edm::OwnVector<TrackingRecHit> recHitsContainer;
288  TrajectorySeed fwFit(seedTSOS,recHitsContainer,seedDirection);
289 
290  seedForBW = fwFit;
291  }
292  else if(theBWSeedType == "fromGenerator") {
293  seedForBW = seed;
294  }
295  else
296  LogWarning(metname) << "Wrong seed type for the backward filter!";
297 
299  Trajectory trajectoryBW(seedForBW,bwDirection);
300 
301  // FIXME! under check!
302  bwfilter()->refit(tsosAfterRefit,filter()->lastDetLayer(),trajectoryBW);
303 
304  // Get the last TSOS
305  TrajectoryStateOnSurface tsosAfterBWRefit = bwfilter()->lastUpdatedTSOS();
306 
307  LogTrace(metname) << "StandAloneMuonTrajectoryBuilder BW filter output " << endl ;
308  LogTrace(metname) << debug.dumpTSOS(tsosAfterBWRefit);
309 
310  LogTrace(metname)
311  << "Number of RecHits: " << trajectoryBW.foundHits() << "\n"
312  << "Number of DT/CSC/RPC chamber used (bw): "
313  << bwfilter()->getDTChamberUsed() << "/"
314  << bwfilter()->getCSCChamberUsed() << "/"
315  << bwfilter()->getRPCChamberUsed();
316 
317  // -- The trajectory is "good" if there are at least 2 chambers used in total and at
318  // least 1 is "tracking" (DT or CSC)
319  // -- The trajectory satisfies the "compatibility" requirements if there are at least
320  // 2 compatible chambers (not necessarily used!) in total and at
321  // least 1 is "tracking" (DT or CSC)
322  // 1st attempt
323  /*
324  if (bwfilter()->isCompatibilitySatisfied()) {
325 
326  if (doRefit && !trajectoryBW.empty() && bwfilter()->goodState()){
327  pair<bool,Trajectory> refitResult = refitter()->refit(trajectoryBW);
328  if (refitResult.first){
329  trajectoryContainer.push_back(new Trajectory(refitResult.second));
330  LogTrace(metname) << "StandAloneMuonTrajectoryBuilder Refit output " << endl;
331  LogTrace(metname) << debug.dumpTSOS(refitResult.second.lastMeasurement().updatedState());
332  }
333  else
334  trajectoryContainer.push_back(new Trajectory(trajectoryBW));
335  }
336  else
337  trajectoryContainer.push_back(new Trajectory(trajectoryBW));
338 
339  LogTrace(metname)<< "Trajectory saved" << endl;
340 
341  }
342  //if the trajectory is not saved, but at least two chamber are used in the
343  //forward filtering, try to build a new trajectory starting from the old
344  //trajectory w/o the latest measurement and a looser chi2 cut
345  else if ( filter()->getTotalChamberUsed() >= 2 ) {
346  LogTrace(metname)<< "Trajectory NOT saved. Second Attempt." << endl;
347  // FIXME:
348  // a better choice could be: identify the poorest one, exclude it, redo
349  // the fw and bw filtering. Or maybe redo only the bw without the excluded
350  // measure. As first step I will port the ORCA algo, then I will move to the
351  // above pattern.
352 
353  }
354 
355  else {
356  LogTrace(metname) << "Compatibility NOT satisfied after Backward filter!" << endl;
357  LogTrace(metname) << "The result of Forward filter will be loaded!" << endl;
358 
359  trajectoryContainer.push_back(new Trajectory(trajectoryFW));
360  }
361  */
362  // end 1st attempt
363 
364  // 2nd attempt
365  if( bwfilter()->goodState() ) {
366  LogTrace(metname) << debug.dumpLayer( bwfilter()->lastDetLayer() );
367  }
368  else if( bwfilter()->isCompatibilitySatisfied() ) {
369  LogTrace(metname) << "Compatibility satisfied after Backward filter, but too few valid RecHits ("
370  << trajectoryBW.foundHits() << ")." << endl
371  << "The (good) result of FW filter will be loaded!" << endl;
372  trajectoryContainer.push_back(new Trajectory(trajectoryFW));
373  return trajectoryContainer;
374  }
375  else {
376  LogTrace(metname) << "Compatibility NOT satisfied after Backward filter!" << endl
377  << "The Forward trajectory will be invalidated and then loaded!" << endl;
378  Trajectory defaultTraj(seed,fwDirection);
379  filter()->createDefaultTrajectory(trajectoryFW, defaultTraj);
380  trajectoryContainer.push_back(new Trajectory(defaultTraj));
381  return trajectoryContainer;
382  }
383  // end 2nd attempt
384 
385  if(doRefit) {
386  pair<bool,Trajectory> refitResult = refitter()->refit(trajectoryBW);
387  if(refitResult.first) {
388  trajectoryContainer.push_back(new Trajectory(refitResult.second));
389  LogTrace(metname) << "StandAloneMuonTrajectoryBuilder Refit output " << endl;
390  LogTrace(metname) << debug.dumpTSOS(refitResult.second.lastMeasurement().updatedState());
391  }
392  else
393  trajectoryContainer.push_back(new Trajectory(trajectoryBW));
394  }
395  else
396  trajectoryContainer.push_back(new Trajectory(trajectoryBW));
397 
398  LogTrace(metname) << "Trajectory saved" << endl;
399 
400  return trajectoryContainer;
401 }
402 
403 
406 
407  const std::string metname = "Muon|RecoMuon|StandAloneMuonTrajectoryBuilder";
409 
410  DetId seedDetId(aDetId);
411  // const GeomDet* gdet = theService->trackingGeometry()->idToDet( seedDetId );
412 
413  TrajectoryStateOnSurface initialState(aTSOS);
414 
415  LogTrace(metname)<<"Seed's Pt: "<<initialState.freeState()->momentum().perp()<<endl;
416 
417  LogTrace(metname)<< "Seed's id: "<< endl ;
418  LogTrace(metname) << debug.dumpMuonId(seedDetId);
419 
420  // Get the layer on which the seed relies
421  const DetLayer *initialLayer = theService->detLayerGeometry()->idToLayer( seedDetId );
422 
423  LogTrace(metname)<< "Seed's detLayer: "<< endl ;
424  LogTrace(metname) << debug.dumpLayer(initialLayer);
425 
426  LogTrace(metname)<< "TrajectoryStateOnSurface before propagation:" << endl;
427  LogTrace(metname) << debug.dumpTSOS(initialState);
428 
429 
431 
432  // ask for compatible layers
433  vector<const DetLayer*> detLayers;
434 
435  if(theNavigationType == "Standard")
436  detLayers = initialLayer->compatibleLayers( *initialState.freeState(),detLayerOrder);
437  else if (theNavigationType == "Direct"){
438  DirectMuonNavigation navigation( &*theService->detLayerGeometry() );
439  detLayers = navigation.compatibleLayers( *initialState.freeState(),detLayerOrder);
440  }
441  else
442  edm::LogError(metname) << "No Properly Navigation Selected!!"<<endl;
443 
444 
445  LogTrace(metname) << "There are "<< detLayers.size() <<" compatible layers"<<endl;
446 
447  DetLayerWithState result = DetLayerWithState(initialLayer,initialState);
448 
449  if(detLayers.size()){
450 
451  LogTrace(metname) << "Compatible layers:"<<endl;
452  for( vector<const DetLayer*>::const_iterator layer = detLayers.begin();
453  layer != detLayers.end(); layer++){
454  LogTrace(metname) << debug.dumpMuonId((*layer)->basicComponents().front()->geographicalId());
455  LogTrace(metname) << debug.dumpLayer(*layer);
456  }
457 
458  const DetLayer* finalLayer = detLayers.back();
459 
460  if(theSeedPosition == recoMuon::in) LogTrace(metname) << "Most internal one:"<<endl;
461  else LogTrace(metname) << "Most external one:"<<endl;
462 
463  LogTrace(metname) << debug.dumpLayer(finalLayer);
464 
465  const TrajectoryStateOnSurface propagatedState =
466  theService->propagator(theSeedPropagatorName)->propagate(initialState,
467  finalLayer->surface());
468 
469  if(propagatedState.isValid()){
470  result = DetLayerWithState(finalLayer,propagatedState);
471 
472  LogTrace(metname) << "Trajectory State on Surface after the extrapolation"<<endl;
473  LogTrace(metname) << debug.dumpTSOS(propagatedState);
474  LogTrace(metname) << debug.dumpLayer(finalLayer);
475  }
476  else
477  LogTrace(metname)<< "Extrapolation failed. Keep the original seed state" <<endl;
478  }
479  else
480  LogTrace(metname)<< "No compatible layers. Keep the original seed state" <<endl;
481 
482  return result;
483 }
484 
485 
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:234
T getParameter(std::string const &) const
int foundHits() const
Definition: Trajectory.h:224
virtual const BoundSurface & surface() const =0
The surface of the GeometricSearchDet.
T perp() const
Definition: PV3DBase.h:71
void createDefaultTrajectory(const Trajectory &, Trajectory &)
std::string dumpLayer(const DetLayer *layer) const
const std::string metname
void setServices(const edm::EventSetup &)
Set the services needed by the SeedTransformer.
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
ConstRecHitPointer recHit() const
MuonCandidate::TrajectoryContainer TrajectoryContainer
int getRPCCompatibleChambers() const
PropagationDirection
TrajectoryStateOnSurface lastUpdatedTSOS() const
the Trajectory state on the last surface of the fitting
void refit(const TrajectoryStateOnSurface &initialState, const DetLayer *, Trajectory &trajectory)
Perform the inner-outward fitting.
std::vector< const DetLayer * > compatibleLayers(const FreeTrajectoryState &fts, PropagationDirection timeDirection) const
std::string dumpMuonId(const DetId &id) const
std::vector< Trajectory > seedTransform(const TrajectorySeed &) const
Performs the fit.
std::string dumpTSOS(const TrajectoryStateOnSurface &tsos) const
FreeTrajectoryState * freeState(bool withErrors=true) const
int getCSCCompatibleChambers() const
int getDTCompatibleChambers() const
virtual void setEvent(const edm::Event &event)
Pass the Event to the algo at each event.
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:145
tuple result
Definition: query.py:137
StandAloneMuonFilter * bwfilter() const
actual filter
unsigned int detId() const
RefitResult refit(const Trajectory &)
virtual void setEvent(const edm::Event &event)
Pass the Event to the algo at each event.
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
GlobalVector momentum() const
#define LogTrace(id)
std::pair< const DetLayer *, TrajectoryStateOnSurface > DetLayerWithState
Definition: DetId.h:20
PTrajectoryStateOnDet const & startingState() const
TrajectoryStateOnSurface seedTransientState(const TrajectorySeed &) const
StandAloneMuonFilter * filter() const
pre-filter
TrajectoryContainer trajectories(const TrajectorySeed &)
return a container of the reconstructed trajectories compatible with a given seed ...
std::vector< const DetLayer * > compatibleLayers(NavigationDirection direction) const
Definition: DetLayer.cc:60
int getTotalCompatibleChambers() const
std::string theSeedPropagatorName
Propagator for the seed extrapolation.
StandAloneMuonTrajectoryBuilder(const edm::ParameterSet &, const MuonServiceProxy *)
Constructor with Parameter set and MuonServiceProxy.
#define debug
Definition: MEtoEDMFormat.h:34
virtual ~StandAloneMuonTrajectoryBuilder()
Destructor.
StandAloneMuonRefitter * refitter() const
refitter of the hits container
TrajectoryStateOnSurface lastCompatibleTSOS() const
the Trajectory state on the last compatible surface
DetLayerWithState propagateTheSeedTSOS(TrajectoryStateOnSurface &aTSOS, DetId &aDetId)