diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7d9711e1..57f3dc2d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,6 +17,7 @@ jobs: python -m pip install pytest_pylint configparser astroid pyyml papermill python -m pip install -e . python setup.py test + python -m pip install -e . python -m pylint hepdata_lib/*.py python -m pylint tests/*.py --rcfile=tests/pylintrc diff --git a/README.md b/README.md index 2fe8a196..714adb18 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ There are a few more examples available that can directly be run using the [bind - [Reading in a CMS combine ntuple](examples/combine_limits.ipynb) [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/HEPData/hepdata_lib/master?filepath=examples/combine_limits.ipynb) - [Reading in ROOT histograms](examples/reading_histograms.ipynb) [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/HEPData/hepdata_lib/master?filepath=examples/reading_histograms.ipynb) - [Reading a correlation matrix](examples/correlation.ipynb) [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/HEPData/hepdata_lib/master?filepath=examples/correlation.ipynb) +- [Reading TGraph and TGraphError from '.C' files](examples/read_c_file.ipynb) [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/HEPData/hepdata_lib/master?filepath=examples/read_c_file.ipynb) ## External dependencies diff --git a/examples/example_inputs/brazilianFlag_BulkZZ_VVnew_new_combined_13TeV.C b/examples/example_inputs/brazilianFlag_BulkZZ_VVnew_new_combined_13TeV.C new file mode 100644 index 00000000..725bb412 --- /dev/null +++ b/examples/example_inputs/brazilianFlag_BulkZZ_VVnew_new_combined_13TeV.C @@ -0,0 +1,1053 @@ +void brazilianFlag_BulkZZ_VVnew_new_combined_13TeV() +{ +//=========Macro generated from canvas: c1/c1 +//========= (Fri Oct 13 17:10:04 2017) by ROOT version6.02/05 + TCanvas *c1 = new TCanvas("c1", "c1",66,78,800,600); + gStyle->SetOptFit(1); + gStyle->SetOptStat(0); + gStyle->SetOptTitle(0); + c1->SetHighLightColor(2); + c1->Range(0.7857143,-4.905614,4.238095,2.641169); + c1->SetFillColor(0); + c1->SetBorderMode(0); + c1->SetBorderSize(2); + c1->SetLogy(); + c1->SetLeftMargin(0.12); + c1->SetRightMargin(0.04); + c1->SetTopMargin(0.08); + c1->SetBottomMargin(0.12); + c1->SetFrameFillStyle(0); + c1->SetFrameBorderMode(0); + c1->SetFrameFillStyle(0); + c1->SetFrameBorderMode(0); + + TH1F *hframe1 = new TH1F("hframe1","",1000,1.2,4.1); + hframe1->SetMinimum(0.0001); + hframe1->SetMaximum(109); + hframe1->SetDirectory(0); + hframe1->SetStats(0); + hframe1->SetLineStyle(0); + hframe1->SetMarkerStyle(20); + hframe1->GetXaxis()->SetTitle("M_{G_{Bulk}} (TeV)"); + hframe1->GetXaxis()->SetNdivisions(508); + hframe1->GetXaxis()->SetLabelFont(42); + hframe1->GetXaxis()->SetLabelOffset(0.007); + hframe1->GetXaxis()->SetTitleSize(0.05); + hframe1->GetXaxis()->SetTitleOffset(1.05); + hframe1->GetXaxis()->SetTitleFont(42); + hframe1->GetYaxis()->SetTitle("#sigma #times #bf{#it{#Beta}}(G_{Bulk} #rightarrow ZZ) (pb)"); + hframe1->GetYaxis()->SetLabelFont(42); + hframe1->GetYaxis()->SetLabelOffset(0.007); + hframe1->GetYaxis()->SetTitleSize(0.05); + hframe1->GetYaxis()->SetTitleOffset(1.15); + hframe1->GetYaxis()->SetTitleFont(42); + hframe1->GetZaxis()->SetLabelFont(42); + hframe1->GetZaxis()->SetLabelOffset(0.007); + hframe1->GetZaxis()->SetLabelSize(0.05); + hframe1->GetZaxis()->SetTitleSize(0.06); + hframe1->GetZaxis()->SetTitleFont(42); + hframe1->Draw(" "); + + Double_t Graph0_fx1[60] = { + 1.2, + 1.3, + 1.4, + 1.5, + 1.6, + 1.7, + 1.8, + 1.9, + 2, + 2.1, + 2.2, + 2.3, + 2.4, + 2.5, + 2.6, + 2.7, + 2.8, + 2.9, + 3, + 3.1, + 3.2, + 3.3, + 3.4, + 3.5, + 3.6, + 3.7, + 3.8, + 3.9, + 4, + 4.1, + 4.1, + 4, + 3.9, + 3.8, + 3.7, + 3.6, + 3.5, + 3.4, + 3.3, + 3.2, + 3.1, + 3, + 2.9, + 2.8, + 2.7, + 2.6, + 2.5, + 2.4, + 2.3, + 2.2, + 2.1, + 2, + 1.9, + 1.8, + 1.7, + 1.6, + 1.5, + 1.4, + 1.3, + 1.2}; + Double_t Graph0_fy1[60] = { + 0.01595315, + 0.01137211, + 0.008485666, + 0.006614575, + 0.005102327, + 0.004022337, + 0.003202503, + 0.002579359, + 0.002131173, + 0.001797356, + 0.0015182, + 0.001345438, + 0.001190896, + 0.001064013, + 0.000977376, + 0.0008746895, + 0.0008029064, + 0.000722928, + 0.0006655113, + 0.0006069727, + 0.0005659956, + 0.0005199696, + 0.0004851391, + 0.0004552844, + 0.0004135268, + 0.0003896235, + 0.0003705009, + 0.0003561589, + 0.0003370363, + 0.0003226943, + 0.00255538, + 0.002668953, + 0.002820267, + 0.002933835, + 0.003085068, + 0.003274269, + 0.003463533, + 0.003690498, + 0.003955294, + 0.004182393, + 0.004454959, + 0.004760982, + 0.005135107, + 0.005518466, + 0.00592637, + 0.00645113, + 0.006971318, + 0.007641557, + 0.008419919, + 0.00937533, + 0.01076798, + 0.01255619, + 0.01494493, + 0.0181399, + 0.02226404, + 0.02746336, + 0.03493462, + 0.04399361, + 0.05763384, + 0.08203879}; + TGraph *graph = new TGraph(60,Graph0_fx1,Graph0_fy1); + graph->SetName("Graph0"); + graph->SetTitle("Graph"); + + Int_t ci; // for color index setting + TColor *color; // for color definition with alpha + ci = TColor::GetColor("#ffcc00"); + graph->SetFillColor(ci); + + ci = TColor::GetColor("#ffcc00"); + graph->SetLineColor(ci); + graph->SetMarkerStyle(20); + + TH1F *Graph_Graph1 = new TH1F("Graph_Graph1","Graph",100,0.91,4.39); + Graph_Graph1->SetMinimum(0.0002904249); + Graph_Graph1->SetMaximum(0.0902104); + Graph_Graph1->SetDirectory(0); + Graph_Graph1->SetStats(0); + Graph_Graph1->SetLineStyle(0); + Graph_Graph1->SetMarkerStyle(20); + Graph_Graph1->GetXaxis()->SetLabelFont(42); + Graph_Graph1->GetXaxis()->SetLabelOffset(0.007); + Graph_Graph1->GetXaxis()->SetLabelSize(0.05); + Graph_Graph1->GetXaxis()->SetTitleSize(0.06); + Graph_Graph1->GetXaxis()->SetTitleOffset(0.9); + Graph_Graph1->GetXaxis()->SetTitleFont(42); + Graph_Graph1->GetYaxis()->SetLabelFont(42); + Graph_Graph1->GetYaxis()->SetLabelOffset(0.007); + Graph_Graph1->GetYaxis()->SetLabelSize(0.05); + Graph_Graph1->GetYaxis()->SetTitleSize(0.06); + Graph_Graph1->GetYaxis()->SetTitleOffset(1.25); + Graph_Graph1->GetYaxis()->SetTitleFont(42); + Graph_Graph1->GetZaxis()->SetLabelFont(42); + Graph_Graph1->GetZaxis()->SetLabelOffset(0.007); + Graph_Graph1->GetZaxis()->SetLabelSize(0.05); + Graph_Graph1->GetZaxis()->SetTitleSize(0.06); + Graph_Graph1->GetZaxis()->SetTitleFont(42); + graph->SetHistogram(Graph_Graph1); + + graph->Draw("f"); + + Double_t Graph1_fx2[60] = { + 1.2, + 1.3, + 1.4, + 1.5, + 1.6, + 1.7, + 1.8, + 1.9, + 2, + 2.1, + 2.2, + 2.3, + 2.4, + 2.5, + 2.6, + 2.7, + 2.8, + 2.9, + 3, + 3.1, + 3.2, + 3.3, + 3.4, + 3.5, + 3.6, + 3.7, + 3.8, + 3.9, + 4, + 4.1, + 4.1, + 4, + 3.9, + 3.8, + 3.7, + 3.6, + 3.5, + 3.4, + 3.3, + 3.2, + 3.1, + 3, + 2.9, + 2.8, + 2.7, + 2.6, + 2.5, + 2.4, + 2.3, + 2.2, + 2.1, + 2, + 1.9, + 1.8, + 1.7, + 1.6, + 1.5, + 1.4, + 1.3, + 1.2}; + Double_t Graph1_fy2[60] = { + 0.02217097, + 0.01576992, + 0.01184516, + 0.009217427, + 0.007141818, + 0.005655557, + 0.00452342, + 0.003660109, + 0.003028332, + 0.00255747, + 0.00217565, + 0.001932701, + 0.001719004, + 0.00154723, + 0.001406285, + 0.00127097, + 0.001166666, + 0.001061114, + 0.000976838, + 0.0009002073, + 0.0008394338, + 0.0007836981, + 0.0007312015, + 0.0006862045, + 0.0006374985, + 0.0005957413, + 0.0005665025, + 0.0005445734, + 0.0005153345, + 0.0004934054, + 0.00154857, + 0.001617395, + 0.001694328, + 0.001762556, + 0.001829185, + 0.001932793, + 0.002044515, + 0.002159168, + 0.002293378, + 0.002425055, + 0.002577029, + 0.002772753, + 0.002984393, + 0.003223677, + 0.003511886, + 0.003817528, + 0.004155923, + 0.004589493, + 0.005091001, + 0.005665162, + 0.006556213, + 0.007707147, + 0.009248577, + 0.01127572, + 0.01390637, + 0.01732098, + 0.02210318, + 0.02798274, + 0.03686041, + 0.0521819}; + graph = new TGraph(60,Graph1_fx2,Graph1_fy2); + graph->SetName("Graph1"); + graph->SetTitle("Graph"); + + ci = TColor::GetColor("#00cc00"); + graph->SetFillColor(ci); + + ci = TColor::GetColor("#00cc00"); + graph->SetLineColor(ci); + graph->SetMarkerStyle(20); + + TH1F *Graph_Graph2 = new TH1F("Graph_Graph2","Graph",100,0.91,4.39); + Graph_Graph2->SetMinimum(0.0004440649); + Graph_Graph2->SetMaximum(0.05735075); + Graph_Graph2->SetDirectory(0); + Graph_Graph2->SetStats(0); + Graph_Graph2->SetLineStyle(0); + Graph_Graph2->SetMarkerStyle(20); + Graph_Graph2->GetXaxis()->SetLabelFont(42); + Graph_Graph2->GetXaxis()->SetLabelOffset(0.007); + Graph_Graph2->GetXaxis()->SetLabelSize(0.05); + Graph_Graph2->GetXaxis()->SetTitleSize(0.06); + Graph_Graph2->GetXaxis()->SetTitleOffset(0.9); + Graph_Graph2->GetXaxis()->SetTitleFont(42); + Graph_Graph2->GetYaxis()->SetLabelFont(42); + Graph_Graph2->GetYaxis()->SetLabelOffset(0.007); + Graph_Graph2->GetYaxis()->SetLabelSize(0.05); + Graph_Graph2->GetYaxis()->SetTitleSize(0.06); + Graph_Graph2->GetYaxis()->SetTitleOffset(1.25); + Graph_Graph2->GetYaxis()->SetTitleFont(42); + Graph_Graph2->GetZaxis()->SetLabelFont(42); + Graph_Graph2->GetZaxis()->SetLabelOffset(0.007); + Graph_Graph2->GetZaxis()->SetLabelSize(0.05); + Graph_Graph2->GetZaxis()->SetTitleSize(0.06); + Graph_Graph2->GetZaxis()->SetTitleFont(42); + graph->SetHistogram(Graph_Graph2); + + graph->Draw("f"); + + Double_t Graph2_fx1001[30] = { + 1.2, + 1.3, + 1.4, + 1.5, + 1.6, + 1.7, + 1.8, + 1.9, + 2, + 2.1, + 2.2, + 2.3, + 2.4, + 2.5, + 2.6, + 2.7, + 2.8, + 2.9, + 3, + 3.1, + 3.2, + 3.3, + 3.4, + 3.5, + 3.6, + 3.7, + 3.8, + 3.9, + 4, + 4.1}; + Double_t Graph2_fy1001[30] = { + 0.03306888, + 0.0234779, + 0.01773331, + 0.01393688, + 0.0108398, + 0.00861689, + 0.006918488, + 0.005619711, + 0.004683092, + 0.003983751, + 0.003409291, + 0.003034644, + 0.002709949, + 0.00245394, + 0.002254128, + 0.002054317, + 0.001885725, + 0.001729622, + 0.001592251, + 0.001479857, + 0.001379951, + 0.001305022, + 0.001217604, + 0.001142675, + 0.001080233, + 0.001017792, + 0.0009678391, + 0.0009303744, + 0.0008804214, + 0.0008429566}; + Double_t Graph2_fex1001[30] = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + Double_t Graph2_fey1001[30] = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + TGraphErrors *gre = new TGraphErrors(30,Graph2_fx1001,Graph2_fy1001,Graph2_fex1001,Graph2_fey1001); + gre->SetName("Graph2"); + gre->SetTitle("Graph"); + gre->SetFillColor(1); + gre->SetLineStyle(3); + gre->SetLineWidth(4); + gre->SetMarkerStyle(20); + + TH1F *Graph_Graph1001 = new TH1F("Graph_Graph1001","Graph",100,0.91,4.39); + Graph_Graph1001->SetMinimum(0.000758661); + Graph_Graph1001->SetMaximum(0.03629147); + Graph_Graph1001->SetDirectory(0); + Graph_Graph1001->SetStats(0); + Graph_Graph1001->SetLineStyle(0); + Graph_Graph1001->SetMarkerStyle(20); + Graph_Graph1001->GetXaxis()->SetLabelFont(42); + Graph_Graph1001->GetXaxis()->SetLabelOffset(0.007); + Graph_Graph1001->GetXaxis()->SetLabelSize(0.05); + Graph_Graph1001->GetXaxis()->SetTitleSize(0.06); + Graph_Graph1001->GetXaxis()->SetTitleOffset(0.9); + Graph_Graph1001->GetXaxis()->SetTitleFont(42); + Graph_Graph1001->GetYaxis()->SetLabelFont(42); + Graph_Graph1001->GetYaxis()->SetLabelOffset(0.007); + Graph_Graph1001->GetYaxis()->SetLabelSize(0.05); + Graph_Graph1001->GetYaxis()->SetTitleSize(0.06); + Graph_Graph1001->GetYaxis()->SetTitleOffset(1.25); + Graph_Graph1001->GetYaxis()->SetTitleFont(42); + Graph_Graph1001->GetZaxis()->SetLabelFont(42); + Graph_Graph1001->GetZaxis()->SetLabelOffset(0.007); + Graph_Graph1001->GetZaxis()->SetLabelSize(0.05); + Graph_Graph1001->GetZaxis()->SetTitleSize(0.06); + Graph_Graph1001->GetZaxis()->SetTitleFont(42); + gre->SetHistogram(Graph_Graph1001); + + gre->Draw("l"); + + Double_t Graph3_fx1002[30] = { + 1.2, + 1.3, + 1.4, + 1.5, + 1.6, + 1.7, + 1.8, + 1.9, + 2, + 2.1, + 2.2, + 2.3, + 2.4, + 2.5, + 2.6, + 2.7, + 2.8, + 2.9, + 3, + 3.1, + 3.2, + 3.3, + 3.4, + 3.5, + 3.6, + 3.7, + 3.8, + 3.9, + 4, + 4.1}; + Double_t Graph3_fy1002[30] = { + 0.03598755, + 0.01610116, + 0.02320307, + 0.01525207, + 0.005968025, + 0.008148118, + 0.008378009, + 0.006453676, + 0.005740356, + 0.005050603, + 0.005455484, + 0.0049174, + 0.003183152, + 0.002217362, + 0.002256415, + 0.002605496, + 0.002598886, + 0.002560049, + 0.002647637, + 0.002457457, + 0.002286259, + 0.00202766, + 0.001160274, + 0.0007028884, + 0.0005824113, + 0.0005595336, + 0.0005714641, + 0.0005831253, + 0.0005912939, + 0.0005886642}; + Double_t Graph3_fex1002[30] = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + Double_t Graph3_fey1002[30] = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + gre = new TGraphErrors(30,Graph3_fx1002,Graph3_fy1002,Graph3_fex1002,Graph3_fey1002); + gre->SetName("Graph3"); + gre->SetTitle("Graph"); + gre->SetFillColor(1); + gre->SetLineWidth(2); + gre->SetMarkerStyle(8); + gre->SetMarkerSize(0.8); + + TH1F *Graph_Graph1002 = new TH1F("Graph_Graph1002","Graph",100,0.91,4.39); + Graph_Graph1002->SetMinimum(0.0005035803); + Graph_Graph1002->SetMaximum(0.03953035); + Graph_Graph1002->SetDirectory(0); + Graph_Graph1002->SetStats(0); + Graph_Graph1002->SetLineStyle(0); + Graph_Graph1002->SetMarkerStyle(20); + Graph_Graph1002->GetXaxis()->SetLabelFont(42); + Graph_Graph1002->GetXaxis()->SetLabelOffset(0.007); + Graph_Graph1002->GetXaxis()->SetLabelSize(0.05); + Graph_Graph1002->GetXaxis()->SetTitleSize(0.06); + Graph_Graph1002->GetXaxis()->SetTitleOffset(0.9); + Graph_Graph1002->GetXaxis()->SetTitleFont(42); + Graph_Graph1002->GetYaxis()->SetLabelFont(42); + Graph_Graph1002->GetYaxis()->SetLabelOffset(0.007); + Graph_Graph1002->GetYaxis()->SetLabelSize(0.05); + Graph_Graph1002->GetYaxis()->SetTitleSize(0.06); + Graph_Graph1002->GetYaxis()->SetTitleOffset(1.25); + Graph_Graph1002->GetYaxis()->SetTitleFont(42); + Graph_Graph1002->GetZaxis()->SetLabelFont(42); + Graph_Graph1002->GetZaxis()->SetLabelOffset(0.007); + Graph_Graph1002->GetZaxis()->SetLabelSize(0.05); + Graph_Graph1002->GetZaxis()->SetTitleSize(0.06); + Graph_Graph1002->GetZaxis()->SetTitleFont(42); + gre->SetHistogram(Graph_Graph1002); + + gre->Draw("lp"); + + Double_t BulkZZ_gtheory_fx1003[11] = { + 1, + 1.2, + 1.4, + 1.6, + 1.8, + 2, + 2.5, + 3, + 3.5, + 4, + 4.5}; + Double_t BulkZZ_gtheory_fy1003[11] = { + 0.01024985, + 0.00341978, + 0.00130687, + 0.0005741872, + 0.0002441465, + 0.0001197594, + 2.242854e-05, + 4.91243e-06, + 2.098791e-06, + 1.219122e-06, + 1.044836e-06}; + Double_t BulkZZ_gtheory_fex1003[11] = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + Double_t BulkZZ_gtheory_fey1003[11] = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0}; + gre = new TGraphErrors(11,BulkZZ_gtheory_fx1003,BulkZZ_gtheory_fy1003,BulkZZ_gtheory_fex1003,BulkZZ_gtheory_fey1003); + gre->SetName("BulkZZ_gtheory"); + gre->SetTitle("Graph"); + gre->SetFillColor(1); + + ci = TColor::GetColor("#ff0000"); + gre->SetLineColor(ci); + gre->SetLineWidth(3); + + TH1F *Graph_BulkZZ_gtheory1003 = new TH1F("Graph_BulkZZ_gtheory1003","Graph",100,0.65,4.85); + Graph_BulkZZ_gtheory1003->SetMinimum(9.403527e-07); + Graph_BulkZZ_gtheory1003->SetMaximum(0.01127473); + Graph_BulkZZ_gtheory1003->SetDirectory(0); + Graph_BulkZZ_gtheory1003->SetStats(0); + Graph_BulkZZ_gtheory1003->SetLineStyle(0); + Graph_BulkZZ_gtheory1003->SetMarkerStyle(20); + Graph_BulkZZ_gtheory1003->GetXaxis()->SetLabelFont(42); + Graph_BulkZZ_gtheory1003->GetXaxis()->SetLabelOffset(0.007); + Graph_BulkZZ_gtheory1003->GetXaxis()->SetLabelSize(0.05); + Graph_BulkZZ_gtheory1003->GetXaxis()->SetTitleSize(0.06); + Graph_BulkZZ_gtheory1003->GetXaxis()->SetTitleOffset(0.9); + Graph_BulkZZ_gtheory1003->GetXaxis()->SetTitleFont(42); + Graph_BulkZZ_gtheory1003->GetYaxis()->SetLabelFont(42); + Graph_BulkZZ_gtheory1003->GetYaxis()->SetLabelOffset(0.007); + Graph_BulkZZ_gtheory1003->GetYaxis()->SetLabelSize(0.05); + Graph_BulkZZ_gtheory1003->GetYaxis()->SetTitleSize(0.06); + Graph_BulkZZ_gtheory1003->GetYaxis()->SetTitleOffset(1.25); + Graph_BulkZZ_gtheory1003->GetYaxis()->SetTitleFont(42); + Graph_BulkZZ_gtheory1003->GetZaxis()->SetLabelFont(42); + Graph_BulkZZ_gtheory1003->GetZaxis()->SetLabelOffset(0.007); + Graph_BulkZZ_gtheory1003->GetZaxis()->SetLabelSize(0.05); + Graph_BulkZZ_gtheory1003->GetZaxis()->SetTitleSize(0.06); + Graph_BulkZZ_gtheory1003->GetZaxis()->SetTitleFont(42); + gre->SetHistogram(Graph_BulkZZ_gtheory1003); + + gre->Draw("l"); + + Double_t BulkZZ_grshade_fx3[22] = { + 1, + 1.2, + 1.4, + 1.6, + 1.8, + 2, + 2.5, + 3, + 3.5, + 4, + 4.5, + 4.5, + 4, + 3.5, + 3, + 2.5, + 2, + 1.8, + 1.6, + 1.4, + 1.2, + 1}; + Double_t BulkZZ_grshade_fy3[22] = { + 0.0134007, + 0.004611356, + 0.001811763, + 0.0008217326, + 0.0003600711, + 0.0001818294, + 3.647573e-05, + 8.680007e-06, + 4.007235e-06, + 2.568563e-06, + 2.408994e-06, + 9.808328e-08, + 2.553502e-07, + 6.904563e-07, + 2.048e-06, + 1.139506e-05, + 6.948064e-05, + 0.000148981, + 0.0003680936, + 0.0008800111, + 0.002397691, + 0.007501346}; + graph = new TGraph(22,BulkZZ_grshade_fx3,BulkZZ_grshade_fy3); + graph->SetName("BulkZZ_grshade"); + graph->SetTitle("Graph"); + + ci = TColor::GetColor("#ff0000"); + graph->SetFillColor(ci); + graph->SetFillStyle(3013); + graph->SetLineColor(0); + + TH1F *Graph_BulkZZ_grshade3 = new TH1F("Graph_BulkZZ_grshade3","Graph",100,0.65,4.85); + Graph_BulkZZ_grshade3->SetMinimum(8.827495e-08); + Graph_BulkZZ_grshade3->SetMaximum(0.01474076); + Graph_BulkZZ_grshade3->SetDirectory(0); + Graph_BulkZZ_grshade3->SetStats(0); + Graph_BulkZZ_grshade3->SetLineStyle(0); + Graph_BulkZZ_grshade3->SetMarkerStyle(20); + Graph_BulkZZ_grshade3->GetXaxis()->SetLabelFont(42); + Graph_BulkZZ_grshade3->GetXaxis()->SetLabelOffset(0.007); + Graph_BulkZZ_grshade3->GetXaxis()->SetLabelSize(0.05); + Graph_BulkZZ_grshade3->GetXaxis()->SetTitleSize(0.06); + Graph_BulkZZ_grshade3->GetXaxis()->SetTitleOffset(0.9); + Graph_BulkZZ_grshade3->GetXaxis()->SetTitleFont(42); + Graph_BulkZZ_grshade3->GetYaxis()->SetLabelFont(42); + Graph_BulkZZ_grshade3->GetYaxis()->SetLabelOffset(0.007); + Graph_BulkZZ_grshade3->GetYaxis()->SetLabelSize(0.05); + Graph_BulkZZ_grshade3->GetYaxis()->SetTitleSize(0.06); + Graph_BulkZZ_grshade3->GetYaxis()->SetTitleOffset(1.25); + Graph_BulkZZ_grshade3->GetYaxis()->SetTitleFont(42); + Graph_BulkZZ_grshade3->GetZaxis()->SetLabelFont(42); + Graph_BulkZZ_grshade3->GetZaxis()->SetLabelOffset(0.007); + Graph_BulkZZ_grshade3->GetZaxis()->SetLabelSize(0.05); + Graph_BulkZZ_grshade3->GetZaxis()->SetTitleSize(0.06); + Graph_BulkZZ_grshade3->GetZaxis()->SetTitleFont(42); + graph->SetHistogram(Graph_BulkZZ_grshade3); + + graph->Draw("f"); + + TPaveText *pt = new TPaveText(0.52,0.2,0.8,0.9,"brNDC"); + pt->SetBorderSize(0); + pt->SetFillColor(0); + pt->SetFillStyle(0); + pt->SetLineColor(0); + pt->SetTextAlign(12); + pt->SetTextFont(42); + pt->SetTextSize(0.035); + TText *AText = pt->AddText("Narrow width approximation"); + pt->Draw(); + TLatex *tex = new TLatex(0.96,0.936,"35.9 fb^{-1} (13 TeV)"); +tex->SetNDC(); + tex->SetTextAlign(31); + tex->SetTextFont(42); + tex->SetTextSize(0.048); + tex->SetLineWidth(2); + tex->Draw(); + tex = new TLatex(0.1578,0.892,"CMS"); +tex->SetNDC(); + tex->SetTextAlign(13); + tex->SetTextFont(61); + tex->SetTextSize(0.06); + tex->SetLineWidth(2); + tex->Draw(); + tex = new TLatex(0.1578,0.82,""); +tex->SetNDC(); + tex->SetTextAlign(13); + tex->SetTextFont(52); + tex->SetTextSize(0.0456); + tex->SetLineWidth(2); + tex->Draw(); + + TH1F *hframe_copy2 = new TH1F("hframe_copy2","",1000,1.2,4.1); + hframe_copy2->SetMinimum(0.0001); + hframe_copy2->SetMaximum(109); + hframe_copy2->SetDirectory(0); + hframe_copy2->SetStats(0); + hframe_copy2->SetLineStyle(0); + hframe_copy2->SetMarkerStyle(20); + hframe_copy2->GetXaxis()->SetTitle("M_{G_{Bulk}} (TeV)"); + hframe_copy2->GetXaxis()->SetNdivisions(508); + hframe_copy2->GetXaxis()->SetLabelFont(42); + hframe_copy2->GetXaxis()->SetLabelOffset(0.007); + hframe_copy2->GetXaxis()->SetTitleSize(0.05); + hframe_copy2->GetXaxis()->SetTitleOffset(1.05); + hframe_copy2->GetXaxis()->SetTitleFont(42); + hframe_copy2->GetYaxis()->SetTitle("#sigma #times #bf{#it{#Beta}}(G_{Bulk} #rightarrow ZZ) (pb)"); + hframe_copy2->GetYaxis()->SetLabelFont(42); + hframe_copy2->GetYaxis()->SetLabelOffset(0.007); + hframe_copy2->GetYaxis()->SetTitleSize(0.05); + hframe_copy2->GetYaxis()->SetTitleOffset(1.15); + hframe_copy2->GetYaxis()->SetTitleFont(42); + hframe_copy2->GetZaxis()->SetLabelFont(42); + hframe_copy2->GetZaxis()->SetLabelOffset(0.007); + hframe_copy2->GetZaxis()->SetLabelSize(0.05); + hframe_copy2->GetZaxis()->SetTitleSize(0.06); + hframe_copy2->GetZaxis()->SetTitleFont(42); + hframe_copy2->Draw("sameaxis"); + + TH1F *hframe_copy3 = new TH1F("hframe_copy3","",1000,1.2,4.1); + hframe_copy3->SetMinimum(0.0001); + hframe_copy3->SetMaximum(109); + hframe_copy3->SetDirectory(0); + hframe_copy3->SetStats(0); + hframe_copy3->SetLineStyle(0); + hframe_copy3->SetMarkerStyle(20); + hframe_copy3->GetXaxis()->SetTitle("M_{G_{Bulk}} (TeV)"); + hframe_copy3->GetXaxis()->SetNdivisions(508); + hframe_copy3->GetXaxis()->SetLabelFont(42); + hframe_copy3->GetXaxis()->SetLabelOffset(0.007); + hframe_copy3->GetXaxis()->SetTitleSize(0.05); + hframe_copy3->GetXaxis()->SetTitleOffset(1.05); + hframe_copy3->GetXaxis()->SetTitleFont(42); + hframe_copy3->GetYaxis()->SetTitle("#sigma #times #bf{#it{#Beta}}(G_{Bulk} #rightarrow ZZ) (pb)"); + hframe_copy3->GetYaxis()->SetLabelFont(42); + hframe_copy3->GetYaxis()->SetLabelOffset(0.007); + hframe_copy3->GetYaxis()->SetTitleSize(0.05); + hframe_copy3->GetYaxis()->SetTitleOffset(1.15); + hframe_copy3->GetYaxis()->SetTitleFont(42); + hframe_copy3->GetZaxis()->SetLabelFont(42); + hframe_copy3->GetZaxis()->SetLabelOffset(0.007); + hframe_copy3->GetZaxis()->SetLabelSize(0.05); + hframe_copy3->GetZaxis()->SetTitleSize(0.06); + hframe_copy3->GetZaxis()->SetTitleFont(42); + hframe_copy3->Draw("sameaxig"); + + TLegend *leg = new TLegend(0.52,0.6002591,0.806734,0.9011917,NULL,"brNDC"); + leg->SetBorderSize(1); + leg->SetTextFont(62); + leg->SetTextSize(0.038); + leg->SetLineColor(0); + leg->SetLineStyle(1); + leg->SetLineWidth(1); + leg->SetFillColor(0); + leg->SetFillStyle(1001); + TLegendEntry *entry=leg->AddEntry("Graph3","Observed","Lp"); + entry->SetLineColor(1); + entry->SetLineStyle(1); + entry->SetLineWidth(2); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(8); + entry->SetMarkerSize(0.8); + entry->SetTextFont(62); + entry=leg->AddEntry("Graph1","Expected #pm 1 std. deviation","f"); + + ci = TColor::GetColor("#00cc00"); + entry->SetFillColor(ci); + entry->SetFillStyle(1001); + + ci = TColor::GetColor("#00cc00"); + entry->SetLineColor(ci); + entry->SetLineStyle(1); + entry->SetLineWidth(1); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + entry=leg->AddEntry("Graph0","Expected #pm 2 std. deviation","f"); + + ci = TColor::GetColor("#ffcc00"); + entry->SetFillColor(ci); + entry->SetFillStyle(1001); + + ci = TColor::GetColor("#ffcc00"); + entry->SetLineColor(ci); + entry->SetLineStyle(1); + entry->SetLineWidth(1); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + entry=leg->AddEntry("BulkZZ_gtheory","#sigma_{TH}#times#bf{#it{#Beta}}(G_{Bulk}#rightarrowZZ) #tilde{k}=0.5","L"); + + ci = TColor::GetColor("#ff0000"); + entry->SetLineColor(ci); + entry->SetLineStyle(1); + entry->SetLineWidth(3); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + leg->Draw(); + + leg = new TLegend(0.52,0.6002591,0.8046734,0.9011917,NULL,"brNDC"); + leg->SetTextFont(62); + leg->SetTextSize(0.038); + leg->SetLineColor(0); + leg->SetLineStyle(1); + leg->SetLineWidth(1); + leg->SetFillColor(0); + leg->SetFillStyle(0); + entry=leg->AddEntry("Graph3"," ",""); + entry->SetLineColor(1); + entry->SetLineStyle(1); + entry->SetLineWidth(1); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + entry=leg->AddEntry("Graph2"," ","L"); + entry->SetLineColor(1); + entry->SetLineStyle(3); + entry->SetLineWidth(4); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + entry=leg->AddEntry("Graph2"," ","L"); + entry->SetLineColor(1); + entry->SetLineStyle(3); + entry->SetLineWidth(4); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + entry=leg->AddEntry("BulkZZ_grshade"," ","F"); + + ci = TColor::GetColor("#ff0000"); + entry->SetFillColor(ci); + entry->SetFillStyle(3013); + entry->SetLineStyle(1); + entry->SetLineWidth(1); + entry->SetMarkerColor(1); + entry->SetMarkerStyle(21); + entry->SetMarkerSize(1); + entry->SetTextFont(62); + leg->Draw(); + c1->Modified(); + c1->cd(); + c1->SetSelected(c1); +} diff --git a/examples/read_c_file.ipynb b/examples/read_c_file.ipynb new file mode 100644 index 00000000..31fc5feb --- /dev/null +++ b/examples/read_c_file.ipynb @@ -0,0 +1,250 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Notebook for reading TGraphs from '.C' files\n", + "\n", + "With this notebook you can read TGraphs from '.C' files. As explained in the \"Getting started\" notebook, a `Submission` needs to exist or be created." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to JupyROOT 6.20/04\n" + ] + } + ], + "source": [ + "from hepdata_lib import Submission\n", + "submission = Submission()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from hepdata_lib import Table\n", + "table = Table(\"MyLimit\")\n", + "table.description = \"Here is the description.\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we import the `CFileReader` class from `hepdata_lib` and read a '.C' file." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/clange/hepdata_lib/hepdata_lib/root_utils.py:412: FutureWarning: ROOT.Double is deprecated and will disappear in a future version of ROOT. Instead, use ctypes.c_double for pass-by-ref of doubles\n", + " graph.GetPoint(i, x_val, y_val)\n" + ] + } + ], + "source": [ + "from hepdata_lib.c_file_reader import CFileReader\n", + "c_file = \"example_inputs/brazilianFlag_BulkZZ_VVnew_new_combined_13TeV.C\"\n", + "reader = CFileReader(c_file)\n", + "graphs = reader.get_graphs()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once you have read the '.C' file, you can list the included graphs with the following command:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Graph0', 'Graph1', 'BulkZZ_grshade', 'Graph2', 'Graph3', 'BulkZZ_gtheory']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(graphs.keys())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`CFileReader` lists all `TGraphs` as a dictionary. You can access the included values by opening individual dictionaries and choosing the variable you want." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from hepdata_lib import Variable, Uncertainty\n", + "d = Variable(\"Bulk graviton mass\", is_independent=True, is_binned=False, units=\"GeV\")\n", + "d.values = graphs[\"Graph0\"]['x'][:30]\n", + "\n", + "obs = Variable(\"BulkZZ\", is_independent=False, is_binned=False, units=\"pb\")\n", + "obs.values = graphs[\"Graph2\"]['y'][:30]\n", + "obs.add_qualifier(\"Limit\", \"Observed\")\n", + "obs.add_qualifier(\"SQRT(S)\", 13, \"TeV\")\n", + "obs.add_qualifier(\"LUMINOSITY\", 35.9, \"fb$^{-1}$\")\n", + "\n", + "exp = Variable(\"BulkZZ\", is_independent=False, is_binned=False, units=\"pb\")\n", + "exp.values = graphs[\"Graph3\"]['y'][:30]\n", + "exp.add_qualifier(\"Limit\", \"Expected\")\n", + "exp.add_qualifier(\"SQRT(S)\", 13, \"TeV\")\n", + "exp.add_qualifier(\"LUMINOSITY\", 35.9, \"fb$^{-1}$\")\n", + "\n", + "#+/- 1 sigma\n", + "unc_1s = Uncertainty(\"1 s.d.\", is_symmetric=False)\n", + "unc_1s.set_values_from_intervals(zip(graphs[\"Graph1\"]['y'][:30], graphs[\"Graph1\"]['y'][::-1][:30]), nominal=exp.values)\n", + "exp.add_uncertainty(unc_1s)\n", + "\n", + "# +/- 2 sigma\n", + "unc_2s = Uncertainty(\"2 s.d.\", is_symmetric=False)\n", + "unc_2s.set_values_from_intervals(zip(graphs[\"Graph0\"]['y'][:30], graphs[\"Graph0\"]['y'][::-1][:30]), nominal=exp.values)\n", + "exp.add_uncertainty(unc_2s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "All that needs to be done is to add the variables to the `Table`, the `Table` to the `Submission`, and then run the command to create the output files. Again, please refer to the 'Getting started' notebook for a complete example." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "table.add_variable(d)\n", + "table.add_variable(obs)\n", + "table.add_variable(exp)\n", + "submission.add_table(table)\n", + "submission.create_files(\"example_output\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mylimit.yaml submission.yaml\r\n" + ] + } + ], + "source": [ + "!ls example_output" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dependent_variables:\r\n", + "- header:\r\n", + " name: BulkZZ\r\n", + " units: pb\r\n", + " qualifiers:\r\n", + " - name: Limit\r\n", + " value: Observed\r\n", + " - name: SQRT(S)\r\n", + " units: TeV\r\n", + " value: 13\r\n", + " - name: LUMINOSITY\r\n", + " units: fb$^{-1}$\r\n", + " value: 35.9\r\n", + " values:\r\n", + " - value: 0.033069\r\n", + " - value: 0.023478\r\n", + " - value: 0.017733\r\n", + " - value: 0.013937\r\n", + " - value: 0.01084\r\n", + " - value: 0.0086169\r\n", + " - value: 0.0069185\r\n", + " - value: 0.0056197\r\n", + " - value: 0.0046831\r\n", + " - value: 0.0039838\r\n", + " - value: 0.0034093\r\n", + " - value: 0.0030346\r\n", + " - value: 0.0027099\r\n", + " - value: 0.0024539\r\n", + " - value: 0.0022541\r\n", + " - value: 0.0020543\r\n" + ] + } + ], + "source": [ + "!head -n 30 example_output/mylimit.yaml" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/hepdata_lib/c_file_reader.py b/hepdata_lib/c_file_reader.py new file mode 100644 index 00000000..8a688e7d --- /dev/null +++ b/hepdata_lib/c_file_reader.py @@ -0,0 +1,430 @@ +""".C file reader""" + +import io +from array import array +import six +from ROOT import TGraph, TGraphErrors +import hepdata_lib.root_utils as ru +from hepdata_lib.helpers import check_file_existence + +class CFileReader(object): + """Reads ROOT Objects from .C files""" + + def __init__(self, cfile): + """Initializing cfile""" + self._cfile = None + self.cfile = cfile + + def __del__(self): + if self._cfile: + self._cfile.close() + + @property + def cfile(self): + """The .C file this reader reads from.""" + return self._cfile + + @cfile.setter + def cfile(self, cfile): + """ + Define the '.C' file to read from. + """ + if isinstance(cfile, str): + if not cfile.lower().endswith(".c"): + raise RuntimeError( + "CFileReader: Input file is not a .C file (name does not end in .C)!" + ) + if check_file_existence(cfile): + self._cfile = open(cfile, "r") + elif six.PY2: + if isinstance(cfile, file): # pylint:disable=E0602 + self._cfile = cfile + else: + raise ValueError( + "CFileReader: Encountered unknown type of variable passed as cfile argument: " + + str(type(cfile))) + else: + if isinstance(cfile, io.TextIOBase): + self._cfile = cfile + else: + raise ValueError( + "CFileReader: Encountered unknown type of variable passed as cfile argument: " + + str(type(cfile))) + if not self._cfile: + raise IOError("CFileReader: File not opened properly.") + + def get_graphs(self): + """Parse the .C file trying to find TGraph objects""" + + # Getting tgraph variables and names + found_graphs = self.find_graphs() + graphs = found_graphs[0] + tgraph_names = found_graphs[1] + tgraph_errors = found_graphs[2] + error_names = found_graphs[3] + list_of_tgraphs = [] + + # Creating and adding TGraphs to a dictionary + dict_of_graphs = self.create_tgraph_dict(graphs, list_of_tgraphs) + list_of_tgraphs = zip(tgraph_names, dict_of_graphs) + + # Creating and adding TGraphsErrors to a dictionary + dict_of_graphs = self.create_tgrapherrors_dict(tgraph_errors) + list_of_errors = zip(error_names, dict_of_graphs) + + # Combining dictionaries + graph_object = {} + graph_object.update(list_of_tgraphs) + graph_object.update(list_of_errors) + all_graphs = graph_object + + # Returning a complete dictionary + return all_graphs + + def create_tgraph_dict(self, graph_list, list_of_tgraphs): + """Function to create pyroot TGraph dict""" + # pylint: disable=no-self-use + + # Adding tgraphs into a dictionary + y_values = [] + x_values = [] + graphs = graph_list + count = 0 + while count < len(graphs) -1: + xvalues = self.read_graph(graphs[count]) + yvalues = self.read_graph(graphs[count+1]) + try: + if (all(isinstance(x, int) for x in xvalues) + and all(isinstance(x, int) for x in yvalues)): + for value in xvalues: + x_values.append(value) + for value in yvalues: + y_values.append(value) + elif(any(not isinstance(x, int) for x in xvalues) + or any(not isinstance(x, int) for x in yvalues)): + for value in xvalues: + x_values.append(float(value)) + for value in yvalues: + y_values.append(float(value)) + except ValueError: + raise IndexError("Invalid values. Int or float required.") + tgraph = self.create_tgraph(x_values, y_values) + tgraph = dict(tgraph) + list_of_tgraphs.append(tgraph) + count += 2 + y_values = [] + x_values = [] + + return list_of_tgraphs + + def create_tgrapherrors_dict(self, graph_list): + """Function to create pyroot TGraphErrors dict""" + # pylint: disable=no-self-use + + # Adding TGraphErrors into a dictionary + y_values = [] + x_values = [] + dy_values = [] + dx_values = [] + list_of_tgraphs = [] + count = 0 + while count < len(graph_list) -1: + xvalues = self.read_graph(graph_list[count]) + yvalues = self.read_graph(graph_list[count+1]) + dxvalues = self.read_graph(graph_list[count+2]) + dyvalues = self.read_graph(graph_list[count+3]) + try: + if (all(isinstance(x, int) for x in xvalues) + and all(isinstance(x, int) for x in yvalues) + and all(isinstance(x, int) for x in dxvalues) + and all(isinstance(x, int) for x in dyvalues)): + for value in xvalues: + x_values.append(value) + for value in yvalues: + y_values.append(value) + for value in dxvalues: + dx_values.append(value) + for value in dyvalues: + dy_values.append(value) + elif(any(not isinstance(x, int) for x in xvalues) + or any(not isinstance(x, int) for x in xvalues) + or any(not isinstance(x, int) for x in dxvalues) + or any(not isinstance(x, int) for x in dyvalues)): + for value in xvalues: + x_values.append(float(value)) + for value in yvalues: + y_values.append(float(value)) + for value in dxvalues: + dx_values.append(float(value)) + for value in dyvalues: + dy_values.append(float(value)) + except ValueError: + raise IndexError("Invalid values. Int or float required.") + tgraph_error = self.create_tgrapherrors(x_values, y_values, dx_values, dy_values) + tgraph_error = dict(tgraph_error) + list_of_tgraphs.append(tgraph_error) + count += 4 + y_values = [] + x_values = [] + dy_values = [] + dx_values = [] + + return list_of_tgraphs + + def create_tgrapherrors(self, x_value, y_value, dx_value, dy_value): + """Function to create pyroot TGraphErrors object""" + # pylint: disable=no-self-use + + # Creating pyroot TGraphErrors object + x_values = array('i') + y_values = array('i') + dx_values = array('i') + dy_values = array('i') + length = len(x_value) + if (all(isinstance(x, int) for x in x_value) + and all(isinstance(x, int) for x in y_value) + and all(isinstance(x, int) for x in dx_value) + and all(isinstance(x, int) for x in dy_value)): + y_values = array('i') + x_values = array('i') + dx_values = array('i') + dy_values = array('i') + else: + y_values = array('d') + x_values = array('d') + dx_values = array('d') + dy_values = array('d') + for value in range(length): + x_values.append(x_value[value]) + y_values.append(y_value[value]) + dx_values.append(dx_value[value]) + dy_values.append(dy_value[value]) + t_object = TGraphErrors(length, x_values, y_values, dx_values, dy_values) + graph = ru.get_graph_points(t_object) + + return graph + + def create_tgraph(self, x_value, y_value): + """Function to create pyroot TGraph object""" + # pylint: disable=no-self-use + + # Creating pyroot TGraph object + x_values = array('i') + y_values = array('i') + length = len(x_value) + if (all(isinstance(x, int) for x in x_value) + and all(isinstance(x, int) for x in y_value)): + y_values = array('i') + x_values = array('i') + else: + y_values = array('d') + x_values = array('d') + for value in range(length): + x_values.append(x_value[value]) + y_values.append(y_value[value]) + t_object = TGraph(length, x_values, y_values) + graph = ru.get_graph_points(t_object) + + return graph + + def check_for_comments(self, line): + """Check line for comment""" + # pylint: disable=no-self-use + + _line = line + ignoreline = 0 + continueline = False + if _line.startswith('/*'): + continueline = True + ignoreline = 1 + return continueline, ignoreline, _line + + if _line.startswith('//'): + continueline = True + ignoreline = 0 + return continueline, ignoreline, _line + + if '/*' in _line: + _line = _line.split('/*', 1)[0] + _line = _line.rstrip() + continueline = False + ignoreline = 1 + return continueline, ignoreline, _line + + if '//' in _line: + _line = _line.split('//', 1)[0] + _line = _line.rstrip() + continueline = False + ignoreline = 0 + return continueline, ignoreline, _line + + if '*/' in _line: + _line = _line.split('*/', 1)[1] + _line = _line.rstrip() + continueline = False + ignoreline = 0 + return continueline, ignoreline, _line + return continueline, ignoreline, _line + + def find_graphs(self): + """Find all TGraphs in .C file""" + # pylint: disable=too-many-branches + # pylint: disable=too-many-statements + # pylint: disable=too-many-locals + + c_file = self.cfile + tgraph_names = [] + tgrapherror_names = [] + normal_objects = [] + error_objects = [] + tgraphs = [] + tgraph_errors = [] + start = 0 + ignore = 0 + counter = 0 + #Parsing .C file for variables and names + for line in c_file.readlines(): + checkline = self.check_for_comments(line) + ignore = checkline[1] + if checkline[0] is True: + continue + line = checkline[2] + if(("TGraphErrors(" in line) and ("(" in line) and + (ignore == 0)): + start = 2 + splitline = line.split('(', 1)[1].split(')')[0] + splitlines = splitline.split(',') + for i in splitlines: + j = i.replace(' ', '') + error_objects.append(j) + continue + if(("TGraph(" in line) and ("(" in line) and + (ignore == 0)): + start = 1 + splitline = line.split('(', 1)[1].split(')')[0] + splitlines = splitline.split(',') + for i in splitlines: + j = i.replace(' ', '') + normal_objects.append(j) + continue + if(("SetName(" in line) and ("(" in line) and (ignore == 0) and (counter < 5)): + if start == 1: + try: + tgraph_names.append(line.split('"', 1)[1].split('"')[0]) + except IndexError: + tgraph_names = 'null' + raise IndexError("index out of range") + start = 0 + counter = 0 + if start == 2: + try: + tgrapherror_names.append(line.split('"', 1)[1].split('"')[0]) + except IndexError: + tgrapherror_names = 'null' + raise IndexError("index out of range") + start = 0 + counter = 0 + + #Adding a default name, if no name is found + if start == 1: + counter += 1 + if start == 2: + counter += 1 + if ((start == 1) and (counter >= 5)): + tgraph_names.append("tgraph") + start = 0 + counter = 0 + if ((start == 2) and (counter >= 5)): + tgrapherror_names.append("tgraph") + start = 0 + counter = 0 + + for item in normal_objects: + for subitem in item.split(","): + if subitem.isdigit() is False: + tgraphs.append(subitem) + for item in error_objects: + for subitem in item.split(","): + if subitem.isdigit() is False: + tgraph_errors.append(subitem) + + return tgraphs, tgraph_names, tgraph_errors, tgrapherror_names + + def read_graph(self, graphname): + """Function to read values of a graph""" + # pylint: disable=too-many-branches + # pylint: disable=too-many-statements + + c_file = self.cfile + objects = [] + values = [] + start = 0 + ignore = 0 + c_file.seek(0, 0) + + # Finding values from the correct object + for line in c_file.readlines(): + checkline = self.check_for_comments(line) + ignore = checkline[1] + if checkline[0] is True: + continue + line = checkline[2] + if((graphname in line) and ("{" in line) and ignore == 0 + and (not "}" in line)): + splitline = line.split(graphname, 1)[1] + splitline = splitline.split('{', 1)[1].split(',')[0].replace(' ', '') + try: + try: + _test = float(splitline) + objects.append(splitline) + start = 1 + except ValueError: + _test = int(splitline) + objects.append(splitline) + start = 1 + except ValueError: + start = 1 + continue + start = 1 + continue + if((graphname in line) and ("{" in line) and ignore == 0 + and ("}" in line)): + if start == 1: + splitline = line.split("}", 1)[0] + objects.append(splitline) + splitline = line.split(graphname, 1)[1] + if ("{" in splitline) and ("}" in splitline): + splitline = splitline.split('{', 1)[1].split('}')[0] + splitlines = splitline.split(',') + for i in splitlines: + j = i.replace(' ', '') + objects.append(j) + else: + continue + if "}" in line: + if start == 1: + splitline = line.split("}", 1)[0] + try: + try: + _test = float(splitline) + objects.append(splitline) + start = 0 + except ValueError: + _test = int(splitline) + objects.append(splitline) + start = 0 + except ValueError: + start = 0 + continue + start = 0 + if start == 1: + objects.append(line.split(",")[0]) + for i in objects: + try: + try: + values.append(int(i)) + except ValueError: + values.append(float(i)) + except ValueError: + raise ValueError("Value is not a number in variable:", graphname) + return values diff --git a/setup.py b/setup.py index fef133c3..e2eebba1 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ zip_safe=False, install_requires=DEPS, setup_requires=['pytest-runner', 'pytest-cov'], - tests_require=['pytest', 'papermill'], + tests_require=['pytest', 'papermill', 'six'], project_urls={ 'Documentation': 'https://hepdata-lib.readthedocs.io', 'Bug Reports': 'https://github.com/HEPData/hepdata_lib/issues', diff --git a/tests/test_cfilereader.py b/tests/test_cfilereader.py new file mode 100644 index 00000000..5d658d09 --- /dev/null +++ b/tests/test_cfilereader.py @@ -0,0 +1,364 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +"""Test CFileReader.""" +from __future__ import print_function +from unittest import TestCase +import os +import numpy as np +from hepdata_lib.c_file_reader import CFileReader + +class TestCFileReader(TestCase): + """Test the CFileReader class.""" + + def test_cfile_setter(self): + """ + Test the behavior of the CFileReader setters. + """ + + # Check with nonexistent file that ends in .C + with self.assertRaises(RuntimeError): + _reader = CFileReader("/path/to/nowhere/butEndsIn.C") + # Check with lowercase .c + with self.assertRaises(RuntimeError): + _reader = CFileReader("/path/to/nowhere/butEndsIn.c") + + # Check with existent file that does not end in .C + _file = "text.txt" + with open(_file, "w") as testfile: + testfile.write("TEST CONTENT") + + self.addCleanup(os.remove, _file) + with self.assertRaises(RuntimeError): + _reader = CFileReader(_file) + + # Check with wrong input type + with self.assertRaises(ValueError): + _reader = CFileReader(5) + with self.assertRaises(ValueError): + _reader = CFileReader([]) + with self.assertRaises(ValueError): + _reader = CFileReader({}) + + # Finally, try with good calls + _cfile = "test.C" + with open(_cfile, "w") as testfile: + testfile.write("TGraph* c = new TGraph(60, x_values, y_values)") + + self.addCleanup(os.remove, _cfile) + + # Use file name for opening + try: + _reader = CFileReader(_cfile) + # pylint: disable=W0702 + except: + self.fail("CFileReader raised an unexpected exception.") + # pylint: enable=W0702 + + # Use opened file (io.TextIOBase) + try: + with open(_cfile, "r") as testfile: + _reader = CFileReader(testfile) + # pylint: disable=W0702 + except: + self.fail("CFileReader raised an unexpected exception.") + # pylint: enable=W0702 + + # Clean up + self.doCleanups() + + + def test_get_graphs(self): + """Test function for getting TGraphs from .C file""" + + # Test create a valid dictionary of TGraphs + test_file = "test.C" + with open(test_file, "w") as testfile: + testfile.write( + 'void test() {\n' + 'Double_t Graph0_fx1[2] = {1,2};\n' + + 'Double_t Graph0_fy1[2] = { \n3,\n2};' + + 'TGraph *graph = new TGraph(2,Graph0_fx1,Graph0_fy1);\n' + + 'graph->SetName("Graph0");\n' + + 'Double_t Graph0_fx2[5] ={ 1.2, 2.2 };\n' + + 'Double_t Graph0_fy2[5] = { \n0.123345, \n0.343564};\n' + + 'graph = new TGraph( 5, Graph0_fx2,Graph0_fy2);\n' + + 'graph->SetName("Graph1");\n' + + 'Double_t Graph2_fx1001[30] = {1.2,\n1.3};\n' + + 'Double_t Graph2_fy1001[30] = {0.03306888,\n0.0234779};\n' + + 'Double_t Graph2_fex1001[30] = {0,\n0};' + + 'Double_t Graph2_fey1001[30] = { 0 ,0 };' + + 'TGraphErrors gre = TGraphErrors(30,Graph2_fx1001,Graph2_fy1001,' + + 'Graph2_fex1001,Graph2_fey1001);\n' + + 'gre.SetName("Graph2");}' + ) + graph0_x = [1, 2] + graph0_y = [3, 2] + graph1_x = [1.2, 2.2] + graph1_y = [0.123345, 0.343564] + graph0_x = [1, 2] + graph0_y = [3, 2] + graph2_x = [1.2, 1.3] + graph2_y = [0.03306888, 0.0234779] + graph2_dx = [0, 0] + graph2_dy = [0, 0] + reader = CFileReader(test_file) + tgraphs = reader.get_graphs() + + self.assertTrue(tgraphs["Graph0"]["x"] == graph0_x) + self.assertTrue(tgraphs["Graph0"]["y"] == graph0_y) + self.assertTrue(tgraphs["Graph1"]["x"] == graph1_x) + self.assertTrue(tgraphs["Graph1"]["y"] == graph1_y) + self.assertTrue(tgraphs["Graph2"]["x"] == graph2_x) + self.assertTrue(tgraphs["Graph2"]["y"] == graph2_y) + self.assertTrue(tgraphs["Graph2"]["dx"] == graph2_dx) + self.assertTrue(tgraphs["Graph2"]["dy"] == graph2_dy) + + # Testing with invalid x and y values + with open(test_file, "w") as testfile: + testfile.write( + 'void test() {\n' + + 'Double_t Graph0_fx1[2] = {test,test};\n' + + 'Double_t Graph0_fy1[2] = { \ntest,\ntest};\n' + + 'TGraph *graph = new TGraph(2,Graph0_fx1,Graph0_fy1);\n' + + 'graph->SetName("Graph0");}' + ) + reader = CFileReader(test_file) + with self.assertRaises(ValueError): + reader.get_graphs() + + self.addCleanup(os.remove, test_file) + self.doCleanups() + + def test_create_tgrapherrors(self): + """Test function to create pyroot TGraph object""" + + # Create a valid pyroot TGraphErrors object + _length = 10 + x_value = np.random.uniform(-1e3, 1e3, _length) + y_value = np.random.uniform(-1e3, 1e3, _length) + dx_value = np.random.uniform(0, 0, _length) + dy_value = np.random.uniform(0, 0, _length) + + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('test') + reader = CFileReader(c_file) + graph = reader.create_tgrapherrors(x_value, y_value, dx_value, dy_value) + + self.assertTrue(set(graph.keys()) == set(["x", "y", "dx", "dy"])) + self.assertTrue(all(graph["x"] == x_value)) + self.assertTrue(all(graph["y"] == y_value)) + self.addCleanup(os.remove, c_file) + self.doCleanups() + + def test_create_tgraph(self): + """Test function to create pyroot TGraph object""" + + # Create a valid pyroot TGraph object + _length = 10 + x_value = np.random.uniform(-1e3, 1e3, _length) + y_value = np.random.uniform(-1e3, 1e3, _length) + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('test') + reader = CFileReader(c_file) + graph = reader.create_tgraph(x_value, y_value) + + self.assertTrue(set(graph.keys()) == set(["x", "y"])) + self.assertTrue(all(graph["x"] == x_value)) + self.assertTrue(all(graph["y"] == y_value)) + self.addCleanup(os.remove, c_file) + self.doCleanups() + + def test_find_graphs(self): + """Test function to find TGraph variable names""" + + # Test normal TGraph object + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'TGraph *graph = new TGraph(5,Graph0_fx1,Graph0_fy1);' + + '\ngraph->SetName("Graph0");}') + reader = CFileReader(c_file) + objects = reader.find_graphs() + test1 = objects[0] + test2 = objects[1] + names = ["Graph0_fx1", "Graph0_fy1"] + graphs = ["Graph0"] + self.assertTrue(test1 == names) + self.assertTrue(test2 == graphs) + + # Test with whole line in comment + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + '//TGraph *graph = new TGraph(5,Graph0_fx1,Graph0_fy1);' + + '\n//graph->SetName("Graph0");}') + reader = CFileReader(c_file) + objects = reader.find_graphs() + test1 = objects[0] + test2 = objects[1] + self.assertFalse(test1 == names) + self.assertFalse(test2 == graphs) + + # Test with comment block + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'TGraph *graph = new TGraph(5,Graph0_fx1,Graph0_fy1);' + + '\n/*graph->SetName("Graph0"*/);}') + reader = CFileReader(c_file) + objects = reader.find_graphs() + test1 = objects[0] + test2 = objects[1] + self.assertTrue(test1 == names) + self.assertListEqual(test1, names) + self.assertFalse(test2 == graphs) + + self.addCleanup(os.remove, c_file) + self.doCleanups() + + # Test with whitespaces + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'TGraph *graph = new TGraph(5 ,Graph0_fx1 ,Graph0_fy1);' + + '\ngraph->SetName("Graph0" );}') + reader = CFileReader(c_file) + objects = reader.find_graphs() + test1 = objects[0] + test2 = objects[1] + self.assertTrue(test1 == names) + self.assertTrue(test2 == graphs) + + # Test with line breaks + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'TGraph *graph = new TGraph(5,\n Graph0_fx1, Graph0_fy1);' + + '\ngraph->SetName(\n"Graph0");}') + reader = CFileReader(c_file) + with self.assertRaises(IndexError): + reader.find_graphs() + + def test_read_graph(self): + """Test function to read values""" + + # Testing with a good .C file + graph_names = ["Graph0_fx1", "Graph0_fy1"] + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'Double_t Graph0_fx1[5] = { ' + + '\n1.2,\n1.3};\n' + + 'Double_t Graph0_fy1[5] = { \n0.1666473,\n' + + '0.1284744};\n}') + reader = CFileReader(c_file) + x_values = reader.read_graph(graph_names[0]) + y_values = reader.read_graph(graph_names[1]) + test_xvalues = [1.2, 1.3] + test_yvalues = [0.1666473, 0.1284744] + self.assertListEqual(test_xvalues, x_values) + self.assertListEqual(test_yvalues, y_values) + + # Testing with invalid values + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'Double_t Graph0_fx1[5] = { ' + + '\ntest,\n%&/};\n' + + 'Double_t Graph0_fy1[5] = { \n!"#,\n' + + '"testing"};}') + with self.assertRaises(ValueError): + reader.read_graph(graph_names[0]) + with self.assertRaises(ValueError): + reader.read_graph(graph_names[1]) + + # Testing lines that end in comment + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'Double_t Graph0_fx1[5] = { //Comment ' + + '\n1.2,//Comment\n1.3};\n' + + 'Double_t Graph0_fy1[5] = { \n0.1666473, //Comment\n' + + '0.1284744};}') + x_values = reader.read_graph(graph_names[0]) + y_values = reader.read_graph(graph_names[1]) + self.assertListEqual(test_xvalues, x_values) + self.assertListEqual(test_yvalues, y_values) + + # Testing lines that start with comment + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + 'Double_t Graph0_fx1[5] = { ' + + '\n//1.2,\n1.3};\n' + + 'Double_t Graph0_fy1[5] = { \n0.1666473,\n' + + '0.1284744};}') + test_xvalues = [1.3] + test_yvalues = [0.1666473, 0.1284744] + x_values = reader.read_graph(graph_names[0]) + y_values = reader.read_graph(graph_names[1]) + self.assertListEqual(test_xvalues, x_values) + self.assertListEqual(test_yvalues, y_values) + + # Testing lines with comment block + with open(c_file, "w") as testfile: + testfile.write('void test() {\n' + + '/*Double_t Graph0_fx1[5] = { ' + + '\n1.2,\n1.3*/};\n' + + 'Double_t Graph0_fy1[5] = { \n0.1666473,\n' + + '0.1284744};}') + test_xvalues = [] + test_yvalues = [0.1666473, 0.1284744] + x_values = reader.read_graph(graph_names[0]) + y_values = reader.read_graph(graph_names[1]) + self.assertListEqual(test_xvalues, x_values) + self.assertListEqual(test_yvalues, y_values) + self.addCleanup(os.remove, c_file) + self.doCleanups() + + def test_check_for_comments(self): + """Test function to read values""" + + # Test with a clean line + test_line = "This is just a test" + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('test') + reader = CFileReader(c_file) + line_test = reader.check_for_comments(test_line) + + self.assertTrue(line_test[0] is False) + self.assertTrue(line_test[1] == 0) + self.assertTrue(line_test[2] == test_line) + + # Test with a comment block starting in the middle + test_line = "This is /*just a test" + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('test') + reader = CFileReader(c_file) + line_test = reader.check_for_comments(test_line) + self.assertTrue(line_test[0] is False) + self.assertTrue(line_test[1] == 1) + self.assertTrue(line_test[2] == "This is") + + # Test with a comment block that ends in mid line + test_line = "This is */ just a test" + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('test') + reader = CFileReader(c_file) + line_test = reader.check_for_comments(test_line) + + self.assertTrue(line_test[0] is False) + self.assertTrue(line_test[1] == 0) + self.assertTrue(line_test[2] == " just a test") + + # Test with whole line in a comment + test_line = "//This is just a test" + c_file = "test.C" + with open(c_file, "w") as testfile: + testfile.write('test') + reader = CFileReader(c_file) + line_test = reader.check_for_comments(test_line) + self.assertTrue(line_test[0]) + self.assertTrue(line_test[1] == 0) + self.assertTrue(line_test[2] == test_line) + + self.addCleanup(os.remove, c_file) + self.doCleanups() diff --git a/tests/test_notebooks.py b/tests/test_notebooks.py index f64e141d..b5d276c8 100644 --- a/tests/test_notebooks.py +++ b/tests/test_notebooks.py @@ -28,3 +28,7 @@ def test_reading_histograms(common_kwargs):# pylint: disable=redefined-outer-nam def test_combine_limits(common_kwargs):# pylint: disable=redefined-outer-name """Tests examples/combine_limits.ipynb""" pm.execute_notebook('examples/combine_limits.ipynb', **common_kwargs) + +def test_c_file(common_kwargs):# pylint: disable=redefined-outer-name + """Tests examples/read_c_file.ipynb""" + pm.execute_notebook('examples/read_c_file.ipynb', **common_kwargs)