/ElectroWeakAnalysis/ZMuMu/bin/zChi2Fit.cpp

https://github.com/aivanov-cern/cmssw · C++ · 602 lines · 507 code · 74 blank · 21 comment · 39 complexity · 4c92b2ea0cb0c9e638b1eaf636cdedaf MD5 · raw file

  1. #include "FWCore/Utilities/interface/EDMException.h"
  2. #include "PhysicsTools/Utilities/interface/Parameter.h"
  3. #include "PhysicsTools/Utilities/interface/Gaussian.h"
  4. #include "PhysicsTools/Utilities/interface/Numerical.h"
  5. #include "PhysicsTools/Utilities/interface/Exponential.h"
  6. #include "PhysicsTools/Utilities/interface/Polynomial.h"
  7. #include "PhysicsTools/Utilities/interface/Constant.h"
  8. #include "PhysicsTools/Utilities/interface/Operations.h"
  9. #include "PhysicsTools/Utilities/interface/MultiHistoChiSquare.h"
  10. #include "PhysicsTools/Utilities/interface/HistoChiSquare.h"
  11. #include "PhysicsTools/Utilities/interface/HistoPoissonLikelihoodRatio.h"
  12. #include "PhysicsTools/Utilities/interface/RootMinuit.h"
  13. #include "PhysicsTools/Utilities/interface/RootMinuitCommands.h"
  14. #include "PhysicsTools/Utilities/interface/FunctClone.h"
  15. #include "PhysicsTools/Utilities/interface/rootPlot.h"
  16. #include "PhysicsTools/Utilities/interface/Expression.h"
  17. #include "PhysicsTools/Utilities/interface/HistoPdf.h"
  18. #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
  19. #include "TROOT.h"
  20. #include "TSystem.h"
  21. #include "TH1.h"
  22. #include "TFile.h"
  23. #include <fstream>
  24. #include <iostream>
  25. #include <algorithm>
  26. #include <exception>
  27. #include <iterator>
  28. #include <string>
  29. #include <vector>
  30. using namespace std;
  31. // A helper function to simplify the main part.
  32. template<class T>
  33. ostream& operator<<(ostream& os, const vector<T>& v) {
  34. copy(v.begin(), v.end(), ostream_iterator<T>(cout, " "));
  35. return os;
  36. }
  37. // A function that get histogram and sets contents to 0
  38. // if entries are too small
  39. TH1 * getHisto(TFile * file, const char * name, unsigned int rebin) {
  40. TObject * h = file->Get(name);
  41. if(h == 0)
  42. throw edm::Exception(edm::errors::Configuration)
  43. << "Can't find object " << name << "\n";
  44. TH1 * histo = dynamic_cast<TH1*>(h);
  45. if(histo == 0)
  46. throw edm::Exception(edm::errors::Configuration)
  47. << "Object " << name << " is of type " << h->ClassName() << ", not TH1\n";
  48. histo->Rebin(rebin);
  49. for(int i = 1; i <= histo->GetNbinsX(); ++i) {
  50. if(histo->GetBinContent(i) < 0.1) {
  51. histo->SetBinContent(i, 0.0);
  52. histo->SetBinError(i, 0.0);
  53. }
  54. }
  55. return histo;
  56. }
  57. struct sig_tag;
  58. struct bkg_tag;
  59. typedef funct::FunctExpression Expr;
  60. typedef fit::HistoChiSquare<funct::FunctExpression> ExprChi2;
  61. typedef fit::HistoPoissonLikelihoodRatio<funct::FunctExpression> ExprPLR;
  62. double fMin, fMax;
  63. unsigned int rebinMuMuNoIso ,rebinMuMu =1 , rebinMuMu1HLT , rebinMuMu2HLT , rebinMuTk , rebinMuSa;
  64. // assume that the bin size is 1 GeV!!!
  65. string ext, region;
  66. bool nonIsoTemplateFromMC;
  67. template<typename T>
  68. struct PlotPrefix { };
  69. template<>
  70. struct PlotPrefix<ExprChi2> {
  71. static string str() { return "chi2"; }
  72. };
  73. template<>
  74. struct PlotPrefix<ExprPLR> {
  75. static string str() { return "plr"; }
  76. };
  77. template<typename T>
  78. int main_t(const vector<string> & v_file){
  79. typedef fit::MultiHistoChiSquare<T, T, T, T, T> ChiSquared;
  80. fit::RootMinuitCommands<ChiSquared> commands("zChi2Fit.txt");
  81. cout << "minuit command file completed" << endl;
  82. funct::Constant rebinMuMuNoIsoConst(rebinMuMuNoIso), rebinMuMuConst(rebinMuMu),
  83. rebinMuMu1HLTConst(rebinMuMu1HLT), rebinMuMu2HLTConst(rebinMuMu2HLT),
  84. rebinMuTkConst(rebinMuTk), rebinMuSaConst(rebinMuSa);
  85. for(vector<string>::const_iterator it = v_file.begin(); it != v_file.end(); ++it) {
  86. TFile * root_file = new TFile(it->c_str(), "read");
  87. // default when region==all
  88. // TH1 * histoZMuMuNoIso = getHisto(root_file, "nonIsolatedZToMuMuPlots/zMass",rebinMuMuNoIso);
  89. TH1 * histoZMuMuNoIso = getHisto(root_file, "oneNonIsolatedZToMuMuPlots/zMass",rebinMuMuNoIso);
  90. TH1 * histoZMuMu = getHisto(root_file, "goodZToMuMuPlots/zMass",rebinMuMu);
  91. TH1 * histoZMuMu1HLT = getHisto(root_file, "goodZToMuMu1HLTPlots/zMass", rebinMuMu1HLT);
  92. TH1 * histoZMuMu2HLT = getHisto(root_file, "goodZToMuMu2HLTPlots/zMass", rebinMuMu2HLT);
  93. TH1 * histoZMuTk = getHisto(root_file, "goodZToMuMuOneTrackPlots/zMass", rebinMuTk);
  94. TH1 * histoZMuSa = getHisto(root_file, "goodZToMuMuOneStandAloneMuonPlots/zMass", rebinMuSa);
  95. TH1 * histoZMuSaFromMuMu = getHisto(root_file, "zmumuSaMassHistogram/zMass", rebinMuSa);
  96. TH1 * histoZMuMuNoIsoTemplateFromMC= histoZMuMu;
  97. if (nonIsoTemplateFromMC) {
  98. // histoZMuMuNoIsoTemplateFromMC = getHisto(root_file, "nonIsolatedZToMuMuPlotsMC/zMass",rebinMuMu);
  99. histoZMuMuNoIsoTemplateFromMC = getHisto(root_file, "oneNonIsolatedZToMuMuPlotsMC/zMass",rebinMuMu);
  100. }
  101. if (region=="barrel"){
  102. histoZMuMuNoIso = getHisto(root_file, "nonIsolatedZToMuMuPlotsBarrel/zMass",rebinMuMuNoIso);
  103. histoZMuMu = getHisto(root_file, "goodZToMuMuPlotsBarrel/zMass",rebinMuMu);
  104. histoZMuMu1HLT = getHisto(root_file, "goodZToMuMu1HLTPlotsBarrel/zMass", rebinMuMu1HLT);
  105. histoZMuMu2HLT = getHisto(root_file, "goodZToMuMu2HLTPlotsBarrel/zMass", rebinMuMu2HLT);
  106. histoZMuTk = getHisto(root_file, "goodZToMuMuOneTrackPlotsBarrel/zMass", rebinMuTk);
  107. histoZMuSa = getHisto(root_file, "goodZToMuMuOneStandAloneMuonPlotsBarrel/zMass", rebinMuSa);
  108. histoZMuSaFromMuMu = getHisto(root_file, "zmumuSaMassHistogramBarrel/zMass", rebinMuSa);
  109. }
  110. if (region=="endcap"){
  111. histoZMuMuNoIso = getHisto(root_file, "nonIsolatedZToMuMuPlotsEndCap/zMass",rebinMuMuNoIso);
  112. histoZMuMu = getHisto(root_file, "goodZToMuMuPlotsEndCap/zMass",rebinMuMu);
  113. histoZMuMu1HLT = getHisto(root_file, "goodZToMuMu1HLTPlotsEndCap/zMass", rebinMuMu1HLT);
  114. histoZMuMu2HLT = getHisto(root_file, "goodZToMuMu2HLTPlotsEndCap/zMass", rebinMuMu2HLT);
  115. histoZMuTk = getHisto(root_file, "goodZToMuMuOneTrackPlotsEndCap/zMass", rebinMuTk);
  116. histoZMuSa = getHisto(root_file, "goodZToMuMuOneStandAloneMuonPlotsEndCap/zMass", rebinMuSa);
  117. histoZMuSaFromMuMu = getHisto(root_file, "zmumuSaMassHistogramEndCap/zMass", rebinMuSa);
  118. }
  119. if (region=="barrend"){
  120. histoZMuMuNoIso = getHisto(root_file, "nonIsolatedZToMuMuPlotsBarrEnd/zMass",rebinMuMuNoIso);
  121. histoZMuMu = getHisto(root_file, "goodZToMuMuPlotsBarrEnd/zMass",rebinMuMu);
  122. histoZMuMu1HLT = getHisto(root_file, "goodZToMuMu1HLTPlotsBarrEnd/zMass", rebinMuMu1HLT);
  123. histoZMuMu2HLT = getHisto(root_file, "goodZToMuMu2HLTPlotsBarrEnd/zMass", rebinMuMu2HLT);
  124. histoZMuTk = getHisto(root_file, "goodZToMuMuOneTrackPlotsBarrEnd/zMass", rebinMuTk);
  125. histoZMuSa = getHisto(root_file, "goodZToMuMuOneStandAloneMuonPlotsBarrEnd/zMass", rebinMuSa);
  126. histoZMuSaFromMuMu = getHisto(root_file, "zmumuSaMassHistogramBarrEnd/zMass", rebinMuSa);
  127. }
  128. if (region!="endcap" && region!="barrel" && region!="barrend" && region!="all" ){
  129. cout<< "not a valid region selected"<< endl;
  130. cout << "possible choises are: all, barrel, endcap, barrend " << endl;
  131. return 0;
  132. }
  133. cout << ">>> histogram loaded\n";
  134. string f_string = *it + "_" + PlotPrefix<T>::str() + "_";
  135. replace(f_string.begin(), f_string.end(), '.', '_');
  136. replace(f_string.begin(), f_string.end(), '/', '_');
  137. string plot_string = f_string + "." + ext;
  138. cout << ">>> Input files loaded\n";
  139. const char * kYieldZMuMu = "YieldZMuMu";
  140. const char * kEfficiencyTk = "EfficiencyTk";
  141. const char * kEfficiencySa = "EfficiencySa";
  142. const char * kEfficiencyIso = "EfficiencyIso";
  143. const char * kEfficiencyHLT = "EfficiencyHLT";
  144. const char * kYieldBkgZMuTk = "YieldBkgZMuTk";
  145. const char * kYieldBkgZMuSa = "YieldBkgZMuSa";
  146. const char * kYieldBkgZMuMuNotIso = "YieldBkgZMuMuNotIso";
  147. const char * kAlpha = "Alpha";
  148. const char * kBeta = "Beta";
  149. const char * kLambda = "Lambda";
  150. const char * kA0 = "A0";
  151. const char * kA1 = "A1";
  152. const char * kA2 = "A2";
  153. const char * kB0 = "B0";
  154. const char * kB1 = "B1";
  155. const char * kB2 = "B2";
  156. const char * kC0 = "C0";
  157. const char * kC1 = "C1";
  158. const char * kC2 = "C2";
  159. funct::Parameter yieldZMuMu(kYieldZMuMu, commands.par(kYieldZMuMu));
  160. funct::Parameter effTk(kEfficiencyTk, commands.par(kEfficiencyTk));
  161. funct::Parameter effSa(kEfficiencySa, commands.par(kEfficiencySa));
  162. funct::Parameter effIso(kEfficiencyIso, commands.par(kEfficiencyIso));
  163. funct::Parameter effHLT(kEfficiencyHLT, commands.par(kEfficiencyHLT));
  164. funct::Parameter yieldBkgZMuTk(kYieldBkgZMuTk, commands.par(kYieldBkgZMuTk));
  165. funct::Parameter yieldBkgZMuSa(kYieldBkgZMuSa, commands.par(kYieldBkgZMuSa));
  166. funct::Parameter yieldBkgZMuMuNotIso(kYieldBkgZMuMuNotIso, commands.par(kYieldBkgZMuMuNotIso));
  167. funct::Parameter alpha(kAlpha, commands.par(kAlpha));
  168. funct::Parameter beta(kBeta, commands.par(kBeta));
  169. funct::Parameter lambda(kLambda, commands.par(kLambda));
  170. funct::Parameter a0(kA0, commands.par(kA0));
  171. funct::Parameter a1(kA1, commands.par(kA1));
  172. funct::Parameter a2(kA2, commands.par(kA2));
  173. funct::Parameter b0(kB0, commands.par(kB0));
  174. funct::Parameter b1(kB1, commands.par(kB1));
  175. funct::Parameter b2(kB2, commands.par(kB2));
  176. funct::Parameter c0(kC0, commands.par(kC0));
  177. funct::Parameter c1(kC1, commands.par(kC1));
  178. funct::Parameter c2(kC2, commands.par(kC2));
  179. funct::Constant cFMin(fMin), cFMax(fMax);
  180. // count ZMuMu Yield
  181. double nZMuMu = 0, nZMuMu1HLT = 0, nZMuMu2HLT = 0;
  182. {
  183. unsigned int nBins = histoZMuMu->GetNbinsX();
  184. double xMin = histoZMuMu->GetXaxis()->GetXmin();
  185. double xMax = histoZMuMu->GetXaxis()->GetXmax();
  186. double deltaX =(xMax - xMin) / nBins;
  187. for(unsigned int i = 0; i < nBins; ++i) {
  188. double x = xMin + (i +.5) * deltaX;
  189. if(x > fMin && x < fMax){
  190. nZMuMu += histoZMuMu->GetBinContent(i+1);
  191. nZMuMu1HLT += histoZMuMu1HLT->GetBinContent(i+1);
  192. nZMuMu2HLT += histoZMuMu2HLT->GetBinContent(i+1);
  193. }
  194. }
  195. }
  196. // aggiungi 1HLT 2HLT
  197. cout << ">>> count of ZMuMu yield in the range [" << fMin << ", " << fMax << "]: " << nZMuMu << endl;
  198. cout << ">>> count of ZMuMu (1HLT) yield in the range [" << fMin << ", " << fMax << "]: " << nZMuMu1HLT << endl;
  199. cout << ">>> count of ZMuMu (2HLT) yield in the range [" << fMin << ", " << fMax << "]: " << nZMuMu2HLT << endl;
  200. funct::RootHistoPdf zPdfMuMu(*histoZMuMu, fMin, fMax);
  201. //assign ZMuMu as pdf
  202. funct::RootHistoPdf zPdfMuMuNonIso = zPdfMuMu;
  203. if (nonIsoTemplateFromMC) {
  204. funct::RootHistoPdf zPdfMuMuNoIsoFromMC(*histoZMuMuNoIsoTemplateFromMC, fMin, fMax);
  205. zPdfMuMuNonIso = zPdfMuMuNoIsoFromMC;
  206. }
  207. funct::RootHistoPdf zPdfMuTk = zPdfMuMu;
  208. funct::RootHistoPdf zPdfMuMu1HLT = zPdfMuMu;
  209. funct::RootHistoPdf zPdfMuMu2HLT = zPdfMuMu;
  210. funct::RootHistoPdf zPdfMuSa(*histoZMuSaFromMuMu, fMin, fMax);
  211. zPdfMuMuNonIso.rebin(rebinMuMuNoIso/rebinMuMu);
  212. zPdfMuTk.rebin(rebinMuTk/rebinMuMu);
  213. zPdfMuMu1HLT.rebin(rebinMuMu1HLT/rebinMuMu);
  214. zPdfMuMu2HLT.rebin(rebinMuMu2HLT/rebinMuMu);
  215. funct::Numerical<2> _2;
  216. funct::Numerical<1> _1;
  217. //Efficiency term
  218. Expr zMuMuEff1HLTTerm = _2 * (effTk ^ _2) * (effSa ^ _2) * (effIso ^ _2) * effHLT * (_1 - effHLT);
  219. Expr zMuMuEff2HLTTerm = (effTk ^ _2) * (effSa ^ _2) * (effIso ^ _2) * (effHLT ^ _2) ;
  220. // Expr zMuMuNoIsoEffTerm = (effTk ^ _2) * (effSa ^ _2) * (_1 - (effIso ^ _2)) * (_1 - ((_1 - effHLT)^_2));
  221. // change to both hlt and one not iso
  222. Expr zMuMuNoIsoEffTerm = _2 * (effTk ^ _2) * (effSa ^ _2) * effIso * (_1 - effIso) * (effHLT^_2);
  223. Expr zMuTkEffTerm = _2 * (effTk ^ _2) * effSa * (_1 - effSa) * (effIso ^ _2) * effHLT;
  224. Expr zMuSaEffTerm = _2 * (effSa ^ _2) * effTk * (_1 - effTk) * (effIso ^ _2) * effHLT;
  225. Expr zMuMu1HLT = rebinMuMu1HLTConst * zMuMuEff1HLTTerm * yieldZMuMu;
  226. Expr zMuMu2HLT = rebinMuMu2HLTConst * zMuMuEff2HLTTerm * yieldZMuMu;
  227. Expr zMuTkBkg = yieldBkgZMuTk * funct::Exponential(lambda)* funct::Polynomial<2>(a0, a1, a2);
  228. Expr zMuTkBkgScaled = rebinMuTkConst * zMuTkBkg;
  229. Expr zMuTk = rebinMuTkConst * (zMuTkEffTerm * yieldZMuMu * zPdfMuTk + zMuTkBkg);
  230. Expr zMuMuNoIsoBkg = yieldBkgZMuMuNotIso * funct::Exponential(alpha)* funct::Polynomial<2>(b0, b1, b2);
  231. Expr zMuMuNoIsoBkgScaled = rebinMuMuNoIsoConst * zMuMuNoIsoBkg;
  232. Expr zMuMuNoIso = rebinMuMuNoIsoConst * (zMuMuNoIsoEffTerm * yieldZMuMu * zPdfMuMuNonIso + zMuMuNoIsoBkg);
  233. Expr zMuSaBkg = yieldBkgZMuSa * funct::Exponential(beta)* funct::Polynomial<2>(c0, c1, c2);
  234. Expr zMuSaBkgScaled = rebinMuSaConst * zMuSaBkg;
  235. Expr zMuSa = rebinMuSaConst * (zMuSaEffTerm * yieldZMuMu * zPdfMuSa + zMuSaBkg );
  236. TH1D histoZCount1HLT("histoZCount1HLT", "", 1, fMin, fMax);
  237. histoZCount1HLT.Fill(100, nZMuMu1HLT);
  238. TH1D histoZCount2HLT("histoZCount2HLT", "", 1, fMin, fMax);
  239. histoZCount2HLT.Fill(100, nZMuMu2HLT);
  240. ChiSquared chi2(zMuMu1HLT, & histoZCount1HLT,
  241. zMuMu2HLT, & histoZCount2HLT,
  242. zMuTk, histoZMuTk,
  243. zMuSa, histoZMuSa,
  244. zMuMuNoIso,histoZMuMuNoIso,
  245. fMin, fMax);
  246. cout << "N. bins: " << chi2.numberOfBins() << endl;
  247. fit::RootMinuit<ChiSquared> minuit(chi2, true);
  248. commands.add(minuit, yieldZMuMu);
  249. commands.add(minuit, effTk);
  250. commands.add(minuit, effSa);
  251. commands.add(minuit, effIso);
  252. commands.add(minuit, effHLT);
  253. commands.add(minuit, yieldBkgZMuTk);
  254. commands.add(minuit, yieldBkgZMuSa);
  255. commands.add(minuit, yieldBkgZMuMuNotIso);
  256. commands.add(minuit, lambda);
  257. commands.add(minuit, alpha);
  258. commands.add(minuit, beta);
  259. commands.add(minuit, a0);
  260. commands.add(minuit, a1);
  261. commands.add(minuit, a2);
  262. commands.add(minuit, b0);
  263. commands.add(minuit, b1);
  264. commands.add(minuit, b2);
  265. commands.add(minuit, c0);
  266. commands.add(minuit, c1);
  267. commands.add(minuit, c2);
  268. commands.run(minuit);
  269. const unsigned int nPar = 20;//WARNIG: this must be updated manually for now
  270. ROOT::Math::SMatrix<double, nPar, nPar, ROOT::Math::MatRepSym<double, nPar> > err;
  271. minuit.getErrorMatrix(err);
  272. std::cout << "error matrix:" << std::endl;
  273. for(unsigned int i = 0; i < nPar; ++i) {
  274. for(unsigned int j = 0; j < nPar; ++j) {
  275. std::cout << err(i, j) << "\t";
  276. }
  277. std::cout << std::endl;
  278. }
  279. minuit.printFitResults();
  280. ofstream myfile;
  281. myfile.open ("fitResult.txt", ios::out | ios::app);
  282. myfile<<"\n";
  283. double Y = minuit.getParameterError("YieldZMuMu");
  284. double dY = minuit.getParameterError("YieldZMuMu", Y);
  285. double tk_eff = minuit.getParameterError("EfficiencyTk");
  286. double dtk_eff = minuit.getParameterError("EfficiencyTk", tk_eff);
  287. double sa_eff = minuit.getParameterError("EfficiencySa");
  288. double dsa_eff = minuit.getParameterError("EfficiencySa", sa_eff);
  289. double iso_eff = minuit.getParameterError("EfficiencyIso");
  290. double diso_eff = minuit.getParameterError("EfficiencyIso", iso_eff);
  291. double hlt_eff = minuit.getParameterError("EfficiencyHLT");
  292. double dhlt_eff = minuit.getParameterError("EfficiencyHLT",hlt_eff);
  293. myfile<< Y <<" "<< dY <<" "<< tk_eff <<" "<< dtk_eff <<" "<< sa_eff << " " << dsa_eff << " " << iso_eff <<" " << diso_eff<< " " << hlt_eff << " " << dhlt_eff << " " <<chi2()/(chi2.numberOfBins()- minuit.numberOfFreeParameters());
  294. myfile.close();
  295. //Plot
  296. double s;
  297. s = 0;
  298. for(int i = 1; i <= histoZMuMuNoIso->GetNbinsX(); ++i)
  299. s += histoZMuMuNoIso->GetBinContent(i);
  300. histoZMuMuNoIso->SetEntries(s);
  301. s = 0;
  302. for(int i = 1; i <= histoZMuMu->GetNbinsX(); ++i)
  303. s += histoZMuMu->GetBinContent(i);
  304. histoZMuMu->SetEntries(s);
  305. s = 0;
  306. for(int i = 1; i <= histoZMuMu1HLT->GetNbinsX(); ++i)
  307. s += histoZMuMu1HLT->GetBinContent(i);
  308. histoZMuMu1HLT->SetEntries(s);
  309. s = 0;
  310. for(int i = 1; i <= histoZMuMu2HLT->GetNbinsX(); ++i)
  311. s += histoZMuMu2HLT->GetBinContent(i);
  312. histoZMuMu2HLT->SetEntries(s);
  313. s = 0;
  314. for(int i = 1; i <= histoZMuTk->GetNbinsX(); ++i)
  315. s += histoZMuTk->GetBinContent(i);
  316. histoZMuTk->SetEntries(s);
  317. s = 0;
  318. for(int i = 1; i <= histoZMuSa->GetNbinsX(); ++i)
  319. s += histoZMuSa->GetBinContent(i);
  320. histoZMuSa->SetEntries(s);
  321. string ZMuMu1HLTPlot = "ZMuMu1HLTFit_" + plot_string;
  322. root::plot<Expr>(ZMuMu1HLTPlot.c_str(), *histoZMuMu1HLT, zMuMu1HLT, fMin, fMax,
  323. effTk, effSa, effIso, effHLT, yieldZMuMu,
  324. kOrange-2, 2, kSolid, 100,
  325. "Z -> #mu #mu mass", "#mu #mu invariant mass (GeV/c^{2})",
  326. "Events");
  327. string ZMuMu2HLTPlot = "ZMuMu2HLTFit_" + plot_string;
  328. root::plot<Expr>(ZMuMu2HLTPlot.c_str(), *histoZMuMu2HLT, zMuMu2HLT, fMin, fMax,
  329. effTk, effSa, effIso, effHLT, yieldZMuMu,
  330. kOrange-2, 2, kSolid, 100,
  331. "Z -> #mu #mu mass", "#mu #mu invariant mass (GeV/c^{2})",
  332. "Events");
  333. string ZMuMuNoIsoPlot = "ZMuMuNoIsoFit_X_" + plot_string;
  334. root::plot<Expr>(ZMuMuNoIsoPlot.c_str(), *histoZMuMuNoIso, zMuMuNoIso, fMin, fMax,
  335. effTk, effSa, effIso, effHLT, yieldZMuMu,
  336. yieldBkgZMuMuNotIso, alpha, b0, b1, b2,
  337. kWhite, 2, kSolid, 100,
  338. "Z -> #mu #mu Not Iso mass", "#mu #mu invariant mass (GeV/c^{2})",
  339. "Events");
  340. ZMuMuNoIsoPlot = "ZMuMuNoIsoFit_" + plot_string;
  341. TF1 funZMuMuNoIso = root::tf1_t<sig_tag, Expr>("ZMuMuNoIsoFunction", zMuMuNoIso, fMin, fMax,
  342. effTk, effSa, effIso, effHLT, yieldZMuMu,
  343. yieldBkgZMuMuNotIso, alpha, b0, b1, b2);
  344. funZMuMuNoIso.SetLineColor(kOrange+8);
  345. funZMuMuNoIso.SetLineWidth(3);
  346. //funZMuMuNoIso.SetLineStyle(kDashed);
  347. //funZMuMuNoIso.SetFillColor(kOrange-2);
  348. //funZMuMuNoIso.SetFillStyle(3325);
  349. funZMuMuNoIso.SetNpx(10000);
  350. TF1 funZMuMuNoIsoBkg = root::tf1_t<bkg_tag, Expr>("ZMuMuNoIsoBack", zMuMuNoIsoBkgScaled, fMin, fMax,
  351. yieldBkgZMuMuNotIso, alpha, b0, b1, b2);
  352. funZMuMuNoIsoBkg.SetLineColor(kViolet+3);
  353. funZMuMuNoIsoBkg.SetLineWidth(2);
  354. funZMuMuNoIsoBkg.SetLineStyle(kSolid);
  355. funZMuMuNoIsoBkg.SetFillColor(kViolet-5);
  356. funZMuMuNoIsoBkg.SetFillStyle(3357);
  357. funZMuMuNoIsoBkg.SetNpx(10000);
  358. histoZMuMuNoIso->SetTitle("Z -> #mu #mu Not Iso mass");
  359. histoZMuMuNoIso->SetXTitle("#mu + #mu invariant mass (GeV/c^{2})");
  360. histoZMuMuNoIso->SetYTitle("Events");
  361. TCanvas *canvas = new TCanvas("canvas");
  362. histoZMuMuNoIso->Draw("e");
  363. funZMuMuNoIsoBkg.Draw("same");
  364. funZMuMuNoIso.Draw("same");
  365. canvas->SaveAs(ZMuMuNoIsoPlot.c_str());
  366. canvas->SetLogy();
  367. string logZMuMuNoIsoPlot = "log_" + ZMuMuNoIsoPlot;
  368. canvas->SaveAs(logZMuMuNoIsoPlot.c_str());
  369. double IntSigMMNotIso = ((double) rebinMuMu/ (double) rebinMuMuNoIso) * funZMuMuNoIso.Integral(fMin, fMax);
  370. double IntSigMMNotIsoBkg = ((double) rebinMuMu/ (double) rebinMuMuNoIso) * funZMuMuNoIsoBkg.Integral(fMin, fMax);
  371. cout << "********* ZMuMuNoIsoPlot signal yield from the fit ==> " << IntSigMMNotIso << endl;
  372. cout << "********* ZMuMuNoIsoPlot background yield from the fit ==> " << IntSigMMNotIsoBkg << endl;
  373. string ZMuTkPlot = "ZMuTkFit_X_" + plot_string;
  374. root::plot<Expr>(ZMuTkPlot.c_str(), *histoZMuTk, zMuTk, fMin, fMax,
  375. effTk, effSa, effIso, effHLT, yieldZMuMu,
  376. yieldBkgZMuTk, lambda, a0, a1, a2,
  377. kOrange+3, 2, kSolid, 100,
  378. "Z -> #mu + (unmatched) track mass", "#mu #mu invariant mass (GeV/c^{2})",
  379. "Events");
  380. ZMuTkPlot = "ZMuTkFit_" + plot_string;
  381. TF1 funZMuTk = root::tf1_t<sig_tag, Expr>("ZMuTkFunction", zMuTk, fMin, fMax,
  382. effTk, effSa, effIso, effHLT, yieldZMuMu,
  383. yieldBkgZMuTk, lambda, a0, a1, a2);
  384. funZMuTk.SetLineColor(kOrange+8);
  385. funZMuTk.SetLineWidth(3);
  386. funZMuTk.SetLineStyle(kSolid);
  387. // funZMuTk.SetFillColor(kOrange-2);
  388. //funZMuTk.SetFillStyle(3325);
  389. funZMuTk.SetNpx(10000);
  390. TF1 funZMuTkBkg = root::tf1_t<bkg_tag, Expr>("ZMuTkBack", zMuTkBkgScaled, fMin, fMax,
  391. yieldBkgZMuTk, lambda, a0, a1, a2);
  392. funZMuTkBkg.SetLineColor(kViolet+3);
  393. funZMuTkBkg.SetLineWidth(2);
  394. funZMuTkBkg.SetLineStyle(kSolid);
  395. funZMuTkBkg.SetFillColor(kViolet-5);
  396. funZMuTkBkg.SetFillStyle(3357);
  397. funZMuTkBkg.SetNpx(10000);
  398. histoZMuTk->SetTitle("Z -> #mu + (unmatched) track mass");
  399. histoZMuTk->SetXTitle("#mu + (unmatched) track invariant mass (GeV/c^{2})");
  400. histoZMuTk->SetYTitle("Events");
  401. TCanvas *canvas_ = new TCanvas("canvas_");
  402. histoZMuTk->Draw("e");
  403. funZMuTkBkg.Draw("same");
  404. funZMuTk.Draw("same");
  405. canvas_->SaveAs(ZMuTkPlot.c_str());
  406. canvas_->SetLogy();
  407. string logZMuTkPlot = "log_" + ZMuTkPlot;
  408. canvas_->SaveAs(logZMuTkPlot.c_str());
  409. double IntSigMT = ((double) rebinMuMu/ (double) rebinMuTk) * funZMuTk.Integral(fMin, fMax);
  410. double IntSigMTBkg = ((double) rebinMuMu/ (double) rebinMuTk) * funZMuTkBkg.Integral(fMin, fMax);
  411. cout << "********* ZMuMuTkPlot signal yield from the fit ==> " << IntSigMT << endl;
  412. cout << "********* ZMuMuTkPlot background yield from the fit ==> " << IntSigMTBkg << endl;
  413. string ZMuSaPlot = "ZMuSaFit_X_" + plot_string;
  414. root::plot<Expr>(ZMuSaPlot.c_str(), *histoZMuSa, zMuSa, fMin, fMax,
  415. effSa, effTk, effIso, yieldZMuMu,
  416. yieldBkgZMuSa, beta, c0, c1, c2 ,
  417. kOrange+3, 2, kSolid, 100,
  418. "Z -> #mu + (unmatched) standalone mass",
  419. "#mu + (unmatched) standalone invariant mass (GeV/c^{2})",
  420. "Events");
  421. ZMuSaPlot = "ZMuSaFit_" + plot_string;
  422. TF1 funZMuSa = root::tf1_t<sig_tag, Expr>("ZMuSaFunction", zMuSa, fMin, fMax,
  423. effTk, effSa, effIso, effHLT, yieldZMuMu,
  424. yieldBkgZMuSa, beta, c0, c1, c2);
  425. funZMuSa.SetLineColor(kOrange+8);
  426. funZMuSa.SetLineWidth(3);
  427. funZMuSa.SetLineStyle(kSolid);
  428. // funZMuSa.SetFillColor(kOrange-2);
  429. // funZMuSa.SetFillStyle(3325);
  430. funZMuSa.SetNpx(10000);
  431. TF1 funZMuSaBkg = root::tf1_t<bkg_tag, Expr>("ZMuSaBack", zMuSaBkgScaled, fMin, fMax,
  432. yieldBkgZMuSa, beta, c0, c1, c2);
  433. funZMuSaBkg.SetLineColor(kViolet+3);
  434. funZMuSaBkg.SetLineWidth(2);
  435. funZMuSaBkg.SetLineStyle(kSolid);
  436. funZMuSaBkg.SetFillColor(kViolet-5);
  437. funZMuSaBkg.SetFillStyle(3357);
  438. funZMuSaBkg.SetNpx(10000);
  439. histoZMuSa->SetTitle("Z -> #mu + (unmatched) standalone mass");
  440. histoZMuSa->SetXTitle("#mu + (unmatched) standalone invariant mass (GeV/c^{2})");
  441. histoZMuSa->SetYTitle("Events");
  442. TCanvas *canvas__ = new TCanvas("canvas__");
  443. histoZMuSa->Draw("e");
  444. funZMuSaBkg.Draw("same");
  445. funZMuSa.Draw("same");
  446. canvas__->SaveAs(ZMuSaPlot.c_str());
  447. canvas__->SetLogy();
  448. string logZMuSaPlot = "log_" + ZMuSaPlot;
  449. canvas__->SaveAs(logZMuSaPlot.c_str());
  450. double IntSigMS = ((double) rebinMuMu/ (double) rebinMuSa) * funZMuSa.Integral(fMin, fMax);
  451. double IntSigMSBkg = ((double) rebinMuMu/ (double) rebinMuSa) * funZMuSaBkg.Integral(fMin, fMax);
  452. cout << "********* ZMuMuSaPlot signal yield from the fit ==> " << IntSigMS << endl;
  453. cout << "********* ZMuMuSaPlot background yield from the fit ==> " << IntSigMSBkg << endl;
  454. }
  455. return 0;
  456. }
  457. #include <boost/program_options.hpp>
  458. using namespace boost;
  459. namespace po = boost::program_options;
  460. int main(int ac, char *av[]) {
  461. po::options_description desc("Allowed options");
  462. desc.add_options()
  463. ("help,h", "produce help message")
  464. ("input-file,i", po::value<vector<string> >(), "input file")
  465. ("min,m", po::value<double>(&fMin)->default_value(60), "minimum value for fit range")
  466. ("max,M", po::value<double>(&fMax)->default_value(120), "maximum value for fit range")
  467. ("rebins,R", po::value<vector<unsigned int> >(), "rebins values: rebinMuMu2HLT , rebinMuMu1HLT , rebinMuMuNoIso , rebinMuSa, rebinMuTk")
  468. ("chi2,c", "perform chi-squared fit")
  469. ("plr,p", "perform Poisson likelihood-ratio fit")
  470. ("nonIsoTemplateFromMC,I", po::value<bool>(&nonIsoTemplateFromMC)->default_value(false) , "take the template for nonIso sample from MC")
  471. ("plot-format,f", po::value<string>(&ext)->default_value("eps"), "output plot format")
  472. ("detectorRegion,r",po::value<string> (&region)->default_value("all"), "detector region in which muons are detected" );
  473. po::positional_options_description p;
  474. p.add("input-file", -1);
  475. p.add("rebins", -1);
  476. po::variables_map vm;
  477. po::store(po::command_line_parser(ac, av).
  478. options(desc).positional(p).run(), vm);
  479. po::notify(vm);
  480. if (vm.count("help")) {
  481. cout << "Usage: options_description [options]\n";
  482. cout << desc;
  483. return 0;
  484. }
  485. if (!vm.count("input-file")) {
  486. return 1;
  487. }
  488. cout << "Input files are: "
  489. << vm["input-file"].as< vector<string> >() << "\n";
  490. vector<string> v_file = vm["input-file"].as< vector<string> >();
  491. if (vm.count("rebins") ) {
  492. vector<unsigned int> v_rebin = vm["rebins"].as< vector<unsigned int> >();
  493. if (v_rebin.size()!=5){
  494. cerr << " please provide 5 numbers in the given order: rebinMuMu2HLT , rebinMuMu1HLT , rebinMuMuNoIso, rebinMuSa, rebinMuTk \n";
  495. return 1;
  496. }
  497. rebinMuMuNoIso = v_rebin[2], rebinMuMu1HLT = v_rebin[1], rebinMuMu2HLT = v_rebin[0], rebinMuTk = v_rebin[4], rebinMuSa = v_rebin[3];
  498. }
  499. bool chi2Fit = vm.count("chi2"), plrFit = vm.count("plr");
  500. if(!(chi2Fit||plrFit))
  501. cerr << "Warning: no fit performed. Please, specify either -c or -p options or both" << endl;
  502. gROOT->SetStyle("Plain");
  503. int ret = 0;
  504. try {
  505. if(plrFit) {
  506. std::cout << "==================================== " << std::endl;
  507. std::cout << "=== Poisson Likelihood Ratio fit === " << std::endl;
  508. std::cout << "==================================== " << std::endl;
  509. int ret2 = main_t<ExprPLR>(v_file);
  510. if(ret2 != 0) ret = 1;
  511. }
  512. if(chi2Fit) {
  513. std::cout << "================= " << std::endl;
  514. std::cout << "=== Chi-2 fit === " << std::endl;
  515. std::cout << "================= " << std::endl;
  516. int ret1 = main_t<ExprChi2>(v_file);
  517. if(ret1 != 0) ret = 1;
  518. }
  519. }
  520. catch(std::exception& e) {
  521. cerr << "error: " << e.what() << "\n";
  522. return 1;
  523. }
  524. catch(...) {
  525. cerr << "Exception of unknown type!\n";
  526. }
  527. return ret;
  528. }