void makePlot_legend(TLegend* legend, const std::string& outputFilePath, const std::string& outputFileName) { TCanvas* canvas_legend = new TCanvas("canvas_legend", "canvas_legend", 900, 800); canvas_legend->SetFillColor(10); canvas_legend->SetBorderSize(2); canvas_legend->Draw(); canvas_legend->cd(); legend->SetX1NDC(0.30); legend->SetY1NDC(0.30); legend->SetX2NDC(0.80); legend->SetY2NDC(0.80); legend->SetTextSize(0.070); legend->SetMargin(0.20); TList* legend_primitives = legend->GetListOfPrimitives(); TIter legend_nextObj(legend_primitives); while ( TObject* obj = legend_nextObj() ) { std::string objName = ""; if ( dynamic_cast<TNamed*>(obj) ) objName = (dynamic_cast<TNamed*>(obj))->GetName(); //std::cout << "obj = " << obj << ": name = " << objName << ", type = " << obj->ClassName() << std::endl; TLegendEntry* legendEntry = dynamic_cast<TLegendEntry*>(obj); if ( legendEntry ) { TH1* histogram = dynamic_cast<TH1*>(legendEntry->GetObject()); if ( histogram ) { histogram->SetLineWidth(2*histogram->GetLineWidth()); histogram->SetMarkerSize(3); } } } legend->Draw(); canvas_legend->Update(); std::string outputFileName_full = Form("%s%s", outputFilePath.data(), outputFileName.data()); size_t idx = outputFileName_full.find_last_of('.'); std::string outputFileName_plot = std::string(outputFileName_full, 0, idx); canvas_legend->Print(std::string(outputFileName_plot).append(".pdf").data()); canvas_legend->Print(std::string(outputFileName_plot).append(".root").data()); delete canvas_legend; }
// A macro to print out a TLegend - can be considered a smarter TLegend::ls(). // If no TLegend pointer is passed, it loops over the TLegends drawn on current TPad. void PrintLegend(TLegend *leg=0) { if ( leg==0 ) { if (gROOT->GetListOfCanvases()->GetEntries()==0) return; TList *padprim = gPad->GetListOfPrimitives(); for (int i=0; i<padprim->GetEntries(); i++) { TObject *myobj = gROOT->FindObject(padprim->At(i)->GetName()); if ( myobj != 0 && myobj->InheritsFrom("TLegend") ) PrintLegend((TLegend*)myobj); } return; } TList *ents = leg->GetListOfPrimitives(); for (int i=0; i<ents->GetEntries(); i++) { TLegendEntry *le = (TLegendEntry*)ents->At(i); TString s( le->GetLabel() ); TObject *obj = le->GetObject(); if (!obj) continue; // if no object, this can be the title line, so skip TString color = "???"; if ( obj->InheritsFrom("TH1") ) color = gROOT->GetListOfColors()->At(((TH1*)obj)->GetLineColor())->GetName(); cout << "Item "; cout.width(2); cout.fill('0'); cout << i << " plotted in "; cout.width(7); cout.fill(' '); cout << color << " : " << s << endl; } //leg->ls(); }