#ifndef DFitFunctors_Data_h #define DFitFunctors_Data_h #include #include #include #include "TMath.h" #include "TObject.h" #include "TRandom3.h" #include "TH1D.h" #include "TNtuple.h" #include "TF1.h" #include "DFitFunctor.h" using namespace std; //NOTE: FIT TYPES CANNOT HAVE UNDERSCORES!!! //THEY ALSO CANNOT BE "Total" "Signal" OR "Background" class DFitFunctor_Histogram : public DFitFunctor { public: DFitFunctor_Histogram(const vector& locInitParams, const TH1D* locFitToHist, bool locFixedShiftFlag = false, int locFuncColor = kBlack) : DFitFunctor("Histogram", locInitParams, locFuncColor), dFitToHist(locFitToHist), dFixedShiftFlag(locFixedShiftFlag) { if(locInitParams.size() != 2) { cout << "ERROR: INCORRECT NUMBER OF PARAMETERS IN DFitFunctor CONSTRUCTOR. EXITING" << endl; abort(); } dParamNames[0] = "Histogram Scale Factor"; dParamNames[1] = "Histogram X-Shift"; if(dFixedShiftFlag) dFixedParams.insert(1); } DFitFunctor_Histogram(TF1* locFunc, const TH1D* locFitToHist) : DFitFunctor("Histogram", locFunc), dFitToHist(locFitToHist){} //fit to histogram: can scale in y [0] and shift in x [1] but cannot smear //to prevent shift in x, fix param [1] virtual double operator()(double* locX, double* locParamArray) { double locYScaleFactor = locParamArray[0]; double locXShift = locParamArray[1]; unsigned int locXBin = dFitToHist->GetXaxis()->FindBin(locX[0] - locXShift); return locYScaleFactor*(dFitToHist->GetBinContent(locXBin)); } private: const TH1D* dFitToHist; bool dFixedShiftFlag; ClassDef(DFitFunctor_Histogram, 1) }; class DFitFunctor_NTuple : public DFitFunctor { public: DFitFunctor_NTuple(const vector& locInitParams, TNtuple* locNtuple, string locBranchName, TH1D* locBasisHist, int locFuncColor = kBlack) : DFitFunctor("NTuple", locInitParams, locFuncColor), dNtuple(locNtuple), dBasisHist(locBasisHist), dSmearValue(0.0), dBranchValue(0.0), dBranch(NULL) { if(locInitParams.size() != 3) { cout << "ERROR: INCORRECT NUMBER OF PARAMETERS IN DFitFunctor CONSTRUCTOR. EXITING" << endl; abort(); } dParamNames[0] = "NTuple Scale Factor"; dParamNames[1] = "NTuple X-Shift"; dParamNames[2] = "NTuple Smear"; dRandomGenerator.SetSeed(0); dParamLimits[2] = pair(0.0, 100.0*dInitParams[0]); dBasisHist->Reset(); dNtuple->SetBranchAddress(locBranchName.c_str(), &dBranchValue, &dBranch); } DFitFunctor_NTuple(TF1* locFunc, TNtuple* locNtuple, string locBranchName, TH1D* locBasisHist) : DFitFunctor("NTuple", locFunc), dNtuple(locNtuple), dBasisHist(locBasisHist), dSmearValue(0.0), dBranchValue(0.0), dBranch(NULL){} //fit to ntuple: can scale in y [0] and shift in x [1], and smear [2] //to prevent shift in x, fix param [1] virtual double operator()(double* locX, double* locParamArray) { double locYScaleFactor = locParamArray[0]; double locXShift = locParamArray[1]; double locNewSmear = locParamArray[2]; //fill histogram from ntuple if: //histogram is empty OR if smear value has changed //else histogram content is fine as is, no need to refill and/or resmear if((!(dBasisHist->GetEntries() > 0.0)) || (locNewSmear != dSmearValue)) { dSmearValue = locNewSmear; //update smear value dBasisHist->Reset(); for(unsigned int loc_i = 0; loc_i < dNtuple->GetEntries(); loc_i++) { dBranch->GetEntry(loc_i); double locSmearedValue = (dSmearValue != 0.0) ? dRandomGenerator.Gaus(dBranchValue, dSmearValue) : dBranchValue; dBasisHist->Fill(locSmearedValue); } } unsigned int locXBin = dBasisHist->GetXaxis()->FindBin(locX[0] - locXShift); return locYScaleFactor*(dBasisHist->GetBinContent(locXBin)); } private: TNtuple* dNtuple; TH1D* dBasisHist; //for basis (binning, range) double dSmearValue; double dBranchValue; TBranch* dBranch; TRandom3 dRandomGenerator; ClassDef(DFitFunctor_NTuple, 1) }; #endif