void PlotAlignmentValidation:: setDMRHistStyleAndLegend(TH1F* h, PlotAlignmentValidation::DMRPlotInfo& plotinfo, int direction, int layer) { std::pair<float,float> fitResults(9999., 9999.); h->SetDirectory(0); // The whole DMR plot is plotted with wider line than the split plots // If only split plots are plotted, they will be stronger too, though h->SetLineWidth((direction == 0 || (plotinfo.plotSplits && !plotinfo.plotPlain)) ? 2 : 1); // These lines determine the style of the plots according to rules: // -If the plot is for direction != 0, +1 or +2 is added to the given style for distinction // -However if only direction split plots are to be plotted, the additions should be 0 and +1 respectively // -Modulo 4 arithmetic, because the styles run from 1..4 int linestyle = plotinfo.vars->getLineStyle() - 1, linestyleplus = 0; if (direction == 1) { linestyleplus = 1; } if (direction == -1) { linestyleplus = 2; } if (direction != 0 && plotinfo.plotSplits && !plotinfo.plotPlain) { linestyleplus--; } linestyle = (linestyle + linestyleplus) % 4 + 1; int linecolor = plotinfo.vars->getLineColor(); if (plotinfo.plotLayers && layer > 0) { linecolor += layer - 1; } if (plotinfo.firsthisto) { setHistStyle(*h, plotinfo.variable.c_str(), "#modules", 1); //set color later plotinfo.firsthisto = false; } h->SetLineColor( linecolor ); h->SetLineStyle( linestyle ); if (plotinfo.maxY<h->GetMaximum()){ plotinfo.maxY=h->GetMaximum(); } //fit histogram for median and mean if (plotinfo.variable == "medianX" || plotinfo.variable == "meanX") { fitResults = fitGauss(h, linecolor ); } plotinfo.hstack->Add(h); std::ostringstream legend; legend.precision(2); // Legend: header part if (direction == -1 && plotinfo.subDetId != 2) { legend << "rDirection < 0: "; } else if (direction == 1 && plotinfo.subDetId != 2) { legend << "rDirection > 0: "; } else if (direction == -1 && plotinfo.subDetId == 2) { legend << "zDirection < 0: "; } else if (direction == 1 && plotinfo.subDetId == 2) { legend << "zDirection > 0: "; } else { legend << plotinfo.vars->getName(); if (layer > 0) { legend << ", layer " << layer << ""; } legend << ":"; } // Legend: Statistics if (plotinfo.variable == "medianX" || plotinfo.variable == "meanX" || plotinfo.variable == "medianY" || plotinfo.variable == "meanY") { if (useFit_) { legend << "#mu = " << fitResults.first << " #mum, #sigma = " << fitResults.second << " #mum"; } else { legend << "#mu = " << h->GetMean(1)*10000 << " #mum, rms = " << h->GetRMS(1)*10000 << " #mum"; } } else if (plotinfo.variable == "rmsX" || plotinfo.variable == "rmsY") { legend << "#mu = " << h->GetMean(1)*10000 << " #mum, rms = " << h->GetRMS(1)*10000 << " #mum"; } else if (plotinfo.variable == "meanNormX" || plotinfo.variable == "meanNormY" || plotinfo.variable == "rmsNormX" || plotinfo.variable == "rmsNormY") { legend << "#mu = " << h->GetMean(1) << ", rms = " << h->GetRMS(1); } // Legend: Delta mu for split plots if (plotinfo.h1 != 0 && plotinfo.h2 != 0 && plotinfo.plotSplits && plotinfo.plotPlain && direction == 0) { std::string unit = " #mum"; float factor = 10000.0f; if (plotinfo.variable == "meanNormX" || plotinfo.variable == "meanNormY" || plotinfo.variable == "rmsNormX" || plotinfo.variable == "rmsNormY") { factor = 1.0f; unit = ""; } float deltamu = factor*(plotinfo.h2->GetMean(1) - plotinfo.h1->GetMean(1)); legend << ", #Delta#mu = " << deltamu << unit; } plotinfo.legend->AddEntry(h, legend.str().c_str(), "l"); }
void PlotAlignmentValidation::plotDMR(const std::string variable, Int_t minHits ) { setNiceStyle(); gStyle->SetOptStat(0); // TList treeList=getTreeList(); TCanvas *c = new TCanvas("canv", "canv", 600, 600); setCanvasStyle( *c ); //loop over sub-detectors for (int i=1;i<7;++i){ int histo_Counter=1; TLegend *leg_hist = new TLegend(0.17,0.8,0.85,0.88); setLegendStyle(*leg_hist); //loop over file list //TTree *tree= (TTree*)treeList.First(); //binning int nbinsX=100; double xmin=0; double xmax=0; float maxY=0; bool isHisto = false; std::string plotVar=variable; THStack *hstack=new THStack("hstack","hstack"); for(std::vector<TkOfflineVariables*>::iterator it = sourceList.begin(); it != sourceList.end(); ++it){ //while ( tree ){ plotVar=variable; TString subdet = "entries>="; subdet+=minHits; subdet+=" && subDetId=="; subdet+=i; char binning [50]=""; sprintf (binning, ">>myhisto(%d, %f , %f)", nbinsX, xmin, xmax); TH1F *h = 0; if (histo_Counter==1&&plotVar=="meanX")(*it)->getTree()->Draw( (plotVar+=">>myhisto(50,-0.005,0.005)").c_str(),subdet,"goff"); else if (histo_Counter==1&&plotVar=="meanY")(*it)->getTree()->Draw( (plotVar+=">>myhisto(50,-0.005,0.005)").c_str(),subdet,"goff"); else if (histo_Counter==1&&plotVar=="medianX")(*it)->getTree()->Draw( (plotVar+=">>myhisto(50,-0.005,0.005)").c_str(),subdet,"goff"); else if (histo_Counter==1&&plotVar=="meanNormX")(*it)->getTree()->Draw( (plotVar+=">>myhisto(100,-2,2)").c_str(),subdet,"goff"); else if (histo_Counter==1&&plotVar=="rmsX")(*it)->getTree()->Draw( (plotVar+=">>myhisto(100,0.,0.1)").c_str(),subdet,"goff"); else if (histo_Counter!=1)(*it)->getTree()->Draw( (plotVar+=binning).c_str(),subdet,"goff"); if (gDirectory) gDirectory->GetObject("myhisto", h); std::pair<float,float> fitResults(9999., 9999.); if (h){ if (h->GetEntries()>0) { isHisto = true; h->SetDirectory(0); //general draw options h->SetLineWidth(2); //first histo only, setting optStat... if (histo_Counter==1) setHistStyle(*h,plotVar.c_str() ,"#modules", 1 ); //set color later h->SetLineColor( (*it)->getLineColor() ); h->SetLineStyle( (*it)->getLineStyle() ); //h->SetMarkerStyle(20+file_Counter); //draw options if (maxY<h->GetMaximum()){ maxY=h->GetMaximum(); } //fit histogram for median and mean if (variable=="medianX"||variable =="meanX")fitResults=fitGauss(h, (*it)->getLineColor() ); if (histo_Counter==1){ //get mean and sigma from fit: gauss for 2sigma range hstack->Add(h); nbinsX=h->GetXaxis()->GetNbins(); xmin=h->GetXaxis()->GetXmin(); xmax=h->GetXaxis()->GetXmax(); }else if (histo_Counter!=1 && h->GetEntries()>0)hstack->Add(h); char legend [50]=""; std::string legEntry = (*it)->getName(); if (variable=="medianX"||variable =="meanX")sprintf (legend, "%s: #mu = %4.2f#mum, #sigma = %4.2f#mum ",legEntry.c_str(),fitResults.first ,fitResults.second); else sprintf (legend, "%s ",legEntry.c_str()); if(h) leg_hist->AddEntry(h,legend,"l"); else std::cerr<< "histogram did not exist!"; } } // tree= (TTree*)treeList.After( tree ); // file_Counter++; histo_Counter++; } if (isHisto){ hstack->Draw("nostack"); hstack->SetMaximum(maxY*1.3); setTitleStyle(*hstack,plotVar.c_str() ,"#modules",i); setHistStyle(*hstack->GetHistogram(),plotVar.c_str() ,"#modules", 1 ); leg_hist->Draw(); std::string histName="D"; if (variable=="medianX") histName+="medianR_"; else if (variable=="meanX") histName+="meanR_"; else if (variable=="meanY") histName+="meanYR_"; else if (variable=="rmsX") histName+="rmsR_"; std::string subDetector =""; switch (i){ case 1 : subDetector+="TPB";break; case 2 : subDetector+="TPE";break; case 3 : subDetector+="TIB";break; case 4 : subDetector+="TID";break; case 5 : subDetector+="TOB";break; case 6 : subDetector+="TEC";break; } char PlotName[100]; sprintf( PlotName, "%s/%s%s.eps",outputDir.c_str(), histName.c_str(), subDetector.c_str() ); c->Update(); c->Print(PlotName); //c->Update(); //c->Close(); } delete hstack; hstack=0; } delete c; c=0; }