Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /**
0002  * \class ResolutionFunction
0003  * Class for the resolution function. It can be built from local file or from db.
0004  */
0005 
0006 #ifndef ResolutionFunction_h
0007 #define ResolutionFunction_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 class ResolutionFunction : public BaseFunction {
0016 public:
0017   /**
0018    * The constructor takes a string identifying the parameters to read. It
0019    * parses the txt file containing the parameters, extracts the index of the
0020    * correction function and saves the corresponding pointer. It then fills the
0021    * vector of parameters.
0022    */
0023   ResolutionFunction(TString identifier) {
0024     identifier.Prepend("MuonAnalysis/MomentumScaleCalibration/data/");
0025     identifier.Append(".txt");
0026     edm::FileInPath fileWithFullPath(identifier.Data());
0027     readParameters(fileWithFullPath.fullPath());
0028 
0029     std::vector<int>::const_iterator idIt = functionId_.begin();
0030     for (; idIt != functionId_.end(); ++idIt)
0031       std::cout << "idIt = " << *idIt << std::endl;
0032   }
0033   /**
0034    * This constructor is used when reading parameters from the db.
0035    * It receives a pointer to an object of type MuScleFitDBobject containing
0036    * the parameters and the functions identifiers.
0037    * The object is the same for all the functions.
0038    */
0039   ResolutionFunction(const MuScleFitDBobject* dbObject) : BaseFunction(dbObject) {
0040     std::vector<int>::const_iterator id = functionId_.begin();
0041     for (; id != functionId_.end(); ++id) {
0042       resolutionFunctionVec_.push_back(resolutionFunctionService(*id));
0043     }
0044     // Fill the arrays that will be used when calling the correction function.
0045     convertToArrays(resolutionFunction_, resolutionFunctionVec_);
0046   }
0047 
0048   ~ResolutionFunction() {
0049     if (parArray_ != nullptr) {
0050       for (unsigned int i = 0; i < functionId_.size(); ++i) {
0051         delete[] parArray_[i];
0052         delete resolutionFunction_[i];
0053       }
0054       delete[] parArray_;
0055       delete[] resolutionFunction_;
0056     }
0057   }
0058 
0059   // EM+SC: 2013.01.11
0060   // lorentzVector have both capital and lower case methods for pt(), eta() and phi
0061   // if a lorentzVector is passed use, parArray form iteration i=0
0062 
0063   /// The second, optional, parameter is the iteration number
0064   template <class U>
0065   double sigmaPt(const U& track, const int i = 0) const {
0066     if (i > iterationNum_ || i < 0) {
0067       std::cout << "Error: wrong iteration number, there are " << iterationNum_ << "iterations, ther first one is 0"
0068                 << std::endl;
0069       exit(1);
0070     }
0071     return resolutionFunction_[i]->sigmaPt(track.pt(), track.eta(), parArray_[i]);
0072   }
0073   /// The second, optional, parameter is the iteration number
0074   template <class U>
0075   double sigmaCotgTh(const U& track, const int i = 0) const {
0076     if (i > iterationNum_ || i < 0) {
0077       std::cout << "Error: wrong iteration number, there are " << iterationNum_ << "iterations, ther first one is 0"
0078                 << std::endl;
0079       exit(1);
0080     }
0081     return resolutionFunction_[i]->sigmaCotgTh(track.pt(), track.eta(), parArray_[i]);
0082   }
0083   /// The second, optional, parameter is the iteration number
0084   template <class U>
0085   double sigmaPhi(const U& track, const int i = 0) const {
0086     if (i > iterationNum_ || i < 0) {
0087       std::cout << "Error: wrong iteration number, there are " << iterationNum_ << "iterations, ther first one is 0"
0088                 << std::endl;
0089       exit(1);
0090     }
0091     return resolutionFunction_[i]->sigmaPhi(track.pt(), track.eta(), parArray_[i]);
0092   }
0093   /// Get the ith resolution function
0094   resolutionFunctionBase<double*>* function(const unsigned int i) {
0095     if (resolutionFunctionVec_.size() > i)
0096       return resolutionFunction_[i];
0097     else
0098       return nullptr;
0099   }
0100 
0101 protected:
0102   /// Parser of the parameters file
0103   void readParameters(TString fileName);
0104 
0105   resolutionFunctionBase<double*>** resolutionFunction_;
0106   std::vector<resolutionFunctionBase<double*>*> resolutionFunctionVec_;
0107 };
0108 
0109 #endif  // ResolutionFunction_h