Skip to content

Commit

Permalink
Merge pull request #44257 from mmusich/mm_dev_addUnitTests_forZmmVali…
Browse files Browse the repository at this point in the history
…dation_14_0_X

[14.0.X]  Tracker alignment offline validation tools: miscellaneous fixes to muon-related validations
  • Loading branch information
cmsbuild authored Mar 4, 2024
2 parents 72712d1 + 5446535 commit e17a8af
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 69 deletions.
10 changes: 7 additions & 3 deletions Alignment/OfflineValidation/bin/DiMuonVmerge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ int merge(int argc, char* argv[]) {
// std::cout << " Attribute: " << attr.first << " = " << attr.second.data() << std::endl;
// }

std::cout << childTree.second.get<string>("file") << std::endl;
std::string file = childTree.second.get<string>("file");
std::cout << file << std::endl;
std::cout << childTree.second.get<string>("title") << std::endl;
std::string toAdd = childTree.second.get<string>("file") +
"/DiMuonVertexValidation.root=" + childTree.second.get<string>("title") + ",";

// Check if the file contains "/eos/cms/" and add the prefix accordingly
std::string prefixToAdd = file.find("/eos/cms/") != std::string::npos ? "root://eoscms.cern.ch/" : "";
std::string toAdd =
prefixToAdd + file + "/DiMuonVertexValidation.root=" + childTree.second.get<string>("title") + ",";
filesAndLabels += toAdd;
}

Expand Down
189 changes: 163 additions & 26 deletions Alignment/OfflineValidation/bin/Zmumumerge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <boost/range/adaptor/indexed.hpp>

#include "Alignment/OfflineValidation/interface/FitWithRooFit.h"
#include "Alignment/OfflineValidation/macros/CMS_lumi.h"

using namespace RooFit;
using namespace std;
Expand Down Expand Up @@ -75,22 +76,32 @@ class FitOut {
};

FitOut ZMassBinFit_OldTool(TH1D* th1d_input, TString s_name = "zmumu_fitting", TString output_path = "./") {
double xMin(75), xMax(105), xMean(91);
double sigma = 2;
double sigmaMin = 0.1;
double sigmaMax = 10;
// silence messages
RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);

double xMean = 91.1876;
double xMin = th1d_input->GetXaxis()->GetXmin();
double xMax = th1d_input->GetXaxis()->GetXmax();

double sigma(2.);
double sigmaMin(0.1);
double sigmaMax(10.);

double sigma2(0.1);
double sigma2Min(0.);
double sigma2Max(10.);

std::unique_ptr<FitWithRooFit> fitter = std::make_unique<FitWithRooFit>();

bool useChi2(false);

FitWithRooFit* fitter = new FitWithRooFit();
double sigma2(0.1), sigma2Min(0.), sigma2Max(10.), useChi2(false);
fitter->useChi2_ = useChi2;
fitter->initMean(xMean, xMin, xMax);
fitter->initSigma(sigma, sigmaMin, sigmaMax);
fitter->initSigma2(sigma2, sigma2Min, sigma2Max);
fitter->initAlpha(1.5, 0.05, 10.);
fitter->initN(1, 0.01, 100.);
fitter->initFGCB(0.4, 0., 1.);

fitter->initMean(91.1876, xMin, xMax);
fitter->initGamma(2.4952, 0., 10.);
fitter->gamma()->setConstant(kTRUE);
fitter->initMean2(0., -20., 20.);
Expand All @@ -109,6 +120,7 @@ FitOut ZMassBinFit_OldTool(TH1D* th1d_input, TString s_name = "zmumu_fitting", T
fitter->initA4(0., -10., 10.);
fitter->initA5(0., -10., 10.);
fitter->initA6(0., -10., 10.);

TCanvas* c1 = new TCanvas();
c1->Clear();
c1->SetLeftMargin(0.15);
Expand All @@ -121,9 +133,10 @@ FitOut ZMassBinFit_OldTool(TH1D* th1d_input, TString s_name = "zmumu_fitting", T

FitOut fitRes(
fitter->mean()->getValV(), fitter->mean()->getError(), fitter->sigma()->getValV(), fitter->sigma()->getError());

return fitRes;
}
void Draw_th1d(TH1D* th1d_input, TString variable_name) {
void Draw_th1d(TH1D* th1d_input, TString variable_name, TString output_path) {
TCanvas* c = new TCanvas();
c->cd();
gStyle->SetOptStat(0);
Expand All @@ -137,12 +150,21 @@ void Draw_th1d(TH1D* th1d_input, TString variable_name) {
th1d_input->GetYaxis()->SetTitle("Mass mean (GeV)");
th1d_input->Draw();
tlxg->DrawLatexNDC(0.2, 0.8, Form("%s", GT.Data()));
c->Print(Form("%s/fitResultPlot/mass_VS_%s.pdf", GT.Data(), variable_name.Data()));
c->Print(Form("%s/fitResultPlot/mass_VS_%s.pdf", output_path.Data(), variable_name.Data()));
}

const static int variables_number = 8;
const TString tstring_variables_name[variables_number] = {
"CosThetaCS", "DeltaEta", "EtaMinus", "EtaPlus", "PhiCS", "PhiMinus", "PhiPlus", "Pt"};
const TString tstring_variables_name_label[variables_number] = {"cos #theta_{CS}",
"#Delta #eta",
"#eta_{#mu^{-}}",
"#eta_{#mu^{+}}",
"#phi_{CS}",
"#phi_{#mu^{-}}",
"#phi_{#mu^{+}}",
"p_{T}"};

void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path) {
TH2D* th2d_mass_variables[variables_number];
TFile* inputfile = TFile::Open(inputfile_name.Data());
Expand All @@ -154,7 +176,7 @@ void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path)

gSystem->Exec(Form("mkdir -p %s", output_path.Data()));
gSystem->Exec(Form("mkdir -p %s/fitResultPlot", output_path.Data()));
TFile* outpufile = TFile::Open(Form("%s/fitting_output.root", output_path.Data()), "recreate");
TFile* outputfile = TFile::Open(Form("%s/fitting_output.root", output_path.Data()), "RECREATE");
TH1D* th1d_variables_meanmass[variables_number];
TH1D* th1d_variables_entries[variables_number];
const int variables_rebin[variables_number] = {1, 1, 1, 1, 1, 1, 1, 1};
Expand All @@ -169,8 +191,10 @@ void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path)
if (i == 7 and j > 25) {
continue;
}
cout << "th1d_variables_meanmass[i]->GetNbinsX()=" << th1d_variables_meanmass[i]->GetNbinsX() << endl;
cout << "th2d_mass_variables[i]->GetNbinsY()=" << th2d_mass_variables[i]->GetNbinsY() << endl;
std::cout << __PRETTY_FUNCTION__
<< " th1d_variables_meanmass[i]->GetNbinsX()=" << th1d_variables_meanmass[i]->GetNbinsX() << endl;
std::cout << __PRETTY_FUNCTION__ << " th2d_mass_variables[i]->GetNbinsY()=" << th2d_mass_variables[i]->GetNbinsY()
<< endl;
th1d_variables_meanmass[i]->SetBinContent(j, 0);
th1d_variables_meanmass[i]->SetBinError(j, 0);

Expand All @@ -181,36 +205,51 @@ void Fitting_GetMassmeanVSvariables(TString inputfile_name, TString output_path)
TString s_name = Form("%s_%d", tstring_variables_name[i].Data(), j);

FitOut fitR = ZMassBinFit_OldTool(th1d_i, s_name, output_path);

th1d_variables_meanmass[i]->SetBinContent(j, fitR.mean);
th1d_variables_meanmass[i]->SetBinError(j, fitR.mean_err);
}

th1d_variables_meanmass[i]->GetXaxis()->SetRangeUser(xaxis_range[i][0], xaxis_range[i][1]);
Draw_th1d(th1d_variables_meanmass[i], tstring_variables_name[i]);
outpufile->cd();
Draw_th1d(th1d_variables_meanmass[i], tstring_variables_name[i], output_path);
outputfile->cd();
th1d_variables_meanmass[i]->Write(th1d_name);

TString th1d_name_entries = Form("th1d_entries_%s", tstring_variables_name[i].Data());
th1d_variables_entries[i] = th2d_mass_variables[i]->ProjectionY(th1d_name_entries, 0, -1, "d");
th1d_variables_entries[i]->GetXaxis()->SetTitle(tstring_variables_name[i].Data());
th1d_variables_entries[i]->GetYaxis()->SetTitle("Entry");
outpufile->cd();
outputfile->cd();
th1d_variables_entries[i]->Write(th1d_name_entries);
}

outpufile->Write();
outpufile->Close();
delete outpufile;
if (outputfile->IsOpen()) {
// Get the path (current working directory) in which the file is going to be written
const char* path = outputfile->GetPath();

if (path) {
std::cout << "File is going to be written in the directory: " << path << " for input file: " << inputfile_name
<< std::endl;
} else {
std::cerr << "Error: Unable to determine the path." << std::endl;
}
outputfile->Close();
delete outputfile;
}
}

const static int max_file_number = 10;
void Draw_TH1D_forMultiRootFiles(const vector<TString>& file_names,
const vector<TString>& label_names,
const vector<int>& colors,
const vector<int>& styles,
const TString& Rlabel,
const TString& th1d_name,
const TString& xlabel,
const TString& ylabel,
const TString& output_name) {
if (file_names.empty() || label_names.empty()) {
cout << "Provided an empty list of file and label names" << std::endl;
std::cout << "Provided an empty list of file and label names" << std::endl;
return;
}

Expand All @@ -225,24 +264,96 @@ void Draw_TH1D_forMultiRootFiles(const vector<TString>& file_names,
th1d_input[filename.index()]->SetTitle("");
}

TCanvas* c = new TCanvas();
TLegend* lg = new TLegend(0.2, 0.7, 0.5, 0.95);
c->cd();
int W = 800;
int H = 800;
// references for T, B, L, R
float T = 0.08 * H;
float B = 0.12 * H;
float L = 0.12 * W;
float R = 0.04 * W;

// Form the canvas name by appending th1d_name
TString canvasName;
canvasName.Form("canv_%s", th1d_name.Data());

// Create a new canvas with the formed name
TCanvas* canv = new TCanvas(canvasName, canvasName, W, H);
canv->SetFillColor(0);
canv->SetBorderMode(0);
canv->SetFrameFillStyle(0);
canv->SetFrameBorderMode(0);
canv->SetLeftMargin(L / W + 0.05);
canv->SetRightMargin(R / W);
canv->SetTopMargin(T / H);
canv->SetBottomMargin(B / H);
canv->SetTickx(0);
canv->SetTicky(0);
canv->SetGrid();
canv->cd();

gStyle->SetOptStat(0);

TLegend* lg = new TLegend(0.3, 0.7, 0.7, 0.9);
lg->SetFillStyle(0);
lg->SetLineColor(0);
lg->SetEntrySeparation(0.05);

double ymin;
double ymax;

for (auto const& labelname : label_names | boost::adaptors::indexed(0)) {
double temp_ymin = th1d_input[labelname.index()]->GetMinimum();
double temp_ymax = th1d_input[labelname.index()]->GetMaximum();
if (labelname.index() == 0) {
ymin = temp_ymin;
ymax = temp_ymax;
}
if (temp_ymin <= ymin) {
ymin = temp_ymin;
}
if (temp_ymax >= ymax) {
ymax = temp_ymax;
}
}

for (auto const& labelname : label_names | boost::adaptors::indexed(0)) {
th1d_input[labelname.index()]->SetMarkerColor(colors[labelname.index()]);
th1d_input[labelname.index()]->SetLineColor(colors[labelname.index()]);
th1d_input[labelname.index()]->SetMarkerStyle(styles[labelname.index()]);
th1d_input[labelname.index()]->Draw("same");
th1d_input[labelname.index()]->GetXaxis()->SetTitle(xlabel);
th1d_input[labelname.index()]->GetYaxis()->SetTitle(ylabel);
th1d_input[labelname.index()]->GetYaxis()->SetTitleOffset(2.0);
lg->AddEntry(th1d_input[labelname.index()], labelname.value());

TString label_meanmass_plot = "Mean M_{#mu#mu} (GeV)";
if (ylabel.EqualTo(label_meanmass_plot)) {
double ycenter = (ymax + ymin) / 2.0;
double yrange = (ymax - ymin) * 2;
double Ymin = ycenter - yrange;
double Ymax = ycenter + yrange * 1.10;
th1d_input[labelname.index()]->SetAxisRange(Ymin, Ymax, "Y");
th1d_input[labelname.index()]->Draw("PEX0same");
} else {
double Ymin = ymin - ymin * 0.07;
double Ymax = ymax + ymax * 0.35;
th1d_input[labelname.index()]->SetAxisRange(Ymin, Ymax, "Y");
th1d_input[labelname.index()]->Draw("HIST E1 same");
}
}

CMS_lumi(canv, 0, 3, Rlabel);

lg->Draw("same");
c->SaveAs(output_name);

canv->Update();
canv->RedrawAxis();
canv->GetFrame()->Draw();
canv->SaveAs(output_name);

if (output_name.Contains(".pdf")) {
TString output_name_png(output_name); // output_name is const, copy to modify
output_name_png.Replace(output_name_png.Index(".pdf"), 4, ".png");
c->SaveAs(output_name_png);
canv->SaveAs(output_name_png);
}
}

Expand All @@ -253,6 +364,7 @@ int Zmumumerge(int argc, char* argv[]) {
vector<TString> vec_title;
vector<int> vec_color;
vector<int> vec_style;
vector<TString> vec_right_title;

Options options;
options.helper(argc, argv);
Expand All @@ -261,16 +373,31 @@ int Zmumumerge(int argc, char* argv[]) {
pt::read_json(options.config, main_tree);
pt::ptree alignments = main_tree.get_child("alignments");
pt::ptree validation = main_tree.get_child("validation");

for (const auto& childTree : alignments) {
// do not consider the nodes with a "file" to merge
if (childTree.second.find("file") == childTree.second.not_found()) {
std::cerr << "Ignoring alignment: " << childTree.second.get<std::string>("title") << ".\nNo file to merged found!"
<< std::endl;
continue;
} else {
std::cout << "Storing alignment: " << childTree.second.get<std::string>("title") << std::endl;
}
vec_single_file_path.push_back(childTree.second.get<std::string>("file"));
vec_single_file_name.push_back(childTree.second.get<std::string>("file") + "/Zmumu.root");
vec_color.push_back(childTree.second.get<int>("color"));
vec_style.push_back(childTree.second.get<int>("style"));
if (childTree.second.find("customrighttitle") == childTree.second.not_found()) {
vec_right_title.push_back("");
} else {
vec_right_title.push_back(childTree.second.get<std::string>("customrighttitle"));
}
vec_global_tag.push_back(childTree.second.get<std::string>("globaltag"));
vec_title.push_back(childTree.second.get<std::string>("title"));

//Fitting_GetMassmeanVSvariables(childTree.second.get<std::string>("file") + "/Zmumu.root", childTree.second.get<std::string>("file"));
}

TString merge_output = main_tree.get<std::string>("output");
//=============================================
vector<TString> vec_single_fittingoutput;
Expand All @@ -284,22 +411,32 @@ int Zmumumerge(int argc, char* argv[]) {
cout << "files_number=" << files_number << endl;
for (int idx_variable = 0; idx_variable < variables_number; idx_variable++) {
TString th1d_name = Form("th1d_meanmass_%s", tstring_variables_name[idx_variable].Data());

Draw_TH1D_forMultiRootFiles(
vec_single_fittingoutput,
vec_title,
vec_color,
vec_style,
vec_right_title[0],
th1d_name,
tstring_variables_name_label[idx_variable].Data(),
"Mean M_{#mu#mu} (GeV)",
merge_output + Form("/meanmass_%s_GTs.pdf", tstring_variables_name[idx_variable].Data()));

TString th1d_name_entries = Form("th1d_entries_%s", tstring_variables_name[idx_variable].Data());

Draw_TH1D_forMultiRootFiles(
vec_single_fittingoutput,
vec_title,
vec_color,
vec_style,
vec_right_title[0],
th1d_name_entries,
tstring_variables_name_label[idx_variable].Data(),
"Events",
merge_output + Form("/entries_%s_GTs.pdf", tstring_variables_name[idx_variable].Data()));
}

//=============================================
return EXIT_SUCCESS;
}
Expand Down
10 changes: 7 additions & 3 deletions Alignment/OfflineValidation/macros/CMS_lumi.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ TString lumi_sqrtS = "";
bool writeExraLumi = false;
bool drawLogo = false;

void CMS_lumi(TPad* pad, int iPeriod = 3, int iPosX = 10);
void CMS_lumi(TPad* pad, int iPeriod = 3, int iPosX = 10, TString RLabel = "");

inline void CMS_lumi(TPad* pad, int iPeriod, int iPosX) {
inline void CMS_lumi(TPad* pad, int iPeriod, int iPosX, TString RLabel) {
bool outOfFrame = false;
if (iPosX / 10 == 0) {
outOfFrame = true;
Expand Down Expand Up @@ -139,7 +139,11 @@ inline void CMS_lumi(TPad* pad, int iPeriod, int iPosX) {
latex.SetTextFont(42);
latex.SetTextAlign(31);
latex.SetTextSize(lumiTextSize * t);
latex.DrawLatex(1 - r, 1 - t + lumiTextOffset * t, lumiText);
if (RLabel != "") {
latex.DrawLatex(1 - r, 1 - t + lumiTextOffset * t, RLabel);
} else {
latex.DrawLatex(1 - r, 1 - t + lumiTextOffset * t, lumiText);
}

if (outOfFrame) {
latex.SetTextFont(cmsTextFont);
Expand Down
Loading

0 comments on commit e17a8af

Please sign in to comment.