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

**URL:** <https://root-forum.cern.ch/t/how-to-merge-several-tgraphasymmerrors-class-objects-from-several-repositories-into-one-final-canvas/51953>\
**Category:** Newbie\
**Created:** [October 13, 2022, 6:43pm UTC](https://root-forum.cern.ch/t/how-to-merge-several-tgraphasymmerrors-class-objects-from-several-repositories-into-one-final-canvas/51953 "2022-10-13T18:43:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan1/32/24336_2.png) [@Taylor\_Carnahan1](https://root-forum.cern.ch/u/Taylor_Carnahan1)\
**Post date:** [October 13, 2022, 6:43pm UTC](https://root-forum.cern.ch/t/how-to-merge-several-tgraphasymmerrors-class-objects-from-several-repositories-into-one-final-canvas/51953/1 "2022-10-13T18:43:59Z")

</div>

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:

```auto
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:

```auto
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

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 13, 2022, 6:59pm UTC](https://root-forum.cern.ch/t/how-to-merge-several-tgraphasymmerrors-class-objects-from-several-repositories-into-one-final-canvas/51953/2 "2022-10-13T18:59:59Z")

</div>

[TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html)

---

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan1/32/24336_2.png) [@Taylor\_Carnahan1](https://root-forum.cern.ch/u/Taylor_Carnahan1)\
**Post date:** [October 13, 2022, 7:36pm UTC](https://root-forum.cern.ch/t/how-to-merge-several-tgraphasymmerrors-class-objects-from-several-repositories-into-one-final-canvas/51953/3 "2022-10-13T19:36:54Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 14, 2022, 6:01am UTC](https://root-forum.cern.ch/t/how-to-merge-several-tgraphasymmerrors-class-objects-from-several-repositories-into-one-final-canvas/51953/4 "2022-10-14T06:01:03Z")

</div>

If `graphs[i]` are individual `TGraph`s your script is fine.
