CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CrossSectionHandler.h
Go to the documentation of this file.
1 
18 #include <vector>
19 #include <numeric>
20 
21 #include "TString.h"
22 #include "TMinuit.h"
23 
25 {
26 public:
27  CrossSectionHandler(const std::vector<double> & crossSection, const std::vector<int> & resfind) :
28  parNum_(0),
29  numberOfResonances_(resfind.size())
30  {
31  // The number of parameters is the number of fitted resonances minus 1
32  std::vector<int>::const_iterator it = resfind.begin();
33  for( ; it != resfind.end(); ++it ) {
34  if( *it != 0 ) ++parNum_;
35  }
36  if( parNum_ > 0 ) parNum_ = parNum_ - 1;
37 
38  vars_.resize(parNum_);
39 
40  computeRelativeCrossSections(crossSection, resfind);
42  }
43 
45  void addParameters(std::vector<double> & initpar)
46  {
47  std::vector<double>::const_iterator it = vars_.begin();
48  for( ; it != vars_.end(); ++it ) {
49  initpar.push_back(*it);
50  }
51  }
52 
54  void setParameters( double* Start, double* Step, double* Mini, double* Maxi, int* ind, TString* parname,
55  const std::vector<double> & parCrossSection, const std::vector<int> & parCrossSectionOrder,
56  const std::vector<int> & resfind )
57  {
58  computeRelativeCrossSections(parCrossSection, resfind);
60 
61  double thisStep[] = {0.001, 0.001, 0.001, 0.001, 0.001};
62  TString thisParName[] = {"cross section var 1",
63  "cross section var 2",
64  "cross section var 3",
65  "cross section var 4",
66  "cross section var 5"};
67  double thisMini[] = {0., 0., 0., 0., 0.};
68  double thisMaxi[] = {1000., 1000., 1000., 1000., 1000.};
69 
70  // This is used to unlock the parameters in a given order. It is not
71  // a TMinuit parameter, but a MuScleFit parameter.
72  for( unsigned int iPar=0; iPar<numberOfResonances_; ++iPar ) {
73  ind[iPar] = parCrossSectionOrder[iPar];
74  }
75 
76  if( parNum_ > 0 ) {
77  for( unsigned int iPar=0; iPar<parNum_; ++iPar ) {
78  Start[iPar] = vars_[iPar];
79  Step[iPar] = thisStep[iPar];
80  Mini[iPar] = thisMini[iPar];
81  Maxi[iPar] = thisMaxi[iPar];
82  parname[iPar] = thisParName[iPar];
83  }
84  }
85  }
86 
88  bool releaseParameters( TMinuit & rmin, const std::vector<int> & resfind, const std::vector<int> & parfix,
89  const int * ind, const int iorder, const unsigned int shift )
90  {
91  // Find the number of free cross section parameters in this iteration
92  unsigned int freeParNum = 0;
93  for( unsigned int ipar=0; ipar<numberOfResonances_; ++ipar ) {
94  if( (parfix[shift+ipar]==0) && (ind[shift+ipar]<=iorder) && (resfind[ipar] == 1) ) {
95  ++freeParNum;
96  }
97  }
98  if( freeParNum > 0 ) {
99  freeParNum = freeParNum - 1;
100  // Free only the first (freeParNum-1) of the N-1 variables
101  for( unsigned int i=0; i<freeParNum; ++i ) {
102  rmin.Release( shift+i );
103  }
104  return true;
105  }
106  return false;
107  }
108 
109  inline unsigned int parNum()
110  {
111  return parNum_;
112  }
113 
115  std::vector<double> relativeCrossSections( const double * variables, const std::vector<int> & resfind )
116  {
117  // parNum_ is 0 in two cases:
118  // 1) if only one resonance is being fitted, in which case the relative cross section is
119  // fixed to one and there is no need to recompute it
120  // 2) if no resonance is being fitted, in which case all the relative cross sections will
121  // be set to 0.
122  // In both cases there is no need to make the transformation of variables.
123  if( parNum_ != 0 ) {
124  double * partialProduct = new double[numberOfResonances_];
125  double norm = 0.;
126  // Loop on all relative cross sections (that are parNum_+1)
127  for( unsigned int i=0; i<parNum_+1; ++i ) {
128  partialProduct[i] = std::accumulate(variables, variables + i, 1., std::multiplies<double>());
129  norm += partialProduct[i];
130  }
131  for( unsigned int i=0; i<parNum_+1; ++i ) {
132  relativeCrossSectionVec_[i] = partialProduct[i]/norm;
133  }
134  delete[] partialProduct;
135  }
136 
137  std::vector<double> allRelativeCrossSections;
138  std::vector<int>::const_iterator it = resfind.begin();
139  int smallerVectorIndex = 0;
140  for( ; it != resfind.end(); ++it ) {
141  if( *it == 0 ) {
142  allRelativeCrossSections.push_back(0.);
143  }
144  else {
145  allRelativeCrossSections.push_back(relativeCrossSectionVec_[smallerVectorIndex]);
146  ++smallerVectorIndex;
147  }
148  }
149 
150  return allRelativeCrossSections;
151  }
152 
153 protected:
159  // void computeRelativeCrossSections(const double crossSection[], const std::vector<int> resfind, const unsigned int minRes, const unsigned int maxRes)
160  void computeRelativeCrossSections(const std::vector<double> & crossSection, const std::vector<int> & resfind)
161  {
162  relativeCrossSectionVec_.clear();
163  double normalization = 0.;
164  for( unsigned int ires = 0; ires < resfind.size(); ++ires ) {
165  if( resfind[ires] ) {
166  normalization += crossSection[ires];
167  }
168  }
169  if( normalization != 0. ) {
170  for( unsigned int ires = 0; ires < resfind.size(); ++ires ) {
171  if( resfind[ires] ) {
172  relativeCrossSectionVec_.push_back(crossSection[ires]/normalization);
173  }
174  }
175  }
176  }
177 
180  {
181  if( parNum_ > 0 ) {
182  for( unsigned int iVar = 0; iVar < parNum_; ++iVar ) {
184  }
185  }
186  }
187 
188  // Data members
189  std::vector<double> relativeCrossSectionVec_;
190  std::vector<double> vars_;
191  unsigned int parNum_;
192  unsigned int numberOfResonances_;
193 };
int i
Definition: DBlmapReader.cc:9
void setParameters(double *Start, double *Step, double *Mini, double *Maxi, int *ind, TString *parname, const std::vector< double > &parCrossSection, const std::vector< int > &parCrossSectionOrder, const std::vector< int > &resfind)
Initializes the arrays needed by Minuit.
int ires[2]
void computeRelativeCrossSections(const std::vector< double > &crossSection, const std::vector< int > &resfind)
std::vector< double > vars_
std::vector< double > relativeCrossSectionVec_
bool releaseParameters(TMinuit &rmin, const std::vector< int > &resfind, const std::vector< int > &parfix, const int *ind, const int iorder, const unsigned int shift)
Use the information in resfind, parorder and parfix to release the N-1 variables. ...
unsigned int numberOfResonances_
void imposeConstraint()
Change of variables so that we move from N to N-1 variables using the constraint that Sum(x_i) = 1...
std::vector< double > relativeCrossSections(const double *variables, const std::vector< int > &resfind)
Perform a variable transformation from N-1 to relative cross sections.
CrossSectionHandler(const std::vector< double > &crossSection, const std::vector< int > &resfind)
int iorder
static unsigned int const shift
tuple size
Write out results.
void addParameters(std::vector< double > &initpar)
Inputs the vars in a vector.