# Moving TGraph to a new file

**URL:** <https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917>\
**Category:** ROOT\
**Created:** [August 10, 2011, 6:17pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917 "2011-08-10T18:17:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leggett](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@leggett](https://root-forum.cern.ch/u/leggett)\
**Post date:** [August 10, 2011, 6:17pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/1 "2011-08-10T18:17:11Z")

</div>

In order to do event based parallelism in ATLAS, we have a mother  
process that forks worker child processes to individual cores, each of  
which then processes a number of events individually. In order that  
these child processes not interfere with each others’ I/O, they each  
write their outputs into individual directories. At the end of the  
job, the outputs are merged. In order to maximize memory sharing, this  
forking happens after the first event is processed, by which time many  
output files have already been opened. So what we do is clone these  
files into the individual worker directories, and reset file  
descriptors to point to the appropriate places.

We need to do this in a way that’s transparent to the users, so that  
if they hold a pointer to something in the original file, this pointer  
still needs to be valid after the forking has migrated the files to  
the new locations.

We have a service (the THistSvc in Gaudi) that manages all Histograms,  
Trees and Graphs that are registered with it. It holds all the  
pointers to the individual TTrees, THNs, and TGraphs. When the mother  
process clones the original TFiles in the individual worker  
directories, we update all these objects that are registered with the  
service, so that the pointers point to the objects in the new files,  
but the value of the pointers haven’t changed. This way, users can  
still keep using a pointer that they got before the forking.

When dealing with TFiles that contain TTrees and THNs, this isn’t a  
problem as after we have copied the file structure to the new  
location, we can just do a SetDirectory() that reflects the new TFile.

However, there’s no such method in TGraphs.

What I’ve tried doing is cloning the TGraph in the new file (either  
via the constructor or a Clone() method), then doing a memcpy to  
overwrite the original pointer with the clone, so that the original  
pointer will reference a TGraph in the new file, but this doesn’t  
work.

Can anyone suggest a solution?

```
               cheers, Charles.
```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [August 10, 2011, 7:07pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/2 "2011-08-10T19:07:58Z")

</div>

Hi Charles,

TGraph do not know in which directory there are being held and are not automatically added to any directory  
(But strangly enough TGraph2D do and do have a SetDirectory method).

If the TGraph are held in a TDirectory they have been append to it via ‘directory-\>Append(graph\_pointer)’ and to move the graph from one directory to the other you would need to use:`old_directory->Remove(graph_pointer);
new_directory->Append(graph_pointer);`

Cheers,  
Philippe

---

<div class="post-metadata">

**Author:** ![leggett](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@leggett](https://root-forum.cern.ch/u/leggett)\
**Post date:** [August 10, 2011, 8:05pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/3 "2011-08-10T20:05:50Z")

</div>

Thanks Philippe!That seems to work.

I had tried the dir-\>Append(TGraph\*), but it had segfaulted, as I had neglected to do the old-\>Remove(TGraph\*).

```
   cheers, Charles.

```

(And sorry about the crosspost).

---

<div class="post-metadata">

**Author:** ![Zesp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/b5ac83/32.png) [@Zesp](https://root-forum.cern.ch/u/Zesp)\
**Post date:** [September 12, 2011, 6:54am UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/4 "2011-09-12T06:54:56Z")

</div>

> [@](#):
>
> TGraph do not know in which directory there are being held and are not automatically added to any directory  
> (But strangly enough TGraph2D do and do have a SetDirectory method).

Hi Philippe,

Could you or any ROOT developer please harmonize this,  
i.e. implement TGraph2D directory methods (DirectoryAutoAdd, SetDirectory and GetDirectory) and attributes (fDirectory) in TGraph?

Cheers,  
Z

---

<div class="post-metadata">

**Author:** ![Zesp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/b5ac83/32.png) [@Zesp](https://root-forum.cern.ch/u/Zesp)\
**Post date:** [September 20, 2011, 8:31pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/5 "2011-09-20T20:31:22Z")

</div>

Are there any limitations not to do it?  
Cheers,  
Z

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 20, 2011, 8:33pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/6 "2011-09-20T20:33:46Z")

</div>

Hi,

We have to design/implement it in a way that is backward compatible. We will be discussing this issue in the next few weeks.

Cheers,  
Philippe

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 4, 2011, 8:57am UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/7 "2011-10-04T08:57:53Z")

</div>

Hi,

The usage pattern of TGraph and TGraph2D are very different, effectively the size of TGraph objects are usually much smaller and they are often more TGraphs. Thus we want to keep the management/weight of the TGraph has small as possible. TGraph2D is more akin to an histogram.

In addition, any change to the behavior of TGraph is relation to the ownership would not be backward compatible unless we default to not adding it to the directory … in which case we somewhat introduce more confusion by having this one class have a different default that the other autoAdd objects …

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Zesp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/b5ac83/32.png) [@Zesp](https://root-forum.cern.ch/u/Zesp)\
**Post date:** [October 4, 2011, 12:43pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/8 "2011-10-04T12:43:50Z")

</div>

Ok ☹  
Could you please mention how to deal with TGraph in the manual then?  
I’ve been looking for this feature quite a while…  
Cheers,  
Z

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 3, 2012, 1:53pm UTC](https://root-forum.cern.ch/t/moving-tgraph-to-a-new-file/12917/9 "2012-02-03T13:53:09Z")

</div>

Done.  
Philippe.
