CMS 3D CMS Logo

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