Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve offline validation plots #4557

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
235 changes: 199 additions & 36 deletions Alignment/OfflineValidation/macros/PlotAlignmentValidation.C
Expand Up @@ -11,6 +11,7 @@
#include "TTree.h"
#include "TString.h"
#include "TAxis.h"
#include "TGaxis.h"
#include "TProfile.h"
#include "TH2F.h"
#include "TROOT.h"
Expand All @@ -19,6 +20,7 @@
#include "TFile.h"
#include "TDirectoryFile.h"
#include "TLegend.h"
#include "TLegendEntry.h"
#include "THStack.h"
#include <exception>
#include "TKey.h"
Expand All @@ -44,6 +46,11 @@ PlotAlignmentValidation::PlotAlignmentValidation(const char *inputFile,std::stri
loadFileList( inputFile, legendName, lineColor, lineStyle);
moreThanOneSource=false;
useFit_ = false;

// Force ROOT to use scientific notation even with smaller datasets
TGaxis::SetMaxDigits(4);
// (This sets a static variable: correct in .eps-images but must be set
// again manually when viewing the .root-files)
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -450,6 +457,7 @@ void PlotAlignmentValidation::plotSS( const std::string& options, const std::str

TString myTitle = "Surface Shape, ";
myTitle += subDetName;
// TODO: move "layer"/"disc" below into the legend, like with DMRs
if (layer!=0) {
// TEC and TID have discs, the rest have layers
if (iSubDet==4 || iSubDet==6)
Expand All @@ -463,26 +471,32 @@ void PlotAlignmentValidation::plotSS( const std::string& options, const std::str
myTitle += TString(" R1-4");
if (isTEC && iTEC>0)
myTitle += TString(" R5-7");

// Generate histograms with selection
THStack *hs = addHists(selection, residType);
TLegend* legend = 0;
THStack *hs = addHists(selection, residType, &legend);
if (!hs || hs->GetHists()==0 || hs->GetHists()->GetSize()==0) {
std::cout << "No histogram for " << subDetName << ", perhaps not enough data?" << std::endl;
continue;
}
hs->SetTitle( myTitle );
hs->Draw("nostack PE");
hs->SetTitle( myTitle );
modifySSHistAndLegend(hs, legend);

hs->Draw("nostack PE");
legend->Draw();

// Adjust Labels
TH1* firstHisto = (TH1*) hs->GetHists()->First();
TString xName = firstHisto->GetXaxis()->GetTitle();
TString yName = firstHisto->GetYaxis()->GetTitle();
hs->GetHistogram()->GetXaxis()->SetTitleColor( kBlack );
hs->GetHistogram()->GetXaxis()->SetTitle( xName );
hs->GetHistogram()->GetYaxis()->SetTitleColor( kBlack );
hs->GetHistogram()->GetYaxis()->SetTitleColor( kBlack );
// micrometers:
yName.ReplaceAll("cm", "#mum");
hs->GetHistogram()->GetYaxis()->SetTitle( yName );

// Save to file
// Save plot to file
std::ostringstream plotName;
plotName << outputDir << "/SurfaceShape_" << subDetName << "_";
plotName << residType;
Expand All @@ -500,9 +514,18 @@ void PlotAlignmentValidation::plotSS( const std::string& options, const std::str
plotName << "_" << "R1-4";
if (isTEC && iTEC>0)
plotName << "_" << "R5-7";
plotName << ".eps";
c.Update();
c.Print(plotName.str().c_str());

// EPS-file
c.Update();
c.Print((plotName.str() + ".eps").c_str());

// ROOT-file
TFile f((plotName.str() + ".root").c_str(), "recreate");
c.Write();
f.Close();

delete legend;
delete hs;
}
}
}
Expand Down Expand Up @@ -671,7 +694,8 @@ void PlotAlignmentValidation::plotDMR(const std::string& variable, Int_t minHits
if (plotinfo.h1 != 0 && plotinfo.h2 != 0 && !plotinfo.plotPlain) {
std::ostringstream legend;
std::string unit = " #mum";
legend.precision(2);
legend.precision(3);
legend << fixed; // to always show 3 decimals
float factor = 10000.0f;
if (plotinfo.variable == "meanNormX" || plotinfo.variable == "meanNormY" ||
plotinfo.variable == "rmsNormX" || plotinfo.variable == "rmsNormY") {
Expand All @@ -694,11 +718,11 @@ void PlotAlignmentValidation::plotDMR(const std::string& variable, Int_t minHits
if (plotinfo.h1) { setDMRHistStyleAndLegend(plotinfo.h1, plotinfo, -1, layer); }
if (plotinfo.h2) { setDMRHistStyleAndLegend(plotinfo.h2, plotinfo, 1, layer); }
}

}

}

if (plotinfo.h != 0 || plotinfo.h1 != 0 || plotinfo.h2 != 0) {

hstack.Draw("nostack");
Expand Down Expand Up @@ -737,7 +761,6 @@ void PlotAlignmentValidation::plotDMR(const std::string& variable, Int_t minHits
// TEC and TID have discs, the rest have layers
if (i==4 || i==6)
plotName << "_discs";

else
plotName << "_layers";
}
Expand All @@ -750,20 +773,94 @@ void PlotAlignmentValidation::plotDMR(const std::string& variable, Int_t minHits
plotName << plotLayerN;
}

plotName << ".eps";

// EPS-file
c.Update();
c.Print(plotName.str().c_str());
c.Print((plotName.str() + ".eps").c_str());

// ROOT-file
TFile f((plotName.str() + ".root").c_str(), "recreate");
c.Write();
f.Close();

}

}

}

//------------------------------------------------------------------------------
void PlotAlignmentValidation::plotChi2(const char *inputFile)
{
// Opens the file (it should be OfflineValidation(Parallel)_result.root)
// and reads and plots the norm_chi^2 and h_chi2Prob -distributions.

// First set default style: plots are already formatted
TStyle defStyle("Default","Default Style");
defStyle.cd();
gStyle->SetOptStat(1);
TGaxis::SetMaxDigits(3);

Bool_t errorflag = kTRUE;
TFile* fi1 = new TFile(inputFile,"read");
TDirectoryFile* mta1 = NULL;
TDirectoryFile* mtb1 = NULL;
TCanvas* normchi = NULL;
TCanvas* chiprob = NULL;
if (fi1 != NULL) {
mta1 = (TDirectoryFile*) fi1->Get("TrackerOfflineValidationStandalone");
if(mta1 != NULL) {
mtb1 = (TDirectoryFile*) mta1->Get("GlobalTrackVariables");
if(mtb1 != NULL) {
normchi = (TCanvas*) mtb1->Get("h_normchi2");
chiprob = (TCanvas*) mtb1->Get("h_chi2Prob");
if (normchi != NULL && chiprob != NULL) {
errorflag = kFALSE;
}
}
}
}
if(errorflag)
{
std::cout << "PlotAlignmentValidation::plotChi2: Can't find data from given file,"
<< " no chi^2-plots produced" << std::endl;
return;
}

// Small adjustments: move the legend right so that it doesn't block
// the exponent of the y-axis scale
TLegend* l = (TLegend*)findObjectFromCanvas(normchi, "TLegend");
if (l != 0) {
l->SetX1NDC(0.25);
}
l = (TLegend*)findObjectFromCanvas(chiprob, "TLegend");
if (l != 0) {
l->SetX1NDC(0.25);
}

chiprob->Draw();
normchi->Draw();

// EPS-files
normchi->Print((outputDir + "/h_normchi2.eps").c_str());
chiprob->Print((outputDir + "/h_chi2Prob.eps").c_str());

// ROOT-files
TFile fi2((outputDir + "/h_normchi2.root").c_str(), "recreate");
normchi->Write();
fi2.Close();

TFile fi3((outputDir + "/h_chi2Prob.root").c_str(), "recreate");
chiprob->Write();
fi3.Close();

fi1->Close();
TGaxis::SetMaxDigits(4);

}

//------------------------------------------------------------------------------
THStack* PlotAlignmentValidation::addHists(const char *selection, const TString &residType,
bool printModuleIds)
TLegend **myLegend, bool printModuleIds)
{
enum ResidType {
xPrimeRes, yPrimeRes, xPrimeNormRes, yPrimeNormRes, xRes, yRes, xNormRes, /*yResNorm*/
Expand All @@ -789,10 +886,12 @@ THStack* PlotAlignmentValidation::addHists(const char *selection, const TString
}

cout << "PlotAlignmentValidation::addHists: using selection " << selection << endl;
THStack * retHistoStack = new THStack();
double legendY = 0.80;
TLegend * myLegend = new TLegend(0.17, legendY, 0.85, 0.88);
setLegendStyle( *myLegend );
THStack * retHistoStack = new THStack("hstack", "");
if (myLegend != 0)
if (*myLegend == 0) {
*myLegend = new TLegend(0.17, 0.80, 0.85, 0.88);
setLegendStyle( **myLegend );
}

for(std::vector<TkOfflineVariables*>::iterator itSourceFile = sourceList.begin();
itSourceFile != sourceList.end(); ++itSourceFile) {
Expand All @@ -809,7 +908,6 @@ THStack* PlotAlignmentValidation::addHists(const char *selection, const TString
return 0;
}

// Todo: TLegend?

// first loop on tree to find out which entries (i.e. modules) fulfill the selection
// 'Entry$' gives the entry number in the tree
Expand Down Expand Up @@ -889,11 +987,12 @@ THStack* PlotAlignmentValidation::addHists(const char *selection, const TString

if (nSel-nEmpty == 0) continue;

myLegend->AddEntry(myLegendName, myLegendName, "L");

if (myLegend != 0)
(*myLegend)->AddEntry(h, myLegendName, "L");

retHistoStack->Add(h);
}
myLegend->Draw();

return retHistoStack;
}

Expand Down Expand Up @@ -954,6 +1053,22 @@ void PlotAlignmentValidation::setLegendStyle( TLegend& leg )
leg.SetBorderSize( 0 );
}

//------------------------------------------------------------------------------
TObject* PlotAlignmentValidation::findObjectFromCanvas(TCanvas* canv, const char* className, Int_t n) {
// Finds the n-th instance of the given class from the canvas
TIter next(canv->GetListOfPrimitives());
TObject* obj = 0;
Int_t found = 0;
while ((obj = next())) {
if(strncmp(obj->ClassName(), className, 10) == 0) {
if (++found == n)
return obj;
}
}

return 0;
}

//------------------------------------------------------------------------------
void PlotAlignmentValidation::setNiceStyle() {
TStyle *MyStyle = new TStyle ("MyStyle", "My style for nicer plots");
Expand Down Expand Up @@ -1045,20 +1160,20 @@ void PlotAlignmentValidation::setHistStyle( TH1& hist,const char* titleX, const
TString titelYAxis=titleY;

if ( titelXAxis.Contains("Phi") )titel_Xaxis<<titleX<<"[rad]";
else if( titelXAxis.Contains("meanX") )titel_Xaxis<<"#LTx'_{pred}-x'_{hit}#GT[cm]";
else if( titelXAxis.Contains("meanY") )titel_Xaxis<<"#LTy'_{pred}-y'_{hit}#GT[cm]";
else if( titelXAxis.Contains("rmsX") )titel_Xaxis<<"RMS(x'_{pred}-x'_{hit})[cm]";
else if( titelXAxis.Contains("rmsY") )titel_Xaxis<<"RMS(y'_{pred}-y'_{hit})[cm]";
else if( titelXAxis.Contains("meanX") )titel_Xaxis<<"#LTx'_{pred}-x'_{hit}#GT[#mum]";
else if( titelXAxis.Contains("meanY") )titel_Xaxis<<"#LTy'_{pred}-y'_{hit}#GT[#mum]";
else if( titelXAxis.Contains("rmsX") )titel_Xaxis<<"RMS(x'_{pred}-x'_{hit})[#mum]";
else if( titelXAxis.Contains("rmsY") )titel_Xaxis<<"RMS(y'_{pred}-y'_{hit})[#mum]";
else if( titelXAxis.Contains("meanNormX") )titel_Xaxis<<"#LTx'_{pred}-x'_{hit}/#sigma#GT";
else if( titelXAxis.Contains("meanNormY") )titel_Xaxis<<"#LTy'_{pred}-y'_{hit}/#sigma#GT";
else if( titelXAxis.Contains("rmsNormX") )titel_Xaxis<<"RMS(x'_{pred}-x'_{hit}/#sigma)";
else if( titelXAxis.Contains("rmsNormY") )titel_Xaxis<<"RMS(y'_{pred}-y'_{hit}/#sigma)";
else if( titelXAxis.Contains("meanLocalX") )titel_Xaxis<<"#LTx_{pred}-x_{hit}#GT[cm]";
else if( titelXAxis.Contains("rmsLocalX") )titel_Xaxis<<"RMS(x_{pred}-x_{hit})[cm]";
else if( titelXAxis.Contains("meanNormLocalX") )titel_Xaxis<<"#LTx_{pred}-x_{hit}/#sigma#GT[cm]";
else if( titelXAxis.Contains("rmsNormLocalX") )titel_Xaxis<<"RMS(x_{pred}-x_{hit}/#sigma)[cm]";
else if( titelXAxis.Contains("medianX") )titel_Xaxis<<"median(x'_{pred}-x'_{hit})[cm]";
else if( titelXAxis.Contains("medianY") )titel_Xaxis<<"median(y'_{pred}-y'_{hit})[cm]";
else if( titelXAxis.Contains("meanLocalX") )titel_Xaxis<<"#LTx_{pred}-x_{hit}#GT[#mum]";
else if( titelXAxis.Contains("rmsLocalX") )titel_Xaxis<<"RMS(x_{pred}-x_{hit})[#mum]";
else if( titelXAxis.Contains("meanNormLocalX") )titel_Xaxis<<"#LTx_{pred}-x_{hit}/#sigma#GT[#mum]";
else if( titelXAxis.Contains("rmsNormLocalX") )titel_Xaxis<<"RMS(x_{pred}-x_{hit}/#sigma)[#mum]";
else if( titelXAxis.Contains("medianX") )titel_Xaxis<<"median(x'_{pred}-x'_{hit})[#mum]";
else if( titelXAxis.Contains("medianY") )titel_Xaxis<<"median(y'_{pred}-y'_{hit})[#mum]";
else titel_Xaxis<<titleX<<"[cm]";

if (hist.IsA()->InheritsFrom( TH1F::Class() ) )hist.SetLineColor(color);
Expand Down Expand Up @@ -1177,7 +1292,8 @@ setDMRHistStyleAndLegend(TH1F* h, PlotAlignmentValidation::DMRPlotInfo& plotinfo
plotinfo.hstack->Add(h);

std::ostringstream legend;
legend.precision(2);
legend.precision(3);
legend << fixed; // to always show 3 decimals

// Legend: header part
if (direction == -1 && plotinfo.subDetId != 2) { legend << "rDirection < 0: "; }
Expand Down Expand Up @@ -1228,6 +1344,13 @@ setDMRHistStyleAndLegend(TH1F* h, PlotAlignmentValidation::DMRPlotInfo& plotinfo

plotinfo.legend->AddEntry(h, legend.str().c_str(), "l");

// Scale the x-axis (cm to um), if needed
if (plotinfo.variable.find("Norm") == std::string::npos) {
Double_t xmin = h->GetXaxis()->GetXmin();
Double_t xmax = h->GetXaxis()->GetXmax();
h->GetXaxis()->SetLimits(xmin*10000, xmax*10000);
}

}

void PlotAlignmentValidation::
Expand All @@ -1248,3 +1371,43 @@ plotDMRHistogram(PlotAlignmentValidation::DMRPlotInfo& plotinfo, int direction,
else { plotinfo.h = h; }
}
}

void PlotAlignmentValidation::modifySSHistAndLegend(THStack* hs, TLegend* legend)
{
// Add mean-y-values to the legend and scale the histograms.

Double_t legendY = 0.80;
if (hs->GetHists()->GetSize() > 3)
legendY -= 0.01 * (hs->GetHists()->GetSize() - 3);
if (legendY < 0.6) {
std::cerr << "Warning: Huge legend!" << std::endl;
legendY = 0.6;
}
legend->SetY1(legendY);

// Loop over all profiles
TProfile* prof = 0;
TIter next(hs->GetHists());
Int_t index = 0;
while ((prof = (TProfile*)next())) {
//Scaling: from cm to um
Double_t scale = 10000;
prof->Scale(scale);

Double_t stats[6] = {0};
prof->GetStats(stats);

std::ostringstream legendtext;
legendtext.precision(3);
legendtext << fixed; // to always show 3 decimals
legendtext << ": y mean = " << stats[4]/stats[0]*scale << " #mum";

TLegendEntry* entry = (TLegendEntry*)legend->GetListOfPrimitives()->At(index);
if (entry == 0)
cout << "PlotAlignmentValidation::PlotAlignmentValidation::modifySSLegend: Bad legend!" << endl;
else
entry->SetLabel((entry->GetLabel() + legendtext.str()).c_str());
index++;
}

}