CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
BackgroundHandler Class Reference

#include <BackgroundHandler.h>

Public Member Functions

std::pair< double, double > backgroundFunction (const bool doBackgroundFit, const double *parval, const int resTotNum, const int ires, const bool *resConsidered, const double *ResMass, const double ResHalfWidth[], const int MuonType, const double &mass, const double &resEta)
 
 BackgroundHandler (const std::vector< int > &identifiers, const std::vector< double > &leftWindowBorders, const std::vector< double > &rightWindowBorders, const double *ResMass, const double *massWindowHalfWidth)
 
bool checkBackgroundWindow (const double &mass, const int iRegion)
 Check if the mass value is inside the given background region. More...
 
void countEventsInAllWindows (const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &muonPairs, const double &weight)
 
void initializeParNums ()
 Initialize the parNums to be used in the shifts of parval. More...
 
int regionsParNum ()
 Returns the total number of parameters used for the regions. More...
 
void rescale (std::vector< double > &parBgr, const double *ResMass, const double *massWindowHalfWidth, const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &muonPairs, const double &weight=1.)
 
double resMass (const bool doBackgroundFit, const int ires)
 
void setParameters (double *Start, double *Step, double *Mini, double *Maxi, int *ind, TString *parname, const std::vector< double > &parBgr, const std::vector< int > &parBgrOrder, const int muonType)
 Sets initial parameters for all the functions. More...
 
bool unlockParameter (const std::vector< int > &resfind, const unsigned int ipar)
 returns true if the parameter is to be unlocked More...
 
std::pair< double, double > windowBorders (const bool doBackgroundFit, const int ires)
 Returns the appropriate window borders depending on whether the background is being fitted and on the resonance. More...
 
 ~BackgroundHandler ()
 

Private Member Functions

void consistencyCheck (const std::vector< int > &identifiers, const std::vector< double > &leftWindowBorders, const std::vector< double > &rightWindowBorders) const throw (cms::Exception)
 Used to check the consistency of passed parameters. More...
 

Private Attributes

std::vector< MassWindowbackgroundWindow_
 
int parNumsRegions_ [3]
 
int parNumsResonances_ [6]
 
int regToResHW_ [3]
 
std::vector< MassWindowresonanceWindow_
 
int resToReg_ [6]
 

Detailed Description

This class is used to handle the different background functions for the different regions.
It uses the backgroundFunctions defined in Functions.h and the backgroundFunctionService defined in Functions.cc.
More details are in the description of backgroundFunctionBase in Functions.h.

A bool selects if to use the regions functions or the resonances functions.

Definition at line 19 of file BackgroundHandler.h.

Constructor & Destructor Documentation

BackgroundHandler::BackgroundHandler ( const std::vector< int > &  identifiers,
const std::vector< double > &  leftWindowBorders,
const std::vector< double > &  rightWindowBorders,
const double *  ResMass,
const double *  massWindowHalfWidth 
)

Definition at line 10 of file BackgroundHandler.cc.

References backgroundFunctionService(), backgroundWindow_, consistencyCheck(), i, getHLTprescales::index, initializeParNums(), regToResHW_, resonanceWindow_, and resToReg_.

15 {
16  // : leftWindowFactors_(leftWindowFactors),
17  // rightWindowFactors_(rightWindowFactors)
18 
19  // Define the correspondence between regions and halfWidth to use
20  // Defines also the function type to use (but they are checked to be consistent over a region)
21  regToResHW_[0] = 0; // Region 0 use the one from Z
22  regToResHW_[1] = 3; // Region 1 use the one from Upsilon1S
23  regToResHW_[2] = 5; // Region 2 use the one from J/Psi
24 
25  // Define the correspondence between resonances and regions
26  resToReg_[0] = 0; // Z
27  resToReg_[1] = 1; // Upsilon3S
28  resToReg_[2] = 1; // Upsilon2S
29  resToReg_[3] = 1; // Upsilon1S
30  resToReg_[4] = 2; // Psi2S
31  resToReg_[5] = 2; // J/Psi
32 
33  // Throws cms::Exception("Configuration") in case the parameters are not what is expected
34  consistencyCheck(identifiers, leftWindowBorders, rightWindowBorders);
35 
36  // Build the resonance windows
37  for( unsigned int i=0; i<6; ++i ) {
38  double mass = ResMass[i];
39  double lowerLimit = mass - massWindowHalfWidth[i];
40  double upperLimit = mass + massWindowHalfWidth[i];
41  resonanceWindow_.push_back( MassWindow( mass, lowerLimit, upperLimit,
42  std::vector<unsigned int>(1,i),
44  lowerLimit,
45  upperLimit) ) );
46  }
47 
48  // Build the background windows
49  // ----------------------------
50  // Compute the mass center of each region
51  double resMassForRegion[3];
52  resMassForRegion[0] = ResMass[0];
53  resMassForRegion[1] = (ResMass[1]+ResMass[2]+ResMass[3])/3;
54  resMassForRegion[2] = (ResMass[4]+ResMass[5])/2;
55 
56  // Define which resonance is in which background window
57  std::vector<std::vector<unsigned int> > indexes;
58  indexes.push_back(std::vector<unsigned int>(1,0));
59  indexes.push_back(std::vector<unsigned int>());
60  for( int i=1; i<=3; ++i ) { indexes[1].push_back(i); }
61  indexes.push_back(std::vector<unsigned int>());
62  for( int i=4; i<=5; ++i ) { indexes[2].push_back(i); }
63 
64  unsigned int i=0;
65  typedef std::vector<unsigned int> indexVec;
66  BOOST_FOREACH(const indexVec & index, indexes) {
67  // double lowerLimit = resMassForRegion[i] - massWindowHalfWidth[regToResHW_[i]]*leftWindowFactors[i];
68  // double upperLimit = resMassForRegion[i] + massWindowHalfWidth[regToResHW_[i]]*rightWindowFactors[i];
69  // backgroundWindow_.push_back( MassWindow( resMassForRegion[i], lowerLimit, upperLimit, index,
70  // backgroundFunctionService(identifiers[i], lowerLimit, upperLimit ) ) );
71  backgroundWindow_.push_back( MassWindow( resMassForRegion[i], leftWindowBorders[i], rightWindowBorders[i], index,
72  backgroundFunctionService(identifiers[i], leftWindowBorders[i], rightWindowBorders[i] ) ) );
73  ++i;
74  }
75  // Initialize the parNums to be used in the shifts of parval
77 }
int i
Definition: DBlmapReader.cc:9
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_
void consistencyCheck(const std::vector< int > &identifiers, const std::vector< double > &leftWindowBorders, const std::vector< double > &rightWindowBorders) const
Used to check the consistency of passed parameters.
backgroundFunctionBase * backgroundFunctionService(const int identifier, const double &lowerLimit, const double &upperLimit)
Service to build the background functor corresponding to the passed identifier.
Definition: Functions.cc:144
void initializeParNums()
Initialize the parNums to be used in the shifts of parval.
BackgroundHandler::~BackgroundHandler ( )

Definition at line 79 of file BackgroundHandler.cc.

80 {
81 }

Member Function Documentation

std::pair< double, double > BackgroundHandler::backgroundFunction ( const bool  doBackgroundFit,
const double *  parval,
const int  resTotNum,
const int  ires,
const bool *  resConsidered,
const double *  ResMass,
const double  ResHalfWidth[],
const int  MuonType,
const double &  mass,
const double &  resEta 
)

Returns the background fraction parameter (parBgr[0], but shifted to the correct function) and the value returned by the background function.
Depending on the value of doBackgroundFit it returns the values for the regions or the resonances.

Definition at line 224 of file BackgroundHandler.cc.

References backgroundWindow_, parNumsRegions_, parNumsResonances_, resonanceWindow_, and resToReg_.

Referenced by MuScleFitUtils::massProb().

228 {
229  if( doBackgroundFit ) {
230  // Return the values for the region
231  int iReg = resToReg_[ires];
232  return std::make_pair( parval[parNumsRegions_[iReg]] * backgroundWindow_[iReg].backgroundFunction()->fracVsEta(&(parval[parNumsRegions_[iReg]]), resEta),
233  (*(backgroundWindow_[iReg].backgroundFunction()))( &(parval[parNumsRegions_[iReg]]), mass, resEta ) );
234  }
235  // Return the values for the resonance
236  return std::make_pair( parval[parNumsResonances_[ires]] * resonanceWindow_[ires].backgroundFunction()->fracVsEta(&(parval[parNumsResonances_[ires]]), resEta),
237  (*(resonanceWindow_[ires].backgroundFunction()))( &(parval[parNumsResonances_[ires]]), mass, resEta ) );
238 }
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_
std::pair< double, double > backgroundFunction(const bool doBackgroundFit, const double *parval, const int resTotNum, const int ires, const bool *resConsidered, const double *ResMass, const double ResHalfWidth[], const int MuonType, const double &mass, const double &resEta)
bool BackgroundHandler::checkBackgroundWindow ( const double &  mass,
const int  iRegion 
)
inline

Check if the mass value is inside the given background region.

Definition at line 39 of file BackgroundHandler.h.

References backgroundWindow_.

40  {
41  return backgroundWindow_[iRegion].isIn(mass);
42  }
std::vector< MassWindow > backgroundWindow_
void BackgroundHandler::consistencyCheck ( const std::vector< int > &  identifiers,
const std::vector< double > &  leftWindowBorders,
const std::vector< double > &  rightWindowBorders 
) const
throw (cms::Exception
)
private

Used to check the consistency of passed parameters.

Definition at line 266 of file BackgroundHandler.cc.

References edm::hlt::Exception.

Referenced by BackgroundHandler().

269 {
270  if( leftWindowBorders.size() != rightWindowBorders.size() ) {
271  throw cms::Exception("Configuration") << "BackgroundHandler::BackgroundHandler: leftWindowBorders.size() = " << leftWindowBorders.size()
272  << " != rightWindowBorders.size() = " << rightWindowBorders.size() << std::endl;
273  }
274  if( leftWindowBorders.size() != 3 ) {
275  throw cms::Exception("Configuration") << "BackgroundHandler::BackgroundHandler: leftWindowBorders.size() = rightWindowBorders.size() = "
276  << leftWindowBorders.size() << " != 3" << std::endl;
277  }
278  if( identifiers.size() != 3 ) {
279  throw cms::Exception("Configuration") << "BackgroundHandler::BackgroundHandler: identifiers must match the number of regions = 3" << std::endl;
280  }
281 }
void BackgroundHandler::countEventsInAllWindows ( const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &  muonPairs,
const double &  weight 
)

Definition at line 240 of file BackgroundHandler.cc.

References backgroundWindow_, MassWindow::count(), MassWindow::resetCounter(), resonanceWindow_, and CommonMethods::weight().

Referenced by rescale().

242 {
243  // First reset all the counters
244  BOOST_FOREACH(MassWindow & resonanceWindow, resonanceWindow_) {
245  resonanceWindow.resetCounter();
246  }
247  // Count events in background windows
248  BOOST_FOREACH(MassWindow & backgroundWindow, backgroundWindow_) {
249  backgroundWindow.resetCounter();
250  }
251 
252  // Now count the events in each window
253  std::pair<lorentzVector,lorentzVector> muonPair;
254  BOOST_FOREACH(muonPair, muonPairs) {
255  // Count events in resonance windows
256  BOOST_FOREACH(MassWindow & resonanceWindow, resonanceWindow_) {
257  resonanceWindow.count((muonPair.first + muonPair.second).mass(), weight);
258  }
259  // Count events in background windows
260  BOOST_FOREACH(MassWindow & backgroundWindow, backgroundWindow_) {
261  backgroundWindow.count((muonPair.first + muonPair.second).mass(), weight);
262  }
263  }
264 }
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_
void count(const double &mass, const double &weight=1.)
Definition: MassWindow.h:27
void resetCounter()
Definition: MassWindow.h:33
void BackgroundHandler::initializeParNums ( )

Initialize the parNums to be used in the shifts of parval.

Definition at line 83 of file BackgroundHandler.cc.

References backgroundWindow_, i, parNumsRegions_, parNumsResonances_, and resonanceWindow_.

Referenced by BackgroundHandler().

84 {
85  // Initialize the parNums to be used in the shifts of parval
86  parNumsRegions_[0] = 0;
87  for( unsigned int i=1; i<backgroundWindow_.size() ; ++i ) {
88  parNumsRegions_[i] = parNumsRegions_[i-1] + backgroundWindow_[i-1].backgroundFunction()->parNum();
89  }
90  parNumsResonances_[0] = parNumsRegions_[2]+backgroundWindow_[2].backgroundFunction()->parNum();
91  for( unsigned int i=1; i<resonanceWindow_.size() ; ++i ) {
92  parNumsResonances_[i] = parNumsResonances_[i-1] + resonanceWindow_[i-1].backgroundFunction()->parNum();
93  }
94 }
int i
Definition: DBlmapReader.cc:9
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_
int BackgroundHandler::regionsParNum ( )
inline

Returns the total number of parameters used for the regions.

Definition at line 33 of file BackgroundHandler.h.

References parNumsResonances_.

Referenced by MuScleFitUtils::minimizeLikelihood().

34  {
35  return parNumsResonances_[0];
36  }
void BackgroundHandler::rescale ( std::vector< double > &  parBgr,
const double *  ResMass,
const double *  massWindowHalfWidth,
const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &  muonPairs,
const double &  weight = 1. 
)

Computes the rescaled parameters from the regions functions to the resonances functions. It takes into account the difference in intervals and rescales the parameters so that the fraction of events is correctly accounted for.
It uses the list of all muon pairs to compute the number of events in each resonance window.

Definition at line 169 of file BackgroundHandler.cc.

References MassWindow::backgroundFunction(), backgroundWindow_, countEventsInAllWindows(), gather_cfg::cout, MassWindow::events(), backgroundFunctionBase::functionForIntegral(), getHLTprescales::index, MassWindow::indexes(), MassWindow::lowerBound(), parNumsRegions_, parNumsResonances_, resonanceWindow_, resToReg_, and MassWindow::upperBound().

Referenced by MuScleFitUtils::minimizeLikelihood().

172 {
173  countEventsInAllWindows(muonPairs, weight);
174 
175  // Loop on all regions and on all the resonances of each region and compute the background fraction
176  // for each resonance window.
177  unsigned int iRegion = 0;
178  BOOST_FOREACH(MassWindow & backgroundWindow, backgroundWindow_)
179  {
180  // Iterator pointing to the first parameter of this background function in the full set of parameters
181  std::vector<double>::const_iterator parBgrIt = (parBgr.begin()+parNumsRegions_[iRegion]);
182  TF1 * backgroundFunctionForIntegral = backgroundWindow.backgroundFunction()->functionForIntegral(parBgrIt);
183  // WARNING: this expects the background fraction parameter to be parBgr[0] for all the background functions.
184  double kOld = *parBgrIt;
185  double Nbw = backgroundWindow.events();
186  double Ibw = backgroundFunctionForIntegral->Integral(backgroundWindow.lowerBound(),
187  backgroundWindow.upperBound());
188 
189  // index is the index of the resonance in the background window
190  BOOST_FOREACH( unsigned int index, *(backgroundWindow.indexes()) )
191  {
192  // First set all parameters of the resonance window background function to those of the corresponding region
193  for( int iPar = 0; iPar < resonanceWindow_[index].backgroundFunction()->parNum(); ++iPar ) {
194  parBgr[parNumsResonances_[index]+iPar] = parBgr[parNumsRegions_[resToReg_[index]]+iPar];
195  }
196  // Estimated fraction of events in the resonance window
197  double Irw = backgroundFunctionForIntegral->Integral(resonanceWindow_[index].lowerBound(),
198  resonanceWindow_[index].upperBound());
199  double Nrw = resonanceWindow_[index].events();
200 
201  // Ibw is 1 (to avoid effects from approximation errors we set it to 1 and do not include it in the computation).
202  if( Nrw != 0 ) parBgr[parNumsResonances_[index]] = kOld*Nbw/Nrw*Irw;
203  else parBgr[parNumsResonances_[index]] = 0.;
204 
205  // Protect against fluctuations of number of events which could cause the fraction to go above 1.
206  if( parBgr[parNumsResonances_[index]] > 1. ) parBgr[parNumsResonances_[index]] = 1.;
207 
208  double kNew = parBgr[parNumsResonances_[index]];
209  std::cout << "For resonance = " << index << std::endl;
210  std::cout << "backgroundWindow.lowerBound = " << backgroundWindow.lowerBound() << std::endl;
211  std::cout << "backgroundWindow.upperBound = " << backgroundWindow.upperBound() << std::endl;
212  std::cout << "parNumsResonances_["<<index<<"] = " << parNumsResonances_[index] << std::endl;
213  std::cout << "Nbw = " << Nbw << ", Ibw = " << Ibw << std::endl;
214  std::cout << "Nrw = " << Nrw << ", Irw = " << Irw << std::endl;
215  std::cout << "k = " << kOld << ", k' = " << parBgr[parNumsResonances_[index]] << std::endl;
216  std::cout << "background fraction in background window = Nbw*k = " << Nbw*kOld << std::endl;
217  std::cout << "background fraction in resonance window = Nrw*k' = " << Nrw*kNew << std::endl;
218  }
219  ++iRegion;
220  delete backgroundFunctionForIntegral;
221  }
222 }
std::vector< MassWindow > backgroundWindow_
virtual TF1 * functionForIntegral(const std::vector< double >::const_iterator &parBgrIt) const
Definition: Functions.h:2707
std::vector< MassWindow > resonanceWindow_
double upperBound() const
Definition: MassWindow.h:37
double events() const
Definition: MassWindow.h:38
double lowerBound() const
Definition: MassWindow.h:36
const std::vector< unsigned int > * indexes() const
Definition: MassWindow.h:40
backgroundFunctionBase * backgroundFunction() const
Definition: MassWindow.h:39
void countEventsInAllWindows(const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &muonPairs, const double &weight)
tuple cout
Definition: gather_cfg.py:41
double BackgroundHandler::resMass ( const bool  doBackgroundFit,
const int  ires 
)

Returns the appropriate resMass value depending on whether the background is being fitted and on the resonance.
The resMass used for the region is the mean of the mass of the corresponding resonances, so for the Z is the same Z mass, for the Upsilons is the arithmetic mean of the Upsilon masses and the same for the J/Psi and Psi2S region.

Definition at line 157 of file BackgroundHandler.cc.

References backgroundWindow_, resonanceWindow_, and resToReg_.

Referenced by MuScleFitUtils::massProb(), and MuScleFitUtils::minimizeLikelihood().

158 {
159  if( doBackgroundFit ) {
160  // Fitting the background: use the regions
161  return backgroundWindow_[resToReg_[ires]].mass();
162  }
163  else {
164  // Not fitting the background: use the resonances
165  return resonanceWindow_[ires].mass();
166  }
167 }
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_
void BackgroundHandler::setParameters ( double *  Start,
double *  Step,
double *  Mini,
double *  Maxi,
int *  ind,
TString *  parname,
const std::vector< double > &  parBgr,
const std::vector< int > &  parBgrOrder,
const int  muonType 
)

Sets initial parameters for all the functions.

Definition at line 96 of file BackgroundHandler.cc.

References backgroundWindow_, parNumsRegions_, parNumsResonances_, resonanceWindow_, and edm::shift.

Referenced by MuScleFitUtils::minimizeLikelihood().

97 {
98  std::vector<double>::const_iterator parBgrIt = parBgr.begin();
99  std::vector<int>::const_iterator parBgrOrderIt = parBgrOrder.begin();
100  // Set the parameters for the regions only if this is not a rescaling
101  for( int iReg = 0; iReg < 3; ++iReg ) {
102  int shift = parNumsRegions_[iReg];
103  backgroundWindow_[iReg].backgroundFunction()->setParameters( &(Start[shift]), &(Step[shift]), &(Mini[shift]),
104  &(Maxi[shift]), &(ind[shift]), &(parname[shift]),
105  parBgrIt+shift, parBgrOrderIt+shift, muonType );
106  }
107  for( int iRes = 0; iRes < 6; ++iRes ) {
108  // parNumsResonances is already shifted for the regions parameters
109  int shift = parNumsResonances_[iRes];
110  resonanceWindow_[iRes].backgroundFunction()->setParameters( &(Start[shift]), &(Step[shift]), &(Mini[shift]),
111  &(Maxi[shift]), &(ind[shift]), &(parname[shift]),
112  parBgrIt+shift, parBgrOrderIt+shift, muonType );
113  }
114 }
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_
static unsigned int const shift
bool BackgroundHandler::unlockParameter ( const std::vector< int > &  resfind,
const unsigned int  ipar 
)

returns true if the parameter is to be unlocked

Definition at line 116 of file BackgroundHandler.cc.

References parNumsRegions_, and parNumsResonances_.

Referenced by MuScleFitUtils::minimizeLikelihood().

117 {
118  // parNumsRegions_ are shifted: [1] contains the number of parameters for 0 and so on.
119  if( ipar < unsigned(parNumsRegions_[1]) && resfind[0] > 0 ) {
120  return true;
121  }
122  if( ipar >= unsigned(parNumsRegions_[1]) && ipar < unsigned(parNumsRegions_[2]) && ( resfind[1] > 0 || resfind[2] > 0 || resfind[3] > 0 ) ) {
123  return true;
124  }
125  // The first of parNumsResonances_ has the sum of parNums of the regions.
126  if( ipar >= unsigned(parNumsRegions_[2]) && ipar < unsigned(parNumsResonances_[0]) && ( resfind[4] > 0 || resfind[5] > 0 ) ) {
127  return true;
128  }
129  return false;
130 }
std::pair< double, double > BackgroundHandler::windowBorders ( const bool  doBackgroundFit,
const int  ires 
)

Returns the appropriate window borders depending on whether the background is being fitted and on the resonance.

Definition at line 144 of file BackgroundHandler.cc.

References backgroundWindow_, resonanceWindow_, and resToReg_.

Referenced by MuScleFitUtils::computeWeight(), MuScleFitUtils::massProb(), and MuScleFitUtils::minimizeLikelihood().

145 {
146  if( doBackgroundFit ) {
147  // Fitting the background: use the regions
148  return std::make_pair(backgroundWindow_[resToReg_[ires]].lowerBound(), backgroundWindow_[resToReg_[ires]].upperBound());
149  }
150  else {
151  // Not fitting the background: use the resonances
152  // return std::make_pair(1.,1.);
153  return std::make_pair(resonanceWindow_[ires].lowerBound(), resonanceWindow_[ires].upperBound());
154  }
155 }
std::vector< MassWindow > backgroundWindow_
std::vector< MassWindow > resonanceWindow_

Member Data Documentation

std::vector<MassWindow> BackgroundHandler::backgroundWindow_
private
int BackgroundHandler::parNumsRegions_[3]
private
int BackgroundHandler::parNumsResonances_[6]
private
int BackgroundHandler::regToResHW_[3]
private

Definition at line 91 of file BackgroundHandler.h.

Referenced by BackgroundHandler().

std::vector<MassWindow> BackgroundHandler::resonanceWindow_
private
int BackgroundHandler::resToReg_[6]
private