Go to the documentation of this file.00001 #include "TrackingTools/GsfTools/interface/MultiTrajectoryStateAssembler.h"
00002
00003 #include "TrackingTools/GsfTools/interface/BasicMultiTrajectoryState.h"
00004 #include "TrackingTools/GsfTools/src/TrajectoryStateLessWeight.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include "FWCore/Utilities/interface/Exception.h"
00007
00008 MultiTrajectoryStateAssembler::MultiTrajectoryStateAssembler () :
00009 combinationDone(false),
00010 thePzError(false),
00011 theValidWeightSum(0.),
00012 theInvalidWeightSum(0.)
00013 {
00014
00015
00016
00017 sortStates = false;
00018 minValidFraction = 0.01;
00019 minFractionalWeight = 1.e-6;
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 }
00032
00033 void MultiTrajectoryStateAssembler::addState (const TrajectoryStateOnSurface tsos) {
00034
00035
00036
00037
00038
00039 if ( combinationDone )
00040 throw cms::Exception("LogicError")
00041 << "MultiTrajectoryStateAssembler: trying to add states after combination";
00042
00043
00044
00045 if ( !tsos.isValid() )
00046 throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add invalid state";
00047
00048
00049
00050 MultiTSOS components(tsos.components());
00051 addStateVector(components);
00052 }
00053
00054 void MultiTrajectoryStateAssembler::addStateVector (const MultiTSOS& states)
00055 {
00056
00057
00058
00059 if ( combinationDone )
00060 throw cms::Exception("LogicError")
00061 << "MultiTrajectoryStateAssembler: trying to add states after combination";
00062
00063
00064
00065
00066 double sum(0.);
00067 double pzFirst = theStates.empty() ? 0. : theStates.front().localParameters().pzSign();
00068 for ( MultiTSOS::const_iterator i=states.begin();
00069 i!=states.end(); i++ ) {
00070 if ( !(i->isValid()) )
00071 throw cms::Exception("LogicError")
00072 << "MultiTrajectoryStateAssembler: trying to add invalid state";
00073
00074 sum += i->weight();
00075
00076 if ( !theStates.empty() &&
00077 pzFirst*i->localParameters().pzSign()<0. ) thePzError = true;
00078 }
00079 theValidWeightSum += sum;
00080
00081
00082
00083 theStates.insert(theStates.end(),states.begin(),states.end());
00084 }
00085
00086
00087 void MultiTrajectoryStateAssembler::addInvalidState (const double weight) {
00088
00089
00090
00091 theInvalidWeightSum += weight;
00092 }
00093
00094 TrajectoryStateOnSurface MultiTrajectoryStateAssembler::combinedState () {
00095
00096
00097
00098
00099
00100 if ( !prepareCombinedState() ) return TSOS();
00101
00102
00103
00104 if ( theInvalidWeightSum>0. )
00105 return reweightedCombinedState(theValidWeightSum+theInvalidWeightSum);
00106
00107
00108
00109 return TSOS(new BasicMultiTrajectoryState(theStates));
00110 }
00111
00112 TrajectoryStateOnSurface MultiTrajectoryStateAssembler::combinedState (const float newWeight) {
00113
00114
00115
00116
00117
00118 if ( !prepareCombinedState() ) return TSOS();
00119
00120
00121
00122 return reweightedCombinedState(newWeight);
00123 }
00124
00125 bool
00126 MultiTrajectoryStateAssembler::prepareCombinedState () {
00127
00128
00129
00130 if ( invalidCombinedState() ) return false;
00131
00132
00133
00134 if ( thePzError ) removeWrongPz();
00135
00136
00137
00138 double allWeights(theValidWeightSum+theInvalidWeightSum);
00139 if ( theInvalidWeightSum>0. && (theValidWeightSum/allWeights)<minValidFraction ) return false;
00140
00141
00142
00143 if ( combinationDone ) return true;
00144 else combinationDone = true;
00145
00146
00147
00148 removeSmallWeights();
00149 if ( invalidCombinedState() ) return false;
00150
00151
00152
00153 if ( sortStates )
00154 sort(theStates.begin(),theStates.end(),TrajectoryStateLessWeight());
00155
00156 return true;
00157 }
00158
00159 TrajectoryStateOnSurface
00160 MultiTrajectoryStateAssembler::reweightedCombinedState (const double newWeight) const {
00161
00162
00163
00164 if ( invalidCombinedState() ) return TSOS();
00165
00166
00167
00168 double factor = theValidWeightSum>0. ? newWeight/theValidWeightSum : 1;
00169
00170
00171
00172 MultiTSOS reweightedStates;
00173 reweightedStates.reserve(theStates.size());
00174 for ( MultiTSOS::const_iterator i=theStates.begin();
00175 i!=theStates.end(); i++ ) {
00176 double oldWeight = i->weight();
00177 reweightedStates.push_back(TrajectoryStateOnSurface(i->localParameters(),
00178 i->localError(),
00179 i->surface(),
00180 &(i->globalParameters().magneticField()),
00181 i->surfaceSide(),
00182 factor*oldWeight));
00183 }
00184 return TSOS(new BasicMultiTrajectoryState(reweightedStates));
00185 }
00186
00187 void
00188 MultiTrajectoryStateAssembler::removeSmallWeights()
00189 {
00190
00191
00192
00193 double totalWeight(theInvalidWeightSum+theValidWeightSum);
00194 if ( totalWeight == 0. ) {
00195 theStates.clear();
00196 return;
00197 }
00198
00199
00200
00201 bool redo;
00202 do {
00203 redo = false;
00204 for ( MultiTSOS::iterator i=theStates.begin();
00205 i!=theStates.end(); i++ ) {
00206 if ( (*i).weight()/totalWeight < minFractionalWeight ) {
00207 theStates.erase(i);
00208 redo = true;
00209 break;
00210 }
00211 }
00212 } while (redo);
00213 }
00214
00215 void
00216 MultiTrajectoryStateAssembler::removeWrongPz () {
00217
00218
00219
00220
00221
00222
00223
00224
00225 double meanPz(0.);
00226 for ( MultiTSOS::const_iterator is=theStates.begin();
00227 is!=theStates.end(); is++ ) {
00228 meanPz += is->weight()*is->localParameters().pzSign();
00229
00230
00231
00232
00233 }
00234 meanPz /= theValidWeightSum;
00235
00236
00237
00238
00239 theValidWeightSum = 0.;
00240 MultiTSOS oldStates(theStates);
00241 theStates.clear();
00242 for ( MultiTSOS::const_iterator is=oldStates.begin();
00243 is!=oldStates.end(); is++ ) {
00244 if ( meanPz*is->localParameters().pzSign()>=0. ) {
00245 theValidWeightSum += is->weight();
00246 theStates.push_back(*is);
00247 }
00248 else {
00249 theInvalidWeightSum += is->weight();
00250 }
00251 }
00252
00253
00254
00255
00256 }
00257
00258
00259