Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:28

0001 #ifndef PhysicsTools_Utilities_MultiHistoChiSquare_h
0002 #define PhysicsTools_Utilities_MultiHistoChiSquare_h
0003 #include "PhysicsTools/Utilities/interface/RootMinuitResultPrinter.h"
0004 #include "TMath.h"
0005 #include "TH1.h"
0006 
0007 namespace fit {
0008   namespace helper {
0009     struct MultiHistoChiSquareNoArg {};
0010   }  // namespace helper
0011 
0012   template <typename T1,
0013             typename T2 = helper::MultiHistoChiSquareNoArg,
0014             typename T3 = helper::MultiHistoChiSquareNoArg,
0015             typename T4 = helper::MultiHistoChiSquareNoArg,
0016             typename T5 = helper::MultiHistoChiSquareNoArg,
0017             typename T6 = helper::MultiHistoChiSquareNoArg>
0018   class MultiHistoChiSquare {
0019   public:
0020     MultiHistoChiSquare() {}
0021     template <typename TT1, typename TT2, typename TT3, typename TT4, typename TT5, typename TT6>
0022     MultiHistoChiSquare(TT1 &t1,
0023                         TH1 *histo1,
0024                         TT2 &t2,
0025                         TH1 *histo2,
0026                         TT3 &t3,
0027                         TH1 *histo3,
0028                         TT4 &t4,
0029                         TH1 *histo4,
0030                         TT5 &t5,
0031                         TH1 *histo5,
0032                         TT6 &t6,
0033                         TH1 *histo6,
0034                         double rangeMin,
0035                         double rangeMax)
0036         : chi1_(t1, histo1, rangeMin, rangeMax),
0037           chi2_(t2, histo2, rangeMin, rangeMax),
0038           chi3_(t3, histo3, rangeMin, rangeMax),
0039           chi4_(t4, histo4, rangeMin, rangeMax),
0040           chi5_(t5, histo5, rangeMin, rangeMax),
0041           chi6_(t6, histo6, rangeMin, rangeMax) {}
0042     double operator()() const {
0043       double chi2 = chi1_() + chi2_() + chi3_() + chi4_() + chi5_() + chi6_();
0044       static size_t count = 0;
0045       ++count;
0046       if (count % 10 == 0)
0047         return chi2;
0048     }
0049     void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4, TH1 *histo5, TH1 *histo6) {
0050       chi1_.setHistos(histo1);
0051       chi2_.setHistos(histo2);
0052       chi3_.setHistos(histo3);
0053       chi4_.setHistos(histo4);
0054       chi5_.setHistos(histo5);
0055       chi6_.setHistos(histo6);
0056     }
0057     size_t numberOfBins() const {
0058       return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins() + chi4_.numberOfBins() +
0059              chi5_.numberOfBins() + chi6_.numberOfBins();
0060     }
0061     T1 &function1() { return chi1_.function(); }
0062     const T1 &function1() const { return chi1_.function(); }
0063     T2 &function2() { return chi2_.function(); }
0064     const T2 &function2() const { return chi2_.function(); }
0065     T3 &function3() { return chi3_.function(); }
0066     const T3 &function3() const { return chi3_.function(); }
0067     T4 &function4() { return chi4_.function(); }
0068     const T4 &function4() const { return chi4_.function(); }
0069     T5 &function5() { return chi5_.function(); }
0070     const T5 &function5() const { return chi5_.function(); }
0071     T6 &function6() { return chi6_.function(); }
0072     const T6 &function6() const { return chi6_.function(); }
0073 
0074   private:
0075     T1 chi1_;
0076     T2 chi2_;
0077     T3 chi3_;
0078     T4 chi4_;
0079     T5 chi5_;
0080     T6 chi6_;
0081   };
0082 
0083   template <typename T1, typename T2, typename T3, typename T4, typename T5>
0084   class MultiHistoChiSquare<T1, T2, T3, T4, T5, helper::MultiHistoChiSquareNoArg> {
0085   public:
0086     MultiHistoChiSquare() {}
0087     template <typename TT1, typename TT2, typename TT3, typename TT4, typename TT5>
0088     MultiHistoChiSquare(TT1 &t1,
0089                         TH1 *histo1,
0090                         TT2 &t2,
0091                         TH1 *histo2,
0092                         TT3 &t3,
0093                         TH1 *histo3,
0094                         TT4 &t4,
0095                         TH1 *histo4,
0096                         TT5 &t5,
0097                         TH1 *histo5,
0098                         double rangeMin,
0099                         double rangeMax)
0100         : chi1_(t1, histo1, rangeMin, rangeMax),
0101           chi2_(t2, histo2, rangeMin, rangeMax),
0102           chi3_(t3, histo3, rangeMin, rangeMax),
0103           chi4_(t4, histo4, rangeMin, rangeMax),
0104           chi5_(t5, histo5, rangeMin, rangeMax) {}
0105     double operator()() const {
0106       double chi2 = chi1_() + chi2_() + chi3_() + chi4_() + chi5_();
0107       static size_t count = 0;
0108       ++count;
0109       return chi2;
0110     }
0111     void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4, TH1 *histo5) {
0112       chi1_.setHistos(histo1);
0113       chi2_.setHistos(histo2);
0114       chi3_.setHistos(histo3);
0115       chi4_.setHistos(histo4);
0116       chi5_.setHistos(histo5);
0117     }
0118     size_t numberOfBins() const {
0119       return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins() + chi4_.numberOfBins() +
0120              chi5_.numberOfBins();
0121     }
0122     T1 &function1() { return chi1_.function(); }
0123     const T1 &function1() const { return chi1_.function(); }
0124     T2 &function2() { return chi2_.function(); }
0125     const T2 &function2() const { return chi2_.function(); }
0126     T3 &function3() { return chi3_.function(); }
0127     const T3 &function3() const { return chi3_.function(); }
0128     T4 &function4() { return chi4_.function(); }
0129     const T4 &function4() const { return chi4_.function(); }
0130     T5 &function5() { return chi5_.function(); }
0131     const T5 &function5() const { return chi5_.function(); }
0132 
0133   private:
0134     T1 chi1_;
0135     T2 chi2_;
0136     T3 chi3_;
0137     T4 chi4_;
0138     T5 chi5_;
0139   };
0140 
0141   template <typename T1, typename T2, typename T3, typename T4>
0142   class MultiHistoChiSquare<T1, T2, T3, T4, helper::MultiHistoChiSquareNoArg, helper::MultiHistoChiSquareNoArg> {
0143   public:
0144     MultiHistoChiSquare() {}
0145     template <typename TT1, typename TT2, typename TT3, typename TT4>
0146     MultiHistoChiSquare(TT1 &t1,
0147                         TH1 *histo1,
0148                         TT2 &t2,
0149                         TH1 *histo2,
0150                         TT3 &t3,
0151                         TH1 *histo3,
0152                         TT4 &t4,
0153                         TH1 *histo4,
0154                         double rangeMin,
0155                         double rangeMax)
0156         : chi1_(t1, histo1, rangeMin, rangeMax),
0157           chi2_(t2, histo2, rangeMin, rangeMax),
0158           chi3_(t3, histo3, rangeMin, rangeMax),
0159           chi4_(t4, histo4, rangeMin, rangeMax) {}
0160     double operator()() const {
0161       double chi2 = chi1_() + chi2_() + chi3_() + chi4_();
0162       static size_t count = 0;
0163       ++count;
0164       return chi2;
0165     }
0166     void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4) {
0167       chi1_.setHistos(histo1);
0168       chi2_.setHistos(histo2);
0169       chi3_.setHistos(histo3);
0170       chi4_.setHistos(histo4);
0171     }
0172     size_t numberOfBins() const {
0173       return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins() + chi4_.numberOfBins();
0174     }
0175     T1 &function1() { return chi1_.function(); }
0176     const T1 &function1() const { return chi1_.function(); }
0177     T2 &function2() { return chi2_.function(); }
0178     const T2 &function2() const { return chi2_.function(); }
0179     T3 &function3() { return chi3_.function(); }
0180     const T3 &function3() const { return chi3_.function(); }
0181     T4 &function4() { return chi4_.function(); }
0182     const T4 &function4() const { return chi4_.function(); }
0183 
0184   private:
0185     T1 chi1_;
0186     T2 chi2_;
0187     T3 chi3_;
0188     T4 chi4_;
0189   };
0190 
0191   template <typename T1, typename T2, typename T3>
0192   class MultiHistoChiSquare<T1,
0193                             T2,
0194                             T3,
0195                             helper::MultiHistoChiSquareNoArg,
0196                             helper::MultiHistoChiSquareNoArg,
0197                             helper::MultiHistoChiSquareNoArg> {
0198   public:
0199     MultiHistoChiSquare() {}
0200     template <typename TT1, typename TT2, typename TT3>
0201     MultiHistoChiSquare(
0202         TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, TT3 &t3, TH1 *histo3, double rangeMin, double rangeMax)
0203         : chi1_(t1, histo1, rangeMin, rangeMax),
0204           chi2_(t2, histo2, rangeMin, rangeMax),
0205           chi3_(t3, histo3, rangeMin, rangeMax) {}
0206     double operator()() const { return chi1_() + chi2_() + chi3_(); }
0207     void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3) {
0208       chi1_.setHistos(histo1);
0209       chi2_.setHistos(histo2);
0210       chi3_.setHistos(histo3);
0211     }
0212     size_t numberOfBins() const { return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins(); }
0213     T1 &function1() { return chi1_.function(); }
0214     const T1 &function1() const { return chi1_.function(); }
0215     T2 &function2() { return chi2_.function(); }
0216     const T2 &function2() const { return chi2_.function(); }
0217     T3 &function3() { return chi3_.function(); }
0218     const T3 &function3() const { return chi3_.function(); }
0219 
0220   private:
0221     T1 chi1_;
0222     T2 chi2_;
0223     T3 chi3_;
0224   };
0225 
0226   template <typename T1, typename T2>
0227   class MultiHistoChiSquare<T1,
0228                             T2,
0229                             helper::MultiHistoChiSquareNoArg,
0230                             helper::MultiHistoChiSquareNoArg,
0231                             helper::MultiHistoChiSquareNoArg,
0232                             helper::MultiHistoChiSquareNoArg> {
0233   public:
0234     MultiHistoChiSquare() {}
0235     template <typename TT1, typename TT2>
0236     MultiHistoChiSquare(TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, double rangeMin, double rangeMax)
0237         : chi1_(t1, histo1, rangeMin, rangeMax), chi2_(t2, histo2, rangeMin, rangeMax) {}
0238     double operator()() const { return chi1_() + chi2_(); }
0239     void setHistos(TH1 *histo1, TH1 *histo2) {
0240       chi1_.setHistos(histo1);
0241       chi2_.setHistos(histo2);
0242     }
0243     size_t numberOfBins() const { return chi1_.numberOfBins() + chi2_.numberOfBins(); }
0244 
0245   private:
0246     T1 chi1_;
0247     T2 chi2_;
0248   };
0249 
0250   template <typename T1>
0251   class MultiHistoChiSquare<T1,
0252                             helper::MultiHistoChiSquareNoArg,
0253                             helper::MultiHistoChiSquareNoArg,
0254                             helper::MultiHistoChiSquareNoArg,
0255                             helper::MultiHistoChiSquareNoArg,
0256                             helper::MultiHistoChiSquareNoArg> {
0257   public:
0258     MultiHistoChiSquare() {}
0259     template <typename TT1>
0260     MultiHistoChiSquare(TT1 &t1, TH1 *histo1, double rangeMin, double rangeMax)
0261         : chi1_(t1, histo1, rangeMin, rangeMax) {}
0262     double operator()() const { return chi1_(); }
0263     void setHistos(TH1 *histo1) { chi1_.setHistos(histo1); }
0264     size_t numberOfBins() const { return chi1_.numberOfBins(); }
0265 
0266   private:
0267     T1 chi1_;
0268   };
0269 
0270   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
0271   struct RootMinuitResultPrinter<MultiHistoChiSquare<T1, T2, T3, T4, T5, T6> > {
0272     static void print(double amin,
0273                       unsigned int numberOfFreeParameters,
0274                       const MultiHistoChiSquare<T1, T2, T3, T4, T5, T6> &f) {
0275       unsigned int ndof = f.numberOfBins() - numberOfFreeParameters;
0276       std::cout << "chi-squared/n.d.o.f. = " << amin << "/" << ndof << " = " << amin / ndof
0277                 << "; prob: " << TMath::Prob(amin, ndof) << std::endl;
0278     }
0279   };
0280 }  // namespace fit
0281 
0282 #endif