Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:37

0001 /**
0002  * MomentumScaleCorrector class
0003  * Author M. De Mattia - 18/11/2008
0004  */
0005 
0006 #ifndef MomentumScaleCorrector_h
0007 #define MomentumScaleCorrector_h
0008 
0009 #include <fstream>
0010 #include <sstream>
0011 #include "MuonAnalysis/MomentumScaleCalibration/interface/BaseFunction.h"
0012 #include "MuonAnalysis/MomentumScaleCalibration/interface/Functions.h"
0013 #include "FWCore/ParameterSet/interface/FileInPath.h"
0014 
0015 /**
0016  * This is used to have a common set of functions for the specialized templates to use.
0017  * The constructor receives the name identifying the parameters for the correction function.
0018  * It reads the parameters from a txt file in data/.
0019  *
0020  * It handles multiple iterations. It is also possible to use different functions in
0021  * different iterations.
0022  *
0023  * ATTENTION: it is important that iterations numbers in the txt file start from 0.
0024  */
0025 class MomentumScaleCorrector : public BaseFunction {
0026 public:
0027   /**
0028    * The constructor takes a string identifying the parameters to read. It
0029    * parses the txt file containing the parameters, extracts the index of the
0030    * correction function and saves the corresponding pointer. It then fills the
0031    * vector of parameters.
0032    */
0033   MomentumScaleCorrector(TString identifier) {
0034     identifier.Prepend("MuonAnalysis/MomentumScaleCalibration/data/");
0035     identifier.Append(".txt");
0036     edm::FileInPath fileWithFullPath(identifier.Data());
0037     readParameters(fileWithFullPath.fullPath());
0038   }
0039   /**
0040    * This constructor is used when reading parameters from the db.
0041    * It receives a pointer to an object of type MuScleFitDBobject containing
0042    * the parameters and the functions identifiers.
0043    */
0044   MomentumScaleCorrector(const MuScleFitDBobject* dbObject) : BaseFunction(dbObject) {
0045     std::vector<int>::const_iterator id = functionId_.begin();
0046     for (; id != functionId_.end(); ++id) {
0047       scaleFunctionVec_.push_back(scaleFunctionService(*id));
0048     }
0049     // Fill the arrays that will be used when calling the correction function.
0050     convertToArrays(scaleFunction_, scaleFunctionVec_);
0051   }
0052 
0053   ~MomentumScaleCorrector() {
0054     if (parArray_ != nullptr) {
0055       for (unsigned int i = 0; i < functionId_.size(); ++i) {
0056         delete[] parArray_[i];
0057         delete scaleFunction_[i];
0058       }
0059       delete[] parArray_;
0060       delete[] scaleFunction_;
0061     }
0062   }
0063 
0064   /// Returns a pointer to the selected function
0065   scaleFunctionBase<double*>* function(const int i) { return scaleFunction_[i]; }
0066 
0067   /// Method to do the corrections. It is templated to work with all the track types.
0068   template <class U>
0069   double operator()(const U& track) {
0070     // Loop on all the functions and apply them iteratively on the pt corrected by the previous function.
0071     double pt = track.pt();
0072     for (int i = 0; i <= iterationNum_; ++i) {
0073       // return ( scaleFunction_->scale( track.pt(), track.eta(), track.phi(), track.charge(), parScale_) );
0074       pt = (scaleFunction_[i]->scale(pt, track.eta(), track.phi(), track.charge(), parArray_[i]));
0075     }
0076     return pt;
0077   }
0078 
0079   /// Alternative method that can be used with lorentzVectors.
0080   template <class U>
0081   double correct(const U& lorentzVector) {
0082     // Loop on all the functions and apply them iteratively on the pt corrected by the previous function.
0083     double pt = lorentzVector.Pt();
0084     for (int i = 0; i <= iterationNum_; ++i) {
0085       pt = (scaleFunction_[i]->scale(pt, lorentzVector.Eta(), lorentzVector.Phi(), 1, parArray_[i]));
0086     }
0087     return pt;
0088   }
0089 
0090 protected:
0091   /// Parser of the parameters file
0092   void readParameters(TString fileName);
0093 
0094   scaleFunctionBase<double*>** scaleFunction_;
0095   std::vector<scaleFunctionBase<double*>*> scaleFunctionVec_;
0096 };
0097 
0098 #endif  // MomentumScaleCorrector_h