CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GsfBetheHeitlerUpdator.cc
Go to the documentation of this file.
2 
6 
7 #include <string>
8 #include <fstream>
9 #include <cmath>
10 
12  const int correctionFlag) :
13  // const CorrectionFlag correctionFlag) :
14  GsfMaterialEffectsUpdator(0.000511),
15  theNrComponents(0),
16  theCorrectionFlag(correctionFlag),
17  theLastDz(0.),
18  theLastP(-1.),
19  theLastPropDir(alongMomentum),
20  theLastRadLength(-1.)
21 {
22  if ( theCorrectionFlag==1 )
23  edm::LogInfo("GsfBetheHeitlerUpdator") << "1st moment of mixture will be corrected";
24  if ( theCorrectionFlag>=2 )
25  edm::LogInfo("GsfBetheHeitlerUpdator")
26  << "1st and 2nd moments of mixture will be corrected";
27 
28  readParameters(fileName);
29 }
30 
32 {
33  std::string name = "TrackingTools/GsfTracking/data/";
34  name += fileName;
35 
36  edm::FileInPath parFile(name);
37  edm::LogInfo("GsfBetheHeitlerUpdator") << "Reading GSF parameterization "
38  << "of Bethe-Heitler energy loss from "
39  << parFile.fullPath();
40  std::ifstream ifs(parFile.fullPath().c_str());
41 
42  ifs >> theNrComponents;
43  int orderP;
44  ifs >> orderP;
45  ifs >> theTransformationCode;
46  for ( int ic=0; ic<theNrComponents; ic++ ) {
47  thePolyWeights.push_back(readPolynomial(ifs,orderP));
48  thePolyMeans.push_back(readPolynomial(ifs,orderP));
49  thePolyVars.push_back(readPolynomial(ifs,orderP));
50  }
51 }
52 
54 GsfBetheHeitlerUpdator::readPolynomial (std::ifstream& aStream,
55  const int order) {
56  std::vector<double> coeffs(order+1);
57  for ( int i=0; i<(order+1); i++ ) aStream >> coeffs[i];
58  return Polynomial(coeffs);
59 }
60 
61 void
63  const PropagationDirection propDir) const
64 {
65  //
66  // clear cache
67  //
68  theWeights.clear();
69  theDeltaPs.clear();
70  theDeltaCovs.clear();
71  //
72  // Get surface and check presence of medium properties
73  //
74  const Surface& surface = TSoS.surface();
75  //
76  // calculate components: first check associated material constants
77  //
78  double rl(0.);
79  double p(0.);
80  if ( surface.mediumProperties() ) {
81  LocalVector pvec = TSoS.localMomentum();
82  p = pvec.mag();
83  rl = surface.mediumProperties()->radLen()/fabs(pvec.z())*p;
84  }
85  //
86  // produce multi-state only in case of x/X0>0
87  //
88  if ( rl>0.0001 ) {
89  //
90  // limit x/x0 to valid range for parametrisation
91  // should be done in a more elegant way ...
92  //
93  if ( rl<0.01 ) rl = 0.01;
94  if ( rl>0.20 ) rl = 0.20;
95 
96  GSContainer mixture;
97  getMixtureParameters(rl,mixture);
98  correctWeights(mixture);
99  if ( theCorrectionFlag>=1 )
100  mixture[0].second = correctedFirstMean(rl,mixture);
101  if ( theCorrectionFlag>=2 )
102  mixture[0].third = correctedFirstVar(rl,mixture);
103 
104  for ( int i=0; i<theNrComponents; i++ ) {
105  double varPinv;
106  theWeights.push_back(mixture[i].first);
107  if ( propDir==alongMomentum ) {
108  //
109  // for forward propagation: calculate in p (linear in 1/z=p_inside/p_outside),
110  // then convert sig(p) to sig(1/p).
111  //
112  theDeltaPs.push_back(p*(mixture[i].second-1));
113  // double f = 1./p/mixture[i].second/mixture[i].second;
114  // patch to ensure consistency between for- and backward propagation
115  double f = 1./p/mixture[i].second;
116  varPinv = f*f*mixture[i].third;
117  }
118  else {
119  //
120  // for backward propagation: delta(1/p) is linear in z=p_outside/p_inside
121  // convert to obtain equivalent delta(p)
122  //
123  theDeltaPs.push_back(p*(1/mixture[i].second-1));
124  varPinv = mixture[i].third/p/p;
125  }
127  errors(0,0) = varPinv;
128  theDeltaCovs.push_back(errors);
129  }
130  }
131  else {
132  theWeights.push_back(1.);
133  theDeltaPs.push_back(0.);
134  theDeltaCovs.push_back(AlgebraicSymMatrix55());
135  }
136  //
137  // Save arguments to avoid duplication of computation
138  //
139  storeArguments(TSoS,propDir);
140 }
141 //
142 // Mixture parameters (in z)
143 //
144 void
146  GSContainer& mixture) const
147 {
148  mixture.clear();
149  mixture.reserve(theNrComponents);
150 
151  for ( int i=0; i<theNrComponents; i++ ) {
152 
153  double weight = thePolyWeights[i](rl);
154  if ( theTransformationCode ) weight = logisticFunction(weight);
155 
156  double z = thePolyMeans[i](rl);
158 
159  double vz = thePolyVars[i](rl);
160  if ( theTransformationCode ) vz = exp(vz);
161  else vz = vz*vz;
162 
163  mixture.push_back(Triplet<double,double,double>(weight,z,vz));
164  }
165 }
166 
167 //
168 // Correct weights
169 //
170 void
172 {
173  if ( mixture.empty() ) return;
174  //
175  // get sum of weights
176  //
177  double wsum(0);
178  for ( GSContainer::const_iterator i=mixture.begin();
179  i!=mixture.end(); i++ ) wsum += (*i).first;
180  //
181  // rescale to obtain 1
182  //
183  for ( GSContainer::iterator i=mixture.begin();
184  i!=mixture.end(); i++ ) (*i).first /= wsum;
185 }
186 //
187 // Correct means
188 //
189 double
191  const GSContainer& mixture) const
192 {
193  if ( mixture.empty() ) return 0.;
194  //
195  // calculate difference true mean - weighted sum
196  //
197  double mean = BetheHeitlerMean(rl);
198  for ( GSContainer::const_iterator i=mixture.begin()+1;
199  i!=mixture.end(); i++ ) mean -= (*i).first*(*i).second;
200  //
201  // return corrected mean for first component
202  //
203  return std::max(std::min(mean/mixture[0].first,1.),0.);
204 }
205 //
206 // Correct variances
207 //
208 double
210  const GSContainer& mixture) const
211 {
212  if ( mixture.empty() ) return 0.;
213  //
214  // calculate difference true variance - weighted sum
215  //
216  double var = BetheHeitlerVariance(rl) +
218  mixture[0].first*mixture[0].second*mixture[0].second;
219  for ( GSContainer::const_iterator i=mixture.begin()+1;
220  i!=mixture.end(); i++ )
221  var -= (*i).first*((*i).second*(*i).second+(*i).third);
222  //
223  // return corrected variance for first component
224  //
225  return std::max(var/mixture[0].first,0.);
226 }
227 
228 //
229 // Compare arguments with the ones of the previous call
230 //
231 bool
233  const PropagationDirection propDir) const {
234  return TSoS.localMomentum().unit().z()!=theLastDz ||
235  TSoS.localMomentum().mag()!=theLastP || propDir!=theLastPropDir ||
237 }
238 //
239 // Save arguments
240 //
242  const PropagationDirection propDir) const {
243  theLastDz = TSoS.localMomentum().unit().z();
244  theLastP = TSoS.localMomentum().mag();
245  theLastPropDir = propDir;
247 }
virtual void storeArguments(const TrajectoryStateOnSurface &, const PropagationDirection) const
storage of arguments for later use of
int i
Definition: DBlmapReader.cc:9
std::vector< Polynomial > thePolyVars
parametrisation of mean for each component
float radLen() const
std::vector< AlgebraicSymMatrix55 > theDeltaCovs
std::vector< Polynomial > thePolyMeans
parametrisation of weight for each component
Polynomial readPolynomial(std::ifstream &, const int)
Read coefficients of one polynomial from file.
float theLastDz
correction of 1st or 1st&amp;2nd moments
void correctWeights(GSContainer &) const
Correction for weight of component 1.
double correctedFirstVar(const double, const GSContainer &) const
Correction for variance of component 1.
std::vector< Polynomial > thePolyWeights
values to be transformed by logistic / exp. function?
void getMixtureParameters(const double, GSContainer &) const
Filling of mixture (in terms of z=E/E0)
#define min(a, b)
Definition: mlp_lapack.h:161
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
PropagationDirection
virtual void compute(const TrajectoryStateOnSurface &, const PropagationDirection) const
Computation: generates vectors of weights, means and standard deviations.
Definition: Triplet.h:9
virtual bool newArguments(const TrajectoryStateOnSurface &, const PropagationDirection) const
check of arguments for use with cached values
double double double z
double BetheHeitlerMean(const double rl) const
First moment of the Bethe-Heitler distribution (in z=E/E0)
const MediumProperties * mediumProperties() const
Definition: Surface.h:93
LocalVector localMomentum() const
U second(std::pair< T, U > const &p)
T mag() const
Definition: PV3DBase.h:66
std::vector< Triplet< double, double, double > > GSContainer
double BetheHeitlerVariance(const double rl) const
Second moment of the Bethe-Heitler distribution (in z=E/E0)
const T & max(const T &a, const T &b)
T z() const
Definition: PV3DBase.h:63
double logisticFunction(const double x) const
Logistic function (needed for transformation of weight and mean)
double f[11][100]
int theTransformationCode
number of components used for parameterisation
bool first
Definition: L1TdeRCT.cc:94
PropagationDirection theLastPropDir
Vector3DBase unit() const
Definition: Vector3DBase.h:57
GsfBetheHeitlerUpdator(const std::string fileName, const int correctionFlag)
constructor with explicit filename and correction flag
int theCorrectionFlag
parametrisation of variance for each component
double correctedFirstMean(const double, const GSContainer &) const
Correction for mean of component 1.
const Surface & surface() const
void readParameters(const std::string)
Read parametrization from file.
std::string fullPath() const
Definition: FileInPath.cc:171