How to merge several TGraphAsymmErrors class objects from several repositories into one final canvas

Hello experts! I am having trouble retrieving data inside several ROOT files. I have looked at every Stack Overflow and ROOT forum, but I am still unable to resolve my issue.

I am trying to go through three repositories of structure:

UNIQUEREPO (1 of 3)/same-path-in-each-repo/5_folders/same-next-folder-in-each-repo/unique_name.root

The “5_folders” corresponds to 5 different folders that I need to iterate through in order to get to the final unique_name.root, of which there are 28 in a sea of nearly 100, that I need to pick out. The unique_name.root inherits its name from its “5_folders” parent directory + a binning designator.

In my ROOT TBrowser, if I open one of these unique_name.root files, I see:

TFile**     UL_2022/plots/muon/generalTracks/Z/Run2022/NUM_LooseID_DEN_TrackerMuons/efficiency/NUM_LooseID_DEN_TrackerMuons_abseta_1_vs_pt.root 
 TFile*     UL_2022/plots/muon/generalTracks/Z/Run2022/NUM_LooseID_DEN_TrackerMuons/efficiency/NUM_LooseID_DEN_TrackerMuons_abseta_1_vs_pt.root 
  KEY: TGraphAsymmErrors    g_0_Data;1  Data
  KEY: TGraphAsymmErrors    g_1_Simulation;1    Simulation

My goal is to grab each g_0_Data per repository, for each unique_name.root file. After I have done this, I need to merge all of the g_0_Data data into one canvas and label each contribution via a Legend. Also, I need to grab only 1 g_1_Simulation from each repository, but that should be pretty direct after getting g_0_Data squared away.

I know that keys come into play here and that g_0_Data is the variable, or object name, for the class TGraphAsymmErrors, so I may need to use a way to get the appropriate key designator per unique_name.root per each repository. (grimacing emoji)

My code in attempting to grab the g_0_Data is in this python format below:

inputDir = "UL_2022*/plots/muon/generalTracks/Z/Run2022/"
if not os.path.exists('effplots/'): os.makedirs('effplots/')
outputDir = "effplots/"

c1 = ROOT.TCanvas('c1','c1')
for filename in os.listdir(inputDir):
    inFile = ROOT.TFile.Open(inputDir + filename + "efficiency/" + filename + "_abseta_*.root")
    hist = inFile.Get("g_0_Data")
    hist.Draw("SAME")
c1.SaveAs(outputDir+"trial.png")

I don’t think that this “inFile” constructor is quite right; I need a better way to iterate through 5 folders and 28 unique_names.root and grab their correct objects to merge together in one canvas. Any help would be greatly appreciated!!

_ROOT Version: 6.20/06
_Platform: LXplus
_Compiler: Python


TMultiGraph

Hey @Wile_E_Coyote , thanks for the tip.
Would it look something like this:

graph = ROOT.TGraphAsymmErrors("g_0_Data") #do I need keys??
ng = len(graphs) # number of graphs proportional to the output list length
    mg = ROOT.TMultiGraph() # multigraph initialization
    for i in range(ng):
        graphs[i].SetLineColor(colors[i])
        graphs[i].SetMarkerColor(colors[i]) # sets a different color per input
        mg.Add(graphs[i]) # add

    canvas = ROOT.TCanvas(savename, savename, 800, 800)
    mg.Draw('ALP') # connected line with error bar dots
    mg.GetXaxis().SetTitle(xlabel)

If graphs[i] are individual TGraphs your script is fine.

1 Like