Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:40:00

0001 #include <cmath>
0002 #include <fstream>
0003 #include <iomanip>
0004 #include <iostream>
0005 #include <string>
0006 
0007 #include "toolbox.h"
0008 #include "FitWithRooFit.cc"
0009 #include "Options.h"
0010 
0011 #include "TAxis.h"
0012 #include "TBranch.h"
0013 #include "TCanvas.h"
0014 #include "TChain.h"
0015 #include "TCut.h"
0016 #include "TF1.h"
0017 #include "TFile.h"
0018 #include "TFrame.h"
0019 #include "TH1.h"
0020 #include "TH1D.h"
0021 #include "TH1F.h"
0022 #include "TH2.h"
0023 #include "TH2F.h"
0024 #include "THStack.h"
0025 #include "TLatex.h"
0026 #include "TLegend.h"
0027 #include "TMath.h"
0028 #include "TPad.h"
0029 #include "TPaveText.h"
0030 #include "TRandom.h"
0031 #include "TString.h"
0032 #include "TStyle.h"
0033 #include "TSystem.h"
0034 #include "TTree.h"
0035 //#include "RooGlobalFunc.h"
0036 
0037 #include <boost/filesystem.hpp>
0038 #include <boost/property_tree/ptree.hpp>
0039 #include <boost/property_tree/json_parser.hpp>
0040 #include <boost/range/adaptor/indexed.hpp>
0041 
0042 using namespace RooFit;
0043 using namespace std;
0044 using namespace AllInOneConfig;
0045 namespace pt = boost::property_tree;
0046 /*--------------------------------------------------------------------*/
0047 void makeNicePlotStyle(RooPlot* plot) {
0048   plot->GetXaxis()->CenterTitle(true);
0049   plot->GetYaxis()->CenterTitle(true);
0050   plot->GetXaxis()->SetTitleFont(42);
0051   plot->GetYaxis()->SetTitleFont(42);
0052   plot->GetXaxis()->SetTitleSize(0.05);
0053   plot->GetYaxis()->SetTitleSize(0.05);
0054   plot->GetXaxis()->SetTitleOffset(0.9);
0055   plot->GetYaxis()->SetTitleOffset(1.3);
0056   plot->GetXaxis()->SetLabelFont(42);
0057   plot->GetYaxis()->SetLabelFont(42);
0058   plot->GetYaxis()->SetLabelSize(.05);
0059   plot->GetXaxis()->SetLabelSize(.05);
0060 }
0061 /*--------------------------------------------------------------------*/
0062 
0063 RooRealVar MuMu_mass("MuMu_mass", "MuMu_mass", 70, 110);
0064 static TString GT = "";
0065 TLatex* tlxg = new TLatex();
0066 class FitOut {
0067 public:
0068   double mean;
0069   double mean_err;
0070   double sigma;
0071   double sigma_err;
0072   double chi2;
0073   FitOut(double a, double b, double c, double d) : mean(a), mean_err(b), sigma(c), sigma_err(d) {}
0074 };
0075 
0076 FitOut ZMassBinFit_OldTool(TH1D* th1d_input, TString s_name = "zmumu_fitting", TString output_path = "./") {
0077   double xMin(75), xMax(105), xMean(91);
0078   double sigma = 2;
0079   double sigmaMin = 0.1;
0080   double sigmaMax = 10;
0081 
0082   FitWithRooFit* fitter = new FitWithRooFit();
0083   double sigma2(0.1), sigma2Min(0.), sigma2Max(10.), useChi2(false);
0084   fitter->useChi2_ = useChi2;
0085   fitter->initMean(xMean, xMin, xMax);
0086   fitter->initSigma(sigma, sigmaMin, sigmaMax);
0087   fitter->initSigma2(sigma2, sigma2Min, sigma2Max);
0088   fitter->initAlpha(1.5, 0.05, 10.);
0089   fitter->initN(1, 0.01, 100.);
0090   fitter->initFGCB(0.4, 0., 1.);
0091 
0092   fitter->initMean(91.1876, xMin, xMax);
0093   fitter->initGamma(2.4952, 0., 10.);
0094   fitter->gamma()->setConstant(kTRUE);
0095   fitter->initMean2(0., -20., 20.);
0096   fitter->mean2()->setConstant(kTRUE);
0097   fitter->initSigma(1.2, 0., 5.);
0098   fitter->initAlpha(1.5, 0.05, 10.);
0099   fitter->initN(1, 0.01, 100.);
0100   fitter->initExpCoeffA0(-1., -10., 10.);
0101   fitter->initExpCoeffA1(0., -10., 10.);
0102   fitter->initExpCoeffA2(0., -2., 2.);
0103   fitter->initFsig(0.9, 0., 1.);
0104   fitter->initA0(0., -10., 10.);
0105   fitter->initA1(0., -10., 10.);
0106   fitter->initA2(0., -10., 10.);
0107   fitter->initA3(0., -10., 10.);
0108   fitter->initA4(0., -10., 10.);
0109   fitter->initA5(0., -10., 10.);
0110   fitter->initA6(0., -10., 10.);
0111   TCanvas* c1 = new TCanvas();
0112   c1->Clear();
0113   c1->SetLeftMargin(0.15);
0114   c1->SetRightMargin(0.10);
0115 
0116   fitter->fit(th1d_input, "breitWignerTimesCB", "exponential", xMin, xMax, false);
0117 
0118   c1->Print(Form("%s/fitResultPlot/%s_oldtool.pdf", output_path.Data(), s_name.Data()));
0119   c1->Print(Form("%s/fitResultPlot/%s_oldtool.root", output_path.Data(), s_name.Data()));
0120 
0121   FitOut fitRes(
0122       fitter->mean()->getValV(), fitter->mean()->getError(), fitter->sigma()->getValV(), fitter->sigma()->getError());
0123   return fitRes;
0124 }
0125 void Draw_th1d(TH1D* th1d_input, TString variable_name) {
0126   TCanvas* c = new TCanvas();
0127   c->cd();
0128   gStyle->SetOptStat(0);
0129   th1d_input->SetMarkerStyle(kFullCircle);
0130   th1d_input->SetMarkerColor(kRed);
0131   th1d_input->SetLineColor(kRed);
0132   th1d_input->SetMaximum(91.4);
0133   th1d_input->SetMinimum(90.85);
0134   th1d_input->GetXaxis()->SetTitle(variable_name.Data());
0135   th1d_input->GetXaxis()->SetTitleOffset(1.2);
0136   th1d_input->GetYaxis()->SetTitle("Mass mean (GeV)");
0137   th1d_input->Draw();
0138   tlxg->DrawLatexNDC(0.2, 0.8, Form("%s", GT.Data()));
0139   c->Print(Form("%s/fitResultPlot/mass_VS_%s.pdf", GT.Data(), variable_name.Data()));
0140 }
0141 
0142 const static int variables_number = 8;
0143 const TString tstring_variables_name[variables_number] = {
0144     "CosThetaCS", "DeltaEta", "EtaMinus", "EtaPlus", "PhiCS", "PhiMinus", "PhiPlus", "Pt"};
0145 void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path) {
0146   TH2D* th2d_mass_variables[variables_number];
0147   TFile* inputfile = TFile::Open(inputfile_name.Data());
0148   TDirectoryFile* tdirectory = (TDirectoryFile*)inputfile->Get("myanalysis");
0149   for (int i = 0; i < variables_number; i++) {
0150     TString th2d_name = Form("th2d_mass_%s", tstring_variables_name[i].Data());
0151     th2d_mass_variables[i] = (TH2D*)tdirectory->Get(th2d_name);
0152   }
0153 
0154   gSystem->Exec(Form("mkdir -p %s", output_path.Data()));
0155   gSystem->Exec(Form("mkdir -p %s/fitResultPlot", output_path.Data()));
0156   TFile* outpufile = TFile::Open(Form("%s/fitting_output.root", output_path.Data()), "recreate");
0157   TH1D* th1d_variables_meanmass[variables_number];
0158   TH1D* th1d_variables_entries[variables_number];
0159   const int variables_rebin[variables_number] = {1, 1, 1, 1, 1, 1, 1, 1};
0160   const double xaxis_range[variables_number][2] = {
0161       {-1, 1}, {-4.8, 4.8}, {-2.4, 2.4}, {-2.4, 2.4}, {-1, 1}, {-M_PI, M_PI}, {-M_PI, M_PI}, {0, 100}};
0162   for (int i = 0; i < variables_number; i++) {
0163     TString th1d_name = Form("th1d_meanmass_%s", tstring_variables_name[i].Data());
0164 
0165     th2d_mass_variables[i]->RebinY(variables_rebin[i]);
0166     th1d_variables_meanmass[i] = th2d_mass_variables[i]->ProjectionY(th1d_name, 1, 1, "d");
0167     for (int j = 0; j < th1d_variables_meanmass[i]->GetNbinsX(); j++) {
0168       if (i == 7 and j > 25) {
0169         continue;
0170       }
0171       cout << "th1d_variables_meanmass[i]->GetNbinsX()=" << th1d_variables_meanmass[i]->GetNbinsX() << endl;
0172       cout << "th2d_mass_variables[i]->GetNbinsY()=" << th2d_mass_variables[i]->GetNbinsY() << endl;
0173       th1d_variables_meanmass[i]->SetBinContent(j, 0);
0174       th1d_variables_meanmass[i]->SetBinError(j, 0);
0175 
0176       TString th1d_mass_temp_name = Form("th1d_mass_%s_%d", tstring_variables_name[i].Data(), j);
0177       TH1D* th1d_i = th2d_mass_variables[i]->ProjectionX(th1d_mass_temp_name, j, j, "d");
0178       th1d_i->Write(th1d_mass_temp_name);
0179       TString s_cut = Form("nocut");
0180       TString s_name = Form("%s_%d", tstring_variables_name[i].Data(), j);
0181 
0182       FitOut fitR = ZMassBinFit_OldTool(th1d_i, s_name, output_path);
0183       th1d_variables_meanmass[i]->SetBinContent(j, fitR.mean);
0184       th1d_variables_meanmass[i]->SetBinError(j, fitR.mean_err);
0185     }
0186     th1d_variables_meanmass[i]->GetXaxis()->SetRangeUser(xaxis_range[i][0], xaxis_range[i][1]);
0187     Draw_th1d(th1d_variables_meanmass[i], tstring_variables_name[i]);
0188     outpufile->cd();
0189     th1d_variables_meanmass[i]->Write(th1d_name);
0190 
0191     TString th1d_name_entries = Form("th1d_entries_%s", tstring_variables_name[i].Data());
0192     th1d_variables_entries[i] = th2d_mass_variables[i]->ProjectionY(th1d_name_entries, 0, -1, "d");
0193     th1d_variables_entries[i]->GetXaxis()->SetTitle(tstring_variables_name[i].Data());
0194     th1d_variables_entries[i]->GetYaxis()->SetTitle("Entry");
0195     outpufile->cd();
0196     th1d_variables_entries[i]->Write(th1d_name_entries);
0197   }
0198 
0199   outpufile->Write();
0200   outpufile->Close();
0201   delete outpufile;
0202 }
0203 
0204 const static int max_file_number = 10;
0205 void Draw_TH1D_forMultiRootFiles(const vector<TString>& file_names,
0206                                  const vector<TString>& label_names,
0207                                  const vector<int>& colors,
0208                                  const vector<int>& styles,
0209                                  const TString& th1d_name,
0210                                  const TString& output_name) {
0211   if (file_names.empty() || label_names.empty()) {
0212     cout << "Provided an empty list of file and label names" << std::endl;
0213     return;
0214   }
0215 
0216   // do not allow the list of files and labels names to differ
0217   assert(file_names.size() == label_names.size());
0218 
0219   TH1D* th1d_input[max_file_number];
0220   TFile* file_input[max_file_number];
0221   for (auto const& filename : file_names | boost::adaptors::indexed(0)) {
0222     file_input[filename.index()] = TFile::Open(filename.value());
0223     th1d_input[filename.index()] = (TH1D*)file_input[filename.index()]->Get(th1d_name);
0224     th1d_input[filename.index()]->SetTitle("");
0225   }
0226 
0227   TCanvas* c = new TCanvas();
0228   TLegend* lg = new TLegend(0.2, 0.7, 0.5, 0.95);
0229   c->cd();
0230   gStyle->SetOptStat(0);
0231 
0232   for (auto const& labelname : label_names | boost::adaptors::indexed(0)) {
0233     th1d_input[labelname.index()]->SetMarkerColor(colors[labelname.index()]);
0234     th1d_input[labelname.index()]->SetLineColor(colors[labelname.index()]);
0235     th1d_input[labelname.index()]->SetMarkerStyle(styles[labelname.index()]);
0236     th1d_input[labelname.index()]->Draw("same");
0237     lg->AddEntry(th1d_input[labelname.index()], labelname.value());
0238   }
0239   lg->Draw("same");
0240   c->SaveAs(output_name);
0241 }
0242 
0243 int Zmumumerge(int argc, char* argv[]) {
0244   vector<TString> vec_single_file_path;
0245   vector<TString> vec_single_file_name;
0246   vector<TString> vec_global_tag;
0247   vector<int> vec_color;
0248   vector<int> vec_style;
0249 
0250   Options options;
0251   options.helper(argc, argv);
0252   options.parser(argc, argv);
0253   pt::ptree main_tree;
0254   pt::read_json(options.config, main_tree);
0255   pt::ptree alignments = main_tree.get_child("alignments");
0256   pt::ptree validation = main_tree.get_child("validation");
0257   for (const auto& childTree : alignments) {
0258     vec_single_file_path.push_back(childTree.second.get<std::string>("file"));
0259     vec_single_file_name.push_back(childTree.second.get<std::string>("file") + "/Zmumu.root");
0260     vec_color.push_back(childTree.second.get<int>("color"));
0261     vec_style.push_back(childTree.second.get<int>("style"));
0262     vec_global_tag.push_back(childTree.second.get<std::string>("globaltag"));
0263 
0264     //Fitting_GetMassmeanVSvariables(childTree.second.get<std::string>("file") + "/Zmumu.root", childTree.second.get<std::string>("file"));
0265   }
0266   TString merge_output = main_tree.get<std::string>("output");
0267   //=============================================
0268   vector<TString> vec_single_fittingoutput;
0269   vec_single_fittingoutput.clear();
0270   for (unsigned i = 0; i < vec_single_file_path.size(); i++) {
0271     Fitting_GetMassmeanVSvariables(vec_single_file_name[i], vec_single_file_path[i]);
0272     vec_single_fittingoutput.push_back(vec_single_file_path[i] + "/fitting_output.root");
0273   }
0274 
0275   int files_number = vec_single_file_path.size();
0276   cout << "files_number=" << files_number << endl;
0277   for (int idx_variable = 0; idx_variable < variables_number; idx_variable++) {
0278     TString th1d_name = Form("th1d_meanmass_%s", tstring_variables_name[idx_variable].Data());
0279     Draw_TH1D_forMultiRootFiles(
0280         vec_single_fittingoutput,
0281         vec_global_tag,
0282         vec_color,
0283         vec_style,
0284         th1d_name,
0285         merge_output + Form("/meanmass_%s_GTs.pdf", tstring_variables_name[idx_variable].Data()));
0286     TString th1d_name_entries = Form("th1d_entries_%s", tstring_variables_name[idx_variable].Data());
0287     Draw_TH1D_forMultiRootFiles(
0288         vec_single_fittingoutput,
0289         vec_global_tag,
0290         vec_color,
0291         vec_style,
0292         th1d_name_entries,
0293         merge_output + Form("/entries_%s_GTs.pdf", tstring_variables_name[idx_variable].Data()));
0294   }
0295   //=============================================
0296   return EXIT_SUCCESS;
0297 }
0298 #ifndef DOXYGEN_SHOULD_SKIP_THIS
0299 int main(int argc, char* argv[]) { return Zmumumerge(argc, argv); }
0300 #endif