CMS 3D CMS Logo

MultiTrajectoryStateAssembler.cc
Go to the documentation of this file.
7 
9  : combinationDone(false), thePzError(false), theValidWeightSum(0.), theInvalidWeightSum(0.) {
10  //
11  // parameters (could be configurable)
12  //
13  sortStates = false;
14  minValidFraction = 0.01;
15  minFractionalWeight = 1.e-6; // 4;
16 }
17 
19  //
20  // refuse to add states after combination has been done
21  //
22  if (combinationDone)
23  throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add states after combination";
24  //
25  // Verify validity of state to be added
26  //
27  if (!tsos.isValid())
28  throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add invalid state";
29  //
30  // Add components (i.e. state to be added can be single or multi state)
31  //
32  GetComponents comps(tsos);
33  MultiTSOS components(comps());
35 }
36 
38  //
39  // refuse to add states after combination has been done
40  //
41  if (combinationDone)
42  throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add states after combination";
43  //
44  // sum up weights (all components are supposed to be valid!!!) and
45  // check for consistent pz
46  //
47  double sum(0.);
48  double pzFirst = theStates.empty() ? 0. : theStates.front().localParameters().pzSign();
49  for (MultiTSOS::const_iterator i = states.begin(); i != states.end(); i++) {
50  if (!(i->isValid()))
51  throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add invalid state";
52  // weights
53  sum += i->weight();
54  // check on p_z
55  if (!theStates.empty() && pzFirst * i->localParameters().pzSign() < 0.)
56  thePzError = true;
57  }
58  theValidWeightSum += sum;
59  //
60  // add to vector of states
61  //
62  theStates.insert(theStates.end(), states.begin(), states.end());
63 }
64 
66  //
67  // change status of combination (contains at least one invalid state)
68  //
70 }
71 
73  //
74  // Prepare resulting state vector
75  //
76  if (!prepareCombinedState())
77  return TSOS();
78  //
79  // If invalid states in input: use reweighting
80  //
81  if (theInvalidWeightSum > 0.)
83  //
84  // Return new multi state without reweighting
85  //
87 }
88 
90  //
91  // Prepare resulting state vector
92  //
93  if (!prepareCombinedState())
94  return TSOS();
95  //
96  // return reweighted state
97  //
98  return reweightedCombinedState(newWeight);
99 }
100 
102  //
103  // Protect against empty combination (no valid input state)
104  //
105  if (invalidCombinedState())
106  return false;
107  //
108  // Check for states with wrong pz
109  //
110  if (thePzError)
111  removeWrongPz();
112  //
113  // Check for minimum fraction of valid states
114  //
115  double allWeights(theValidWeightSum + theInvalidWeightSum);
116  if (theInvalidWeightSum > 0. && theValidWeightSum < minValidFraction * allWeights)
117  return false;
118  //
119  // remaining part to be done only once
120  //
121  if (combinationDone)
122  return true;
123  combinationDone = true;
124  //
125  // Remove states with negligible weights
126  //
128  if (invalidCombinedState())
129  return false;
130  //
131  // Sort output by weights?
132  //
133  if (sortStates)
135 
136  return true;
137 }
138 
140  //
141  // check status
142  //
143  if (invalidCombinedState())
144  return TSOS();
145  //
146  // scaling factor
147  //
148  double factor = theValidWeightSum > 0. ? newWeight / theValidWeightSum : 1;
149  //
150  // create new vector of states & combined state
151  //
152  MultiTSOS reweightedStates;
153  reweightedStates.reserve(theStates.size());
154  for (auto const &is : theStates) {
155  auto oldWeight = is.weight();
156  reweightedStates.emplace_back(factor * oldWeight,
157  is.localParameters(),
158  is.localError(),
159  is.surface(),
160  &(is.globalParameters().magneticField()),
161  is.surfaceSide());
162  }
163  return TSOS((BasicTrajectoryState *)(new BasicMultiTrajectoryState(reweightedStates)));
164 }
165 
167  //
168  // check total weight
169  //
171  if (totalWeight == 0.) {
172  theStates.clear();
173  return;
174  }
175  theStates.erase(
176  std::remove_if(theStates.begin(),
177  theStates.end(),
178  [&](MultiTSOS::value_type const &s) { return s.weight() < minFractionalWeight * totalWeight; }),
179  theStates.end());
180 }
181 
183  LogDebug("GsfTrackFitters") << "MultiTrajectoryStateAssembler: found at least one state with inconsistent pz\n"
184  << " #state / weights before cleaning = " << theStates.size() << " / "
185  << theValidWeightSum << " / " << theInvalidWeightSum;
186  //
187  // Calculate average pz
188  //
189  double meanPz(0.);
190  for (auto const &is : theStates)
191  meanPz += is.weight() * is.localParameters().pzSign();
192  meanPz /= theValidWeightSum;
193  //
194  // Now keep only states compatible with the average pz
195  //
196  theValidWeightSum = 0.;
197  MultiTSOS oldStates(theStates);
198  theStates.clear();
199  for (auto const &is : oldStates) {
200  if (meanPz * is.localParameters().pzSign() >= 0.) {
201  theValidWeightSum += is.weight();
202  theStates.push_back(is);
203  } else {
204  theInvalidWeightSum += is.weight();
205  LogDebug("GsfTrackFitters") << "removing weight / pz / global position = " << is.weight() << " "
206  << is.localParameters().pzSign() << " " << is.globalPosition();
207  }
208  }
209  LogDebug("GsfTrackFitters") << " #state / weights after cleaning = " << theStates.size() << " / "
210  << theValidWeightSum << " / " << theInvalidWeightSum;
211 }
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
TrajectoryStateLessWeight.h
BasicMultiTrajectoryState
Definition: BasicMultiTrajectoryState.h:17
MultiTrajectoryStateAssembler::minValidFraction
float minValidFraction
Definition: MultiTrajectoryStateAssembler.h:68
mps_merge.weight
weight
Definition: mps_merge.py:88
generateEDF.totalWeight
totalWeight
Definition: generateEDF.py:687
MultiTrajectoryStateAssembler::reweightedCombinedState
TrajectoryStateOnSurface reweightedCombinedState(const double) const
Definition: MultiTrajectoryStateAssembler.cc:139
MultiTrajectoryStateAssembler::addState
void addState(const TrajectoryStateOnSurface)
Definition: MultiTrajectoryStateAssembler.cc:18
MultiTrajectoryStateAssembler::sortStates
bool sortStates
Definition: MultiTrajectoryStateAssembler.h:67
MultiTrajectoryStateAssembler::addStateVector
void addStateVector(const MultiTSOS &)
Definition: MultiTrajectoryStateAssembler.cc:37
GetComponents.h
MultiTrajectoryStateAssembler::theInvalidWeightSum
double theInvalidWeightSum
Definition: MultiTrajectoryStateAssembler.h:75
TrajectoryStateOnSurface
Definition: TrajectoryStateOnSurface.h:16
alignCSCRings.s
s
Definition: alignCSCRings.py:92
GetComponents
Definition: GetComponents.h:4
MultiTrajectoryStateAssembler::removeSmallWeights
void removeSmallWeights()
Definition: MultiTrajectoryStateAssembler.cc:166
TrajectoryStateLessWeight
Definition: TrajectoryStateLessWeight.h:10
MultiTrajectoryStateAssembler::invalidCombinedState
bool invalidCombinedState() const
Checks status of combined state.
Definition: MultiTrajectoryStateAssembler.h:47
MultiTrajectoryStateAssembler::removeWrongPz
void removeWrongPz()
Removes states with local p_z != average p_z.
Definition: MultiTrajectoryStateAssembler.cc:182
MultiTrajectoryStateAssembler::thePzError
bool thePzError
Definition: MultiTrajectoryStateAssembler.h:72
DQMScaleToClient_cfi.factor
factor
Definition: DQMScaleToClient_cfi.py:8
MultiTrajectoryStateAssembler.h
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
MultiTrajectoryStateAssembler::addInvalidState
void addInvalidState(const double)
Adds (the weight of an) invalid state to the list.
Definition: MultiTrajectoryStateAssembler.cc:65
MultiTrajectoryStateAssembler::minFractionalWeight
float minFractionalWeight
Definition: MultiTrajectoryStateAssembler.h:69
MultiTrajectoryStateAssembler::theValidWeightSum
double theValidWeightSum
Definition: MultiTrajectoryStateAssembler.h:74
MultiTrajectoryStateAssembler::combinationDone
bool combinationDone
Definition: MultiTrajectoryStateAssembler.h:71
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
BasicTrajectoryState
Definition: BasicTrajectoryState.h:66
MultiTrajectoryStateAssembler::theStates
MultiTSOS theStates
Definition: MultiTrajectoryStateAssembler.h:76
makeMuonMisalignmentScenario.components
string components
Definition: makeMuonMisalignmentScenario.py:58
Exception
Definition: hltDiff.cc:245
MultiTrajectoryStateAssembler::TSOS
TrajectoryStateOnSurface TSOS
Definition: MultiTrajectoryStateAssembler.h:15
Exception.h
MultiTrajectoryStateAssembler::MultiTSOS
std::vector< TrajectoryStateOnSurface > MultiTSOS
Definition: MultiTrajectoryStateAssembler.h:16
MultiTrajectoryStateAssembler::MultiTrajectoryStateAssembler
MultiTrajectoryStateAssembler()
Definition: MultiTrajectoryStateAssembler.cc:8
MultiTrajectoryStateAssembler::prepareCombinedState
bool prepareCombinedState()
Preparation of combined state (cleaning & sorting)
Definition: MultiTrajectoryStateAssembler.cc:101
cms::Exception
Definition: Exception.h:70
MultiTrajectoryStateAssembler::combinedState
TrajectoryStateOnSurface combinedState()
Definition: MultiTrajectoryStateAssembler.cc:72
BasicMultiTrajectoryState.h
weight
Definition: weight.py:1
TrajectoryStateOnSurface::isValid
bool isValid() const
Definition: TrajectoryStateOnSurface.h:54