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 }
TrajectoryStateOnSurface reweightedCombinedState(const double) const
void addState(const TrajectoryStateOnSurface)
Definition: weight.py:1
bool invalidCombinedState() const
Checks status of combined state.
void removeWrongPz()
Removes states with local p_z != average p_z.
void addInvalidState(const double)
Adds (the weight of an) invalid state to the list.
std::vector< TrajectoryStateOnSurface > MultiTSOS
bool prepareCombinedState()
Preparation of combined state (cleaning & sorting)
#define LogDebug(id)