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(i->localParameters(),
178  i->localError(),
179  i->surface(),
180  &(i->globalParameters().magneticField()),
181  i->surfaceSide(),
182  factor*oldWeight));
183  }
184  return TSOS(new BasicMultiTrajectoryState(reweightedStates));
185 }
186 
187 void
189 {
190  //
191  // check total weight
192  //
193  double totalWeight(theInvalidWeightSum+theValidWeightSum);
194  if ( totalWeight == 0. ) {
195  theStates.clear();
196  return;
197  }
198  //
199  // Loop until no more states are removed
200  //
201  bool redo;
202  do {
203  redo = false;
204  for ( MultiTSOS::iterator i=theStates.begin();
205  i!=theStates.end(); i++ ) {
206  if ( (*i).weight()/totalWeight < minFractionalWeight ) {
207  theStates.erase(i);
208  redo = true;
209  break;
210  }
211  }
212  } while (redo);
213 }
214 
215 void
217  // edm::LogDebug("MultiTrajectoryStateAssembler")
218  // << "MultiTrajectoryStateAssembler: found at least one state with inconsistent pz\n"
219  // << " #state / weights before cleaning = " << theStates.size()
220  // << " / " << theValidWeightSum
221  // << " / " << theInvalidWeightSum;
222  //
223  // Calculate average pz
224  //
225  double meanPz(0.);
226  for ( MultiTSOS::const_iterator is=theStates.begin();
227  is!=theStates.end(); is++ ) {
228  meanPz += is->weight()*is->localParameters().pzSign();
229  // edm::LogDebug("MultiTrajectoryStateAssembler")
230  // << " weight / pz / global position = " << is->weight()
231  // << " " << is->localParameters().pzSign()
232  // << " " << is->globalPosition();
233  }
234  meanPz /= theValidWeightSum;
235  //
236  // Now keep only states compatible with the average pz
237  //
238  // double oldValidWeight(theValidWeightSum);
239  theValidWeightSum = 0.;
240  MultiTSOS oldStates(theStates);
241  theStates.clear();
242  for ( MultiTSOS::const_iterator is=oldStates.begin();
243  is!=oldStates.end(); is++ ) {
244  if ( meanPz*is->localParameters().pzSign()>=0. ) {
245  theValidWeightSum += is->weight();
246  theStates.push_back(*is);
247  }
248  else {
249  theInvalidWeightSum += is->weight();
250  }
251  }
252  // edm::LogDebug("MultiTrajectoryStateAssembler")
253  // << " #state / weights after cleaning = " << theStates.size()
254  // << " / " << theValidWeightSum
255  // << " / " << theInvalidWeightSum;
256 }
257 
258 // TimingReport::Item * MultiTrajectoryStateAssembler::theTimerAdd(0);
259 // 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
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.