CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MultiTrajectoryStateAssembler.cc
Go to the documentation of this file.
2 
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;
20  // //
21  // // Timers
22  // //
23  // if ( theTimerAdd==0 ) {
24  // theTimerAdd =
25  // &(*TimingReport::current())[string("MultiTrajectoryStateAssembler::addState")];
26  // theTimerAdd->switchCPU(false);
27  // theTimerComb =
28  // &(*TimingReport::current())[string("MultiTrajectoryStateAssembler::combinedState")];
29  // theTimerComb->switchCPU(false);
30  // }
31 }
32 
34  // // Timer
35  // TimeMe t(*theTimerAdd,false);
36  //
37  // refuse to add states after combination has been done
38  //
39  if ( combinationDone )
40  throw cms::Exception("LogicError")
41  << "MultiTrajectoryStateAssembler: trying to add states after combination";
42  //
43  // Verify validity of state to be added
44  //
45  if ( !tsos.isValid() )
46  throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add invalid state";
47  //
48  // Add components (i.e. state to be added can be single or multi state)
49  //
52 }
53 
55 {
56  //
57  // refuse to add states after combination has been done
58  //
59  if ( combinationDone )
60  throw cms::Exception("LogicError")
61  << "MultiTrajectoryStateAssembler: trying to add states after combination";
62  //
63  // sum up weights (all components are supposed to be valid!!!) and
64  // check for consistent pz
65  //
66  double sum(0.);
67  double pzFirst = theStates.empty() ? 0. : theStates.front().localParameters().pzSign();
68  for ( MultiTSOS::const_iterator i=states.begin();
69  i!=states.end(); i++ ) {
70  if ( !(i->isValid()) )
71  throw cms::Exception("LogicError")
72  << "MultiTrajectoryStateAssembler: trying to add invalid state";
73  // weights
74  sum += i->weight();
75  // check on p_z
76  if ( !theStates.empty() &&
77  pzFirst*i->localParameters().pzSign()<0. ) thePzError = true;
78  }
79  theValidWeightSum += sum;
80  //
81  // add to vector of states
82  //
83  theStates.insert(theStates.end(),states.begin(),states.end());
84 }
85 
86 
88  //
89  // change status of combination (contains at least one invalid state)
90  //
92 }
93 
95  // // Timer
96  // TimeMe t(*theTimerComb,false);
97  //
98  // Prepare resulting state vector
99  //
100  if ( !prepareCombinedState() ) return TSOS();
101  //
102  // If invalid states in input: use reweighting
103  //
104  if ( theInvalidWeightSum>0. )
106  //
107  // Return new multi state without reweighting
108  //
110 }
111 
113  // // Timer
114  // TimeMe t(*theTimerComb,false);
115  //
116  // Prepare resulting state vector
117  //
118  if ( !prepareCombinedState() ) return TSOS();
119  //
120  // return reweighted state
121  //
122  return reweightedCombinedState(newWeight);
123 }
124 
125 bool
127  //
128  // Protect against empty combination (no valid input state)
129  //
130  if ( invalidCombinedState() ) return false;
131  //
132  // Check for states with wrong pz
133  //
134  if ( thePzError ) removeWrongPz();
135  //
136  // Check for minimum fraction of valid states
137  //
138  double allWeights(theValidWeightSum+theInvalidWeightSum);
139  if ( theInvalidWeightSum>0. && (theValidWeightSum/allWeights)<minValidFraction ) return false;
140  //
141  // remaining part to be done only once
142  //
143  if ( combinationDone ) return true;
144  else combinationDone = true;
145  //
146  // Remove states with negligible weights
147  //
149  if ( invalidCombinedState() ) return false;
150  //
151  // Sort output by weights?
152  //
153  if ( sortStates )
155 
156  return true;
157 }
158 
161  //
162  // check status
163  //
164  if ( invalidCombinedState() ) return TSOS();
165  //
166  // scaling factor
167  //
168  double factor = theValidWeightSum>0. ? newWeight/theValidWeightSum : 1;
169  //
170  // create new vector of states & combined state
171  //
172  MultiTSOS reweightedStates;
173  reweightedStates.reserve(theStates.size());
174  for ( MultiTSOS::const_iterator i=theStates.begin();
175  i!=theStates.end(); i++ ) {
176  double oldWeight = i->weight();
177  reweightedStates.push_back(TrajectoryStateOnSurface(factor*oldWeight,
178  i->localParameters(),
179  i->localError(),
180  i->surface(),
181  &(i->globalParameters().magneticField()),
182  i->surfaceSide()
183  ));
184  }
185  return TSOS((BasicTrajectoryState *)(new BasicMultiTrajectoryState(reweightedStates)));
186 }
187 
188 void
190 {
191  //
192  // check total weight
193  //
194  double totalWeight(theInvalidWeightSum+theValidWeightSum);
195  if ( totalWeight == 0. ) {
196  theStates.clear();
197  return;
198  }
199  //
200  // Loop until no more states are removed
201  //
202  bool redo;
203  do {
204  redo = false;
205  for ( MultiTSOS::iterator i=theStates.begin();
206  i!=theStates.end(); i++ ) {
207  if ( (*i).weight()/totalWeight < minFractionalWeight ) {
208  theStates.erase(i);
209  redo = true;
210  break;
211  }
212  }
213  } while (redo);
214 }
215 
216 void
218  // edm::LogDebug("MultiTrajectoryStateAssembler")
219  // << "MultiTrajectoryStateAssembler: found at least one state with inconsistent pz\n"
220  // << " #state / weights before cleaning = " << theStates.size()
221  // << " / " << theValidWeightSum
222  // << " / " << theInvalidWeightSum;
223  //
224  // Calculate average pz
225  //
226  double meanPz(0.);
227  for ( MultiTSOS::const_iterator is=theStates.begin();
228  is!=theStates.end(); is++ ) {
229  meanPz += is->weight()*is->localParameters().pzSign();
230  // edm::LogDebug("MultiTrajectoryStateAssembler")
231  // << " weight / pz / global position = " << is->weight()
232  // << " " << is->localParameters().pzSign()
233  // << " " << is->globalPosition();
234  }
235  meanPz /= theValidWeightSum;
236  //
237  // Now keep only states compatible with the average pz
238  //
239  // double oldValidWeight(theValidWeightSum);
240  theValidWeightSum = 0.;
241  MultiTSOS oldStates(theStates);
242  theStates.clear();
243  for ( MultiTSOS::const_iterator is=oldStates.begin();
244  is!=oldStates.end(); is++ ) {
245  if ( meanPz*is->localParameters().pzSign()>=0. ) {
246  theValidWeightSum += is->weight();
247  theStates.push_back(*is);
248  }
249  else {
250  theInvalidWeightSum += is->weight();
251  }
252  }
253  // edm::LogDebug("MultiTrajectoryStateAssembler")
254  // << " #state / weights after cleaning = " << theStates.size()
255  // << " / " << theValidWeightSum
256  // << " / " << theInvalidWeightSum;
257 }
258 
259 // TimingReport::Item * MultiTrajectoryStateAssembler::theTimerAdd(0);
260 // TimingReport::Item * MultiTrajectoryStateAssembler::theTimerComb(0);
int i
Definition: DBlmapReader.cc:9
void addState(const TrajectoryStateOnSurface)
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
volatile std::atomic< bool > shutdown_flag false
std::vector< TrajectoryStateOnSurface > MultiTSOS
bool prepareCombinedState()
Preparation of combined state (cleaning &amp; sorting)
std::vector< TrajectoryStateOnSurface > components() const
bool invalidCombinedState() const
Checks status of combined state.