/* programma per pił grafici o anche per 1 solo grafico */ #include void grafici4(){ gROOT->SetBatch(kFALSE); gBenchmark->Start("programma completo"); //VARIABLES CONTROLLING PARAMETERS OF PLOT int numero_grafici = 1; bool gridy = false; bool gridx = false; bool logy = false; bool logx = false; bool stat = false; bool esponente = true; //se true cambia l'esponente, altrimenti non lo modifica //mettere true se compare l'esponente sotto il CMS LABEL bool Legenda = true; bool dashed_fill = false; bool empty_fill = true; bool ratio = false; bool offset_bin = true; //se true shifta double xLabelSize = 0.06; double yLabelSize = 0.06; double xLabelOffset = 0.9; //0.9 double yLabelOffset = 1; //0.6 bool XAxisRange = false; double MinXAxis = 0; double MinYAxis = 200; /* xLabelOffset = 0.9; yLabelOffset = 0.5; */ string title; string titleX = "#mu number (ad)"; //"m_{#mu#mu} (GeV)"; string titleY_def = "Counts "; string options = "col text"; //forse non funziona //cout << title << endl; /* string histogram1 = "MuonEtaHistoGL"; int cycle = 1; string cycle_s = to_string(cycle); string histogram2 = histogram1 + ";" + cycle_s; //cout << histogram2 << endl; string function1 = "MuonMassFit1"; */ string nome_file1 = "HistoOutMcDY_1.root"; string nome_file2 = "HistoOutMcDY_2.root"; vector stack_histo; string a1 = "MuonNumberR"; //MuonEtaHistoGL string a2 = "MuonEtaHistoGSL"; //MuonEtaHistoRL string a3 = "MuonEtaHistoRL"; string a4 = "MuonEtaHistoRSL"; string legenda1 = "prova 1"; string legenda2 = "ciao"; string legenda3 = "ciao"; string legenda4 = "ciao"; //CODE TList *FileList = new TList(); FileList->Add( TFile::Open(nome_file1.c_str(), "read") ); FileList->Add( TFile::Open(nome_file2.c_str(), "read") ); // if we cannot open the file, print an error message and return immediately for(TObject *obj: *FileList){ TFile *f1 = (TFile*)obj; if (f1->IsOpen() == kFALSE ) { string file_error = obj->GetName(); printf("Error: cannot open %s", file_error.c_str()); return; } } //FileList->Print(); TCanvas *c1 = new TCanvas("c1", "main canvas",0,0,600,500); //auto hs = new THStack("hs","Stacked 1D histograms"); TH1F *Histo1; TH1F *Histo2; TH1F *Histo3; TH1F *Histo4; TH1F *Histo5; TF1 *FitFunction; //TColor *color = gROOT->GetColor(kOrange); for(TObject *obj: *FileList){ TFile *f1 = (TFile*)obj; TIter keyList(f1->GetListOfKeys()); TKey *key; while ((key = (TKey*)keyList())) { string keyname = key->GetName(); int keycycle = key->GetCycle(); //cout << keyname << "\t" << keycycle << endl; if( keyname == a1){ // && keycycle == cycle //cout << "trovato!" << endl; //Histo = (TH1F*)f1->Get("CorrEtaPtTauG;1"); Histo1 = (TH1F*)f1->Get(a1.c_str()); //colour if (a1.find('L') != std::string::npos){ Histo1->SetLineColor(kOrange-3); } if (a1.find("SL") != std::string::npos){ Histo1->SetLineColor(kAzure+10); } /* else{ Histo1->SetLineColor(kAzure+5); } */ //style if (a1.find('G') != std::string::npos){ Histo1->SetLineStyle(1); } if (a1.find('R') != std::string::npos){ Histo1->SetLineStyle(2); } }//keynamen a1 /* if( keyname == function1 ){ FitFunction = (TF1*)f1->Get("MuonMassFit1"); }*/ if(numero_grafici >= 2){ if( keyname == a2 ){ //&& keycycle == cycle Histo2 = (TH1F*)f1->Get(a2.c_str()); if (a2.find('L') != std::string::npos){ Histo2->SetLineColor(kOrange-3); } if (a2.find("SL") != std::string::npos){ Histo2->SetLineColor(kAzure+10); } if (a2.find('G') != std::string::npos){ Histo2->SetLineStyle(1); } if (a2.find('R') != std::string::npos){ Histo2->SetLineStyle(2); } }//keyname = a2 if(numero_grafici >= 3){ if( keyname == a3 ){ Histo3 = (TH1F*)f1->Get(a3.c_str()); if (a3.find('L') != std::string::npos){ Histo3->SetLineColor(kOrange-3); } if (a3.find("SL") != std::string::npos){ Histo3->SetLineColor(kAzure+10); } if (a3.find('G') != std::string::npos){ Histo3->SetLineStyle(1); } if (a3.find('R') != std::string::npos){ Histo3->SetLineStyle(2); } }//keyname == a3 if(numero_grafici >= 4){ if( keyname == a4 ){ Histo4 = (TH1F*)f1->Get(a4.c_str()); if (a4.find('L') != std::string::npos){ Histo4->SetLineColor(kOrange-3); } if (a4.find("SL") != std::string::npos){ Histo4->SetLineColor(kAzure+10); } if (a4.find('G') != std::string::npos){ Histo4->SetLineStyle(1); } if (a4.find('R') != std::string::npos){ Histo4->SetLineStyle(2); } }//keyname == a4 }//numero grafici = 4 }//numero grafici = 3 }//numero grafici = 2 } } //cout << endl; //cout << "fine file" << endl << endl; //if(numero_grafici == 1){ // --------------------------- BIN WIDTH AND OFFSET ------------------------- /* if(offset_bin == true){ } */ double minX = Histo1->GetXaxis()->GetXmin(); double maxX = Histo1->GetXaxis()->GetXmax(); double BinNumber = Histo1->GetXaxis()->GetNbins(); double width_temp = ( maxX - minX )/BinNumber; double BinWidth = ceil(width_temp * 100.0) / 100.0; // --------------------------- FILL OPTION ------------------------- /* if(empty_fill == false){ //Histo1->SetLineWidth(2); Histo1->SetLineColor(kBlack); if(numero_grafici >= 2){ Histo2->SetLineWidth(2); Histo2->SetLineColor(kBlack); } } if(empty_fill == true && dashed_fill == false){ Histo1->SetFillColor(kWhite); if(numero_grafici >= 2){ Histo2->SetFillColor(kWhite); } } if(dashed_fill == true){ Histo1->SetFillStyle(3345); if(numero_grafici >= 2){ Histo2->SetFillStyle(3305); } } */ // --------------------------- PAD OPTIONS ------------------------- if (gridy == true){ c1->SetGridy(); } if (gridx == true){ c1->SetGridx(); } if (logx == true){ c1->SetLogx(); } if (logy == true){ c1->SetLogy(); } if (stat == false){ gStyle->SetOptStat(0); } if (xLabelOffset!=0){ Histo1->GetXaxis()->SetTitleOffset(xLabelOffset); } if (yLabelOffset!=0){ Histo1->GetYaxis()->SetTitleOffset(yLabelOffset); } gPad->SetTicks(); // ----------------------------- SET EXPONENT OFFSET IN AXIS ----------------------------- if(esponente == true){ TGaxis::SetMaxDigits(2); TGaxis::SetExponentOffset(-0.06, -0.015, "y"); // X and Y offset for Y axis //TGaxis::SetExponentOffset(-0.05, 0.01, "x"); // Y and Y offset for X axis } // ------------------------------- TITLES ---------------------------------- //convert bin width to string, approximate it and remove zeros //and display y axis title with bin width //automatically gets x axis unit and erases (, ) if(!Histo1->InheritsFrom("TH2F")){ //per togliere la divisione nel caso di istogrammi 2d titleY_def = titleY_def + "/ " + to_string(BinWidth); titleY_def.erase ( titleY_def.find_last_not_of('0') + 1, std::string::npos ); titleY_def.erase ( titleY_def.find_last_not_of('.') + 1, std::string::npos ); if (titleX.find("ad") != std::string::npos){ titleY_def = titleY_def + " units"; } else{ std::size_t pos = titleX.find("("); // position of "live" in str std::string str3 = titleX.substr (pos); str3.erase(remove(str3.begin(), str3.end(), '('), str3.end()); str3.erase(remove(str3.begin(), str3.end(), ')'), str3.end()); titleY_def = titleY_def + " " + str3; } } //cout << titleY_def << endl; //titles Histo1->SetTitle( title.c_str() ); Histo1->GetXaxis()->SetTitleSize(xLabelSize); Histo1->GetXaxis()->SetTitle(titleX.c_str()); Histo1->GetYaxis()->SetTitleSize(xLabelSize); Histo1->GetYaxis()->SetTitle(titleY_def.c_str()); //margins c1->SetLeftMargin(0.15); // find the best value to make the title visible c1->SetBottomMargin(0.15); c1->SetTopMargin(0.15); c1->SetRightMargin(0.1); Histo1->SetMarkerStyle(2); Histo1->SetMarkerSize(0); Histo1->SetLineWidth(2); //Histo1->SetLineColor(1); //FitFunction->SetLineColor(2); if(offset_bin == false){ Histo1->Draw(options.c_str()); } if(offset_bin == true){ Histo1->SetBarOffset(-0.5); Histo1->SetFillColor(kWhite); Histo1->Draw("bars col"); } //Histo1->Draw(); /* if(Histo1->InheritsFrom("TH2")){ } */ //FitFunction->Draw("same"); int maxY = Histo1->GetMaximum(); if(XAxisRange == true){ Histo1->GetXaxis()->SetRangeUser(MinXAxis,MinYAxis); } if(Histo1->InheritsFrom("TH1F")){ Histo1->GetYaxis()->SetRangeUser(0,maxY + maxY*0.5); } else{} //per non far cambiare automaticamente il range nel caso di TH2F //cout << "max " << maxY << endl; /* cout << "min " << width_temp << endl; cout << width << endl; */ //} //numero grafici == 1 if(numero_grafici >= 2 ){ Histo2->SetMarkerStyle(2); Histo2->SetMarkerSize(0); Histo2->SetLineWidth(2); Histo2->Draw("same"); if(numero_grafici >= 3){ Histo3->SetMarkerStyle(2); Histo3->SetMarkerSize(0); Histo3->SetLineWidth(2); Histo3->Draw("same"); if(numero_grafici >= 4){ Histo4->SetMarkerStyle(2); Histo4->SetMarkerSize(0); Histo4->SetLineWidth(2); Histo4->Draw("same"); } } } // --------------------------- RATIO PLOT ------------------------- if(ratio == true){ auto rp = new TRatioPlot(Histo1, Histo2); rp->Draw(); } // -------------------------------------------- LEGENDA TLegend legend(0.4, 0.65, 0.8, 0.8); legend.SetFillColor(0); legend.SetBorderSize(0); legend.SetTextSize(0.04); legend.AddEntry(Histo1, legenda1.c_str(), "L"); if(numero_grafici >= 2 ){ legend.AddEntry(Histo2, legenda2.c_str(), "L"); if(numero_grafici >= 3 ){ legend.AddEntry(Histo3, legenda3.c_str(), "L"); if(numero_grafici >= 4 ){ legend.AddEntry(Histo4, legenda4.c_str(), "L"); }//4 }//3 }//2 if(!Histo1->InheritsFrom("TH2F")){ //if(Legenda == true){ legend.Draw(); } // -------------------------------------------- LABEL CMS, RADICE DI S TLatex cms_label; cms_label.SetTextSize(0.04); cms_label.DrawLatexNDC(0.15, 0.87, "Private Work CMS Simulation"); TLatex header; header.SetTextSize(0.04); header.DrawLatexNDC(0.715, 0.87, "#bf{#sqrt{s} = 13.6 TeV}"); c1->Print("prova.pdf"); //AGGIUNGERE CARTELLA // LEGENDA COLORI gROOT->SetBatch(kTRUE); TCanvas *c2 = new TCanvas("c2"); TPaveText *pt = new TPaveText(.05,.1,.95,.8); pt->AddText("A TPaveText can contain severals line of text."); pt->AddText("They are added to the pave using the AddText method."); pt->AddLine(.0,.5,1.,.5); pt->AddText("Even complex TLatex formulas can be added:"); pt->SetBorderSize(0); TText *t1 = pt->AddText("F(t) = #sum_{i=-#infty}^{#infty}A(i)cos#[]{#frac{i}{t+i}}"); t1->SetTextColor(kBlue); pt->Draw(); TPave *box = new TPave(.4,.5,.6,.7); box ->Draw(); c2->Print("grafici/legenda.pdf"); // -------------- COSE NON USATE /* if (a1.find('G') != std::string::npos && a1.find('L') != std::string::npos){ //Histo1->SetFillColor(kOrange-3); Histo1->SetLineColor(kOrange-3); } if (a1.find('G') != std::string::npos && a1.find("SL") != std::string::npos){ //Histo1->SetFillColor(kSpring); Histo1->SetLineColor(kSpring); } if (a1.find('R') != std::string::npos && a1.find('L') != std::string::npos){ //Histo1->SetFillColor(kAzure+10); Histo1->SetLineColor(kAzure+10); } if (a1.find('R') != std::string::npos && a1.find("SL") != std::string::npos){ //Histo1->SetFillColor(kRed-4); Histo1->SetLineColor(kRed-4); } //f1->GetObject(histogram2.c_str(), Histo); //f1->GetObject("CorrEtaPtTauG;1", Histo); if (a1.find('G') != std::string::npos && a1.find('L') != std::string::npos){ Histo1->SetFillColor(kOrange-3); Histo1->SetLineColor(kOrange-3); } if (a1.find('G') != std::string::npos && a1.find("SL") != std::string::npos){ Histo1->SetFillColor(kSpring); Histo1->SetLineColor(kSpring); } if (a1.find('R') != std::string::npos && a1.find('L') != std::string::npos){ Histo1->SetFillColor(kAzure+10); Histo1->SetLineColor(kAzure+10); } if (a1.find('R') != std::string::npos && a1.find("SL") != std::string::npos){ Histo1->SetFillColor(kRed-4); Histo1->SetLineColor(kRed-4); } */ /* stack_histo.push_back(a1); stack_histo.push_back(a2); for(int i=0; iGetColor(kOrange-3); */ /* if(numero_grafici != 1){ for(int i=0; iDraw("same"); } //hs->Draw(); //gStyle->SetPalette(51); } double a = Histo->GetXaxis()->GetTitleSize(); double b = Histo->GetXaxis()->GetTitleOffset(); double c = Histo->GetYaxis()->GetTitleOffset(); cout << b << endl; cout << c << endl; Int_t palette[5]; palette[0] = 15; palette[1] = 20; palette[2] = 23; palette[3] = 30; palette[4] = 32; gStyle->SetPalette(5,palette); TCanvas *c1 = new TCanvas("c1","Histogram Drawing Options",200,10,400,400); TPad *pad1 = new TPad(); MuonEtaHisto->Draw(); c1->Print("eta.pdf"); TCanvas *c2 = new TCanvas("c2","Histogram Drawing Options",200,10,400,400); MuonPhiHisto->Draw(); c2->Print("phi.pdf"); TCanvas *c3 = new TCanvas("c3","Histogram Drawing Options",200,10,400,400); MuonPtHisto->Draw(); c3->Print("Pt.pdf"); TCanvas *c4 = new TCanvas("c4","",200,10,400,400); TPad *pad4 = new TPad(); pad4->Draw(); pad4->cd(); pad4->SetLogy(); MuonDiMassHisto->Draw(); c4->Update(); c4->Print("di mass.pdf"); */ gBenchmark->Show("programma completo"); }