# TTreeReader TChain notify on change of file

**URL:** <https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [February 22, 2024, 12:59pm UTC](https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244 "2024-02-22T12:59:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhubacek](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/9de0a6/32.png) [@zhubacek](https://root-forum.cern.ch/u/zhubacek)\
**Post date:** [February 22, 2024, 12:59pm UTC](https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244/1 "2024-02-22T12:59:18Z")

</div>

Hi, maybe I overlook something - is there some tutorial/example code how to get a notification when **TChain** changes a file in the processing via **TTreeReader**?  
Something like **MakeClass/TSelector ::Notify**?

Thanks,  
Zdenek

* * *

_ROOT Version:_ 6.30.04  
_Platform:_ macOS  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [February 26, 2024, 9:50pm UTC](https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244/2 "2024-02-26T21:50:07Z")

</div>

Dear @zhubacek ,

The feature you are looking for is implemented via [TNotifyLink](https://root.cern.ch/doc/master/classTNotifyLink.html). I am not aware of a specific tutorial for TChain, but in [this tutorial](https://root.cern/doc/master/h1analysisTreeReader_8C.html) there is an example usage at the bottom (the tutorial does many other things). See also the doc of [TChain::LoadTree](https://root.cern.ch/doc/master/classTChain.html#ab6cae493037968229cfff713a3d86844) for an explanation on the usage of the notify object by the chain.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![zhubacek](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/9de0a6/32.png) [@zhubacek](https://root-forum.cern.ch/u/zhubacek)\
**Post date:** [February 28, 2024, 12:43pm UTC](https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244/3 "2024-02-28T12:43:56Z")

</div>

Hi @vpadulan,  
Thanks, but to be honest, I don’t understand, how the `TNotifyLink` should work and some example would be useful - I don’t get what a subscriber should be/if the TNotifyLink should be a template of something?

For the tutorial, yes, I know this one, I could do my own `TSelector` and there I am able to modify the `Notify()` method, but I’m looking for a simpler code (if that is possible).  
I’d like to have a very simple TTreeReader on a TChain:

```auto
TChain *ch = new TChain("myTree");
  ch->Add("file1.root");
  ch->Add("file2.root");

  TTreeReader myReader(ch);
  TTreeReaderValue<int> njet(myReader, "nJets");
  TTreeReaderArray<double> jetpt(myReader, "jetPt");

  int counter = 0;
...
  while (myReader.Next())
    {
      if (ch->FileChanged()) SetSomeFileRelatedWeight();
    }

```

The Notify() seemed to lead to way that I should derive my own class `MyReader` from the `TTreeReader` and implement the notification, but in that case it leads to the `TSelector` which seems to be a unnecessary complicated for a simple reader?

Thanks,  
Zdenek

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [April 11, 2024, 6:12pm UTC](https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244/4 "2024-04-11T18:12:48Z")

</div>

Dear @zhubacek ,

I prepared for you the smallest example that I was able to create, see the attached zipped file. I will paste the instructions to run the contents also here for reference so that the general logic is documented

1. You need a class that acts as the notifier. The only requirement is that the  
class implements a `bool Notify()` method. Write this class in a header. In  
my example the method just prints a notification every time it is called.

2. Create a dictionary for the `TNotifyLink<MyNotifier>` instantiation.  
See [I/O of custom classes - ROOT](https://root.cern/manual/io_custom_classes) for detailed instructions, the  
following is just one way to do it.

3. Compile the program including the dictionary in the compilation:

4. Run the program, notice that at each new file in the chain there will be a  
new notification printed.

[forum\_58244\_tnotifylink.zip](https://root-forum.cern.ch/uploads/short-url/ev9kSbYA2SiIDiRlJeTX1fQs5cd.zip) (7.3 KB)

With TTreeReader it should be trivial to extend by just calling `PrependLink(treereader.GetTree())` or something similar.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![zhubacek](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/9de0a6/32.png) [@zhubacek](https://root-forum.cern.ch/u/zhubacek)\
**Post date:** [April 16, 2024, 8:27pm UTC](https://root-forum.cern.ch/t/ttreereader-tchain-notify-on-change-of-file/58244/5 "2024-04-16T20:27:00Z")

</div>

Dear @vpadulan ,  
Thank you very much!

I slightly extended the notifier to get for example the current filename:

```auto
struct MyNotifier{
  MyNotifier() : fChain(0)
  {}
  void Init(TTree* tree)
  {
    fChain=tree;
  }
    bool Notify(){
      
        std::cout << "Notification! reading new file" << "\n";
	std::cout <<"File name: "<<fChain->GetCurrentFile()->GetName()<<std::endl;

	return true;
    }
  TTree *fChain;
};

```

This works well with the chain, but when I try what you suggest with the TTreeReader:

```auto
...
    TTreeReader myReader(&c);
    TTreeReaderValue<int> var1(myReader,"column1");
    
    MyNotifier n;
    n.Init(&c);
    TNotifyLink<MyNotifier> l{&n};
    l.PrependLink(*myReader.GetTree());
    int counter =0;
    while (myReader.Next())
      {
	counter++;
	std::cout <<"Event: "<<counter<<" "<<*var1<<std::endl;
      }

```

works as well, however I get a warning:  
`Warning in <TChain::TTree::SetNotify>: The tree or chain already has a fNotify registered and it is a TNotifyLink, while the new object is not a TNotifyLink. Setting fNotify to the new value will lead to an orphan linked list of TNotifyLinks and it is most likely not intended. If this is the intended goal, please call SetNotify(nullptr) first to silence this warning.`  
it seems that the notification is set up twice?

In that case, I don’t understand which Notify link is already set up? I tried calling ` c.SetNotify(nullptr);` before setting up `myReader` and `MyNotifier` but this didn’t help?

Thank you  
Zdenek
