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