How to loop over two different TTrees at the same time

Hello co-rooters!

I am dealing with the following issue : There is a root file with 2 TTrees. These two trees don’t have the same number of entries, but they have the same TBranches.

The TTrees have the following structure

TTree1

Row Branch1 Branch2 Branch3 1 1 0.2 1.45 2 1 0.2 1.58 3 1 0.2 1.89 4 2 0.1 1.56 5 2 0.1 1.39 6 3 0.15 1.40 7 4 0.2 1.69 ...

TTree2

Row Branch1 Branch2 Branch3 1 1 0.17 1.49 2 2 0.22 1.87 3 3 0.32 2.31 4 4 0.15 2.34 ...
What I want to do is to plot the correlation between the brunches TBranch2.

The question is how to loop over both and fill the 2D histogram? Obviously I can’t use

for (int i=0; i<TTree1->GetEntries(); i++){ TTree1->GetEntry(i);// or TTree2->GetEntry(i);??? }

so how can this be done?

Thanks a lot in advance!

Hi,

I don’t yet understand what you want to correlate. Given that they have different numbers of entries you probably don’t want a correlation per entry, right? Rather, you want to get the correlation for the distributions of the same variables for the two trees?

For the former it would make sense to plot a 2D histogram, but then I don’t understand the different number of entries. For the latter you can use TTree::Draw() and any histogram correlation tool (e.g. TH1::KolmogorovTest() etc)

Cheers, Axel.

Changed the initial description! I hope it’s more clear now!
Thank you very much for your time!

Hi,

No, not clearer, this doesn’t address the questions I asked. You seem to want to do a per-entry correlation - but then you seem to not know how to address the fact that you have different numbers of entries. I cannot answer that for you. Do you just want to ignore the last entries in the larger tree?

Cheers, Axel.

No!

If you see they share the same TBranch1, although in TTree1 you have more entries.
So what I want to do is correlate TBranch2 when the TBranch1 changes value!

for instance

0.2 with 0.17
0.1 with 0.22
0.15 with 0.32
0.2 with 0.15

Attach both root files.

Hi,

Ha! Now I understand! That’s a weird tree. I’d recommend to generate it “properly”. That said, you can use e.g. a separate TTreeReader for each tree, read the branch1 into two different TTreeReaderValues:

TH1* correlation = ...
TTreeReader trMore(treeWithMoreEntries);
TTreeReaderValue<int> more1(trMore, "branch1");
TTreeReaderValue<double> more2(trMore, "branch2");
TTreeReader trLess(treeWithLessEntries);
TTreeReaderValue<int> less1(trLess, "branch1");
TTreeReaderValue<double> less1(trLess, "branch2");
while (trLess.Next()) {
  // Find the first entry in trMore that has the same value in branch1:
  while (trMore.Next() && *more1 != *less1) {}
  correlation->Fill(*more2, *more1);
}

Warning: I didn’t compile nor test this!

Cheers, Axel.

It’s only one root file, that has some TBranches.
I want to correlate the tflash of PKUP and tflash of FIMG for the same BunchNumbers.

The file is too large to attach do I give a dropbox link

dropbox.com/s/0z1vmqri129bh … .root?dl=0

[quote=“Axel”]Hi,

Ha! Now I understand! That’s a weird tree. I’d recommend to generate it “properly”. That said, you can use e.g. a separate TTreeReader for each tree, read the branch1 into two different TTreeReaderValues:

TH1* correlation = ...
TTreeReader trMore(treeWithMoreEntries);
TTreeReaderValue<int> more1(trMore, "branch1");
TTreeReaderValue<double> more2(trMore, "branch2");
TTreeReader trLess(treeWithLessEntries);
TTreeReaderValue<int> less1(trLess, "branch1");
TTreeReaderValue<double> less1(trLess, "branch2");
while (trLess.Next()) {
  // Find the first entry in trMore that has the same value in branch1:
  while (trMore.Next() && *more1 != *less1) {}
  correlation->Fill(*more2, *more1);
}

Warning: I didn’t compile nor test this!

Cheers, Axel.[/quote]

I am trying to run your code but I can’t!
I suspect that the main thing is that I am running root v5.32. Could that be correct?

Hi,

Yes, correct. 5.32 is very old. You’ll have to do the same using TBranch->SetAddress().

Cheers, Axel.

Since I am running on lxpus I could switch to root6. The thing is that I am drawing the histogram, but it’s empty…

My code is the following

[code]#include “header_c.h”
#include “header_root.h”
#include “TTreeReader.h”
#include “TTreeReaderValue.h”
#include “header_global_variables.h”
#include “header_functions.h”

int PvsF(int run){

// (1) Define isolethargic Binning
int min = 0;// 1e0 ns
int max = 7;// 1e7 ns
int bpd = 100; // bins per decade
int ndec = max - min;// number of decades
int nbins = (int) ndec*bpd;// number of bins
double step = (double) ndec / nbins;
double tbins[nbins+1];

for(int i=0; i <= nbins; i++) {
tbins[i] = (float) pow(10., step * (double) i + min);
}

// (2) Define histograms
// PKUP vs FIMG
TH2F *htflashPKUPvsFIMG2 = new TH2F(“htflashPKUPvsFIMG2”,“PKUP vs. tflash 238U; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG3 = new TH2F(“htflashPKUPvsFIMG3”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG4 = new TH2F(“htflashPKUPvsFIMG4”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG5 = new TH2F(“htflashPKUPvsFIMG5”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG6 = new TH2F(“htflashPKUPvsFIMG6”,“PKUP vs. tflash 235U; time (ns); time (ns);”,nbins, tbins, nbins, tbins);

TFile *foo = TFile::Open(TString::Format(“run_%d_in.root”, run));

TTree TFIMG = (TTree)foo->Get(“FIMG”); TTree TPKUP = (TTree)foo->Get(“PKUP”);
int entriesFIMG = TFIMG->GetEntries(); int entriesPKUP = TPKUP->GetEntries();
TFIMG->SetBranchStatus("",0); TPKUP->SetBranchStatus("",0);

// variables for reading the TTrees
double tflash;
int BunchNumber, detn;

// variables for the analysis
double tflashFIMG, tflashPKUP;
//double tflash2, tflash3, tflash4, tflash5, tflash6;

// FIMG
TFIMG->SetBranchStatus(“tflash”,1); TFIMG->SetBranchAddress(“tflash”,&tflashFIMG);
//TFIMG->SetBranchStatus(“BunchNumber”,1); TFIMG->SetBranchAddress(“BunchNumberFIMG”,&BunchNumber);

// PKUP
TPKUP->SetBranchStatus(“tflash”,1); TPKUP->SetBranchAddress(“tflash”,&tflashPKUP);
//TPKUP->SetBranchStatus(“BunchNumber”,1); TPKUP->SetBranchAddress(“BunchNumberPKUP”,&BunchNumber);

// Usefull variables
int BunchNumber_old;

//*** TEST with TTreeReader ***
TTreeReader trFIMG(TFIMG);
TTreeReaderValue BunchNumberFIMG(trFIMG, “BunchNumber”);
TTreeReaderValue tFlashFIMG(trFIMG, “tflash”);

TTreeReader trPKUP(TPKUP);
TTreeReaderValue BunchNumberPKUP(trPKUP, “BunchNumber”);
TTreeReaderValue tFlashPKUP(trPKUP, “tflash”);

while (trPKUP.Next()) {
cout << “1”;
// Find the first entry in trMore that has the same value in branch1:
while (trFIMG.Next() && *BunchNumberFIMG != *BunchNumberPKUP) {
htflashPKUPvsFIMG6->Fill(*tFlashPKUP, *tFlashFIMG);
}
}

htflashPKUPvsFIMG6->Draw();

return 0;

}[/code]

{ // ... ROOT 6 is required ... // TH2D *h = new TH2D("h", "correlation;PKUP tflash;FIMG tflash", ...); TFile *f = TFile::Open("run200919.root"); TTreeReader trLess("PKUP"); TTreeReaderValue<int> less1(trLess, "BunchNumber"); TTreeReaderValue<double> less2(trLess, "tflash"); TTreeReader trMore("FIMG"); TTreeReaderValue<int> more1(trMore, "BunchNumber"); TTreeReaderValue<double> more2(trMore, "tflash"); trMore.Next(); // "load" the first entry while (trLess.Next()) { // loop over all entries while (*less1 == *more1) { std::cout << *less1 << " " << *less2 << " " << *more2 << std::endl; // h->Fill(*less2, *more2); if (!(trMore.Next())) break; // "load" the next entry } } delete f; }

Thanks, Pepe!

Something is wrong in that.
Although less2 and more2 seems to be printed more or less correctly, less1 is always 0, although in the file it is 1, 2, 3, 4, 5, … (in the file it’s the variable BunchNumber) which is also the case with variable detnFIMG. Is there something evidently wrong?

My code is the following

[code]TFile *foo = TFile::Open(TString::Format(“run_%d_in.root”, run));

TTree TFIMG = (TTree)foo->Get(“FIMG”); TTree TPKUP = (TTree)foo->Get(“PKUP”);

//*** TEST with TTreeReader ***
TTreeReader trFIMG(TFIMG);
TTreeReaderValue BunchNumberFIMG(trFIMG, “BunchNumber”);
TTreeReaderValue detnFIMG(trFIMG, “detn”);
TTreeReaderValue tFlashFIMG(trFIMG, “tflash”);

TTreeReader trPKUP(TPKUP);
TTreeReaderValue BunchNumberPKUP(trPKUP, “BunchNumber”);
TTreeReaderValue tFlashPKUP(trPKUP, “tflash”);

trFIMG.Next(); // Load the first entry
while (trPKUP.Next()) { // loop over all entries
cout << "detn = " << *detnFIMG << endl;
while (*BunchNumberPKUP == *BunchNumberFIMG) {
std::cout << *BunchNumberPKUP << " " << *tFlashPKUP << " " << *tFlashFIMG << " " << *detnFIMG << std::endl;
htflashPKUPvsFIMG6->Fill(*tFlashPKUP, *tFlashFIMG);
if (!(trFIMG.Next())) break; // “load” the next entry
}
}[/code]

Thanks in advance!

Using your new macro with your “run200919.root” file, I get: detn = 5 1 9705.34 8144.54 5 1 9705.34 8144.54 5 1 9705.34 8144.54 5 1 9705.34 8141.16 6 1 9705.34 8141.16 6 (...) 2059 9703.32 8145.16 3 2059 9703.32 8145.16 3 2059 9703.32 8154.92 4 2059 9703.32 8154.92 4 2059 9703.32 8154.92 4 2059 9703.32 8154.92 4

Strange…
What I get is the following…

detn = 0 0 9704.58 8154.92 0 detn = 0 0 9703.9 8154.92 0 detn = 0 0 9704.35 8154.92 0 detn = 0 0 9704.18 8154.92 0 detn = 0 0 9704.87 8154.92 0 detn = 0 0 9704.81 8154.92 0 detn = 0 0 9703.86 8154.92 0 detn = 0 0 9704.13 8154.92 0 detn = 0 0 9703.77 8154.92 0 detn = 0 0 9704.93 8154.92 0 detn = 0 0 9704.17 8154.92 0 detn = 0 0 9704.65 8154.92 0 detn = 0 0 9705.29 8154.92 0 detn = 0 0 9703.69 8154.92 0 detn = 0 0 9703.19 8154.92 0 detn = 0 0 9702.54 8154.92 0 detn = 0 0 9705.32 8154.92 0 detn = 0 0 9704.55 8154.92 0 detn = 0 0 9703.32 8154.92 0

Notice that the first and last columns are always 0 and that the 3rd one is always the same number…

My full code is the following

[code]#include “header_c.h”
#include “header_root.h”
#include “TTreeReader.h”
#include “TTreeReaderValue.h”
#include “header_global_variables.h”
#include “header_functions.h”

/*************************

  1. Load .L Deltat.C++
  2. Run Delta(runNumber)
    *************************/

/** To use ROOT6 run these commands and then execute the macro using root -b -q extrema_root_forum.C

source /afs/cern.ch/sw/lcg/external/gcc/4.8/x86_64-slc6/setup.csh
source /afs/cern.ch/sw/lcg/app/releases/ROOT/6.02.02/x86_64-slc6-gcc48-opt/root/bin/thisroot.csh
*/

int PvsF(int run){

// (1) Define isolethargic Binning
int min = 0;// 1e0 ns
int max = 8;// 1e7 ns
int bpd = 100; // bins per decade
int ndec = max - min;// number of decades
int nbins = (int) ndec*bpd;// number of bins
double step = (double) ndec / nbins;
double tbins[nbins+1];

for(int i=0; i <= nbins; i++) {
tbins[i] = (float) pow(10., step * (double) i + min);
}

// (2) Define histograms
//FIMG
//TH1F *htflash2 = new TH1F(“htflash1”,“Tflash Det. #2 - 238U ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash3 = new TH1F(“htflash2”,“Tflash Det. #3 - 240Pu ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash4 = new TH1F(“htflash3”,“Tflash Det. #4 - 240Pu ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash5 = new TH1F(“htflash4”,“Tflash Det. #5 - 240Pu ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash6 = new TH1F(“htflash5”,“Tflash Det. #6 - 235U ; Time (ns); Counts”,nbins, tbins);

//PKUP
//TH1F *htflashPKUP = new TH1F(“htflashPKUP”,“Tflash - PKUP ; Time (ns); Counts”,nbins, tbins);
TH1F *htflashPKUP = new TH1F(“htflashPKUP”,“Tflash - PKUP ; Time (ns); Counts”, 1.6e7, 0, 1.6e7);

// PKUP vs FIMG
TH2F *htflashPKUPvsFIMG2 = new TH2F(“htflashPKUPvsFIMG2”,“PKUP vs. tflash 238U; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG3 = new TH2F(“htflashPKUPvsFIMG3”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG4 = new TH2F(“htflashPKUPvsFIMG4”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG5 = new TH2F(“htflashPKUPvsFIMG5”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG6 = new TH2F(“htflashPKUPvsFIMG6”,“PKUP vs. tflash 235U; time (ns); time (ns);”,nbins, tbins, nbins, tbins);

//TFile *foo = TFile::Open(TString::Format(“run_%d_in.root”, run));
TFile *foo = TFile::Open(TString::Format(“run%d.root”, run));

TTree TFIMG = (TTree)foo->Get(“FIMG”); TTree TPKUP = (TTree)foo->Get(“PKUP”);
int entriesFIMG = TFIMG->GetEntries(); int entriesPKUP = TPKUP->GetEntries();
TFIMG->SetBranchStatus("",0); TPKUP->SetBranchStatus("",0);

// variables for reading the TTrees
double tflash;
int BunchNumber;//, detn;

// variables for the analysis
double tflashFIMG, tflashPKUP;
//double tflash2, tflash3, tflash4, tflash5, tflash6;

// FIMG
TFIMG->SetBranchStatus(“tflash”,1); TFIMG->SetBranchAddress(“tflash”,&tflashFIMG);
//TFIMG->SetBranchStatus(“BunchNumber”,1); TFIMG->SetBranchAddress(“BunchNumberFIMG”,&BunchNumber);

// PKUP
TPKUP->SetBranchStatus(“tflash”,1); TPKUP->SetBranchAddress(“tflash”,&tflashPKUP);
//TPKUP->SetBranchStatus(“BunchNumber”,1); TPKUP->SetBranchAddress(“BunchNumberPKUP”,&BunchNumber);

// Usefull variables
int BunchNumber_old;

//*** TEST with TTreeReader ***
TTreeReader trFIMG(TFIMG);
TTreeReaderValue BunchNumberFIMG(trFIMG, “BunchNumber”);
TTreeReaderValue detnFIMG(trFIMG, “detn”);
TTreeReaderValue tFlashFIMG(trFIMG, “tflash”);

TTreeReader trPKUP(TPKUP);
TTreeReaderValue BunchNumberPKUP(trPKUP, “BunchNumber”);
TTreeReaderValue tFlashPKUP(trPKUP, “tflash”);

trFIMG.Next(); // Load the first entry
//cout << *detnFIMG << " " ;
while (trPKUP.Next()) { // loop over all entries
//cout << "detn = " << *detnFIMG << endl;
while (*BunchNumberPKUP == *BunchNumberFIMG) {
std::cout << *BunchNumberPKUP << " " << *tFlashPKUP << " " << *tFlashFIMG << " " << *detnFIMG << std::endl;
htflashPKUPvsFIMG6->Fill(*tFlashPKUP, *tFlashFIMG);
if (!(trFIMG.Next())) break; // “load” the next entry
}
}

//htflashPKUP->Draw(“histo”);
htflashPKUPvsFIMG6->Draw(“colz”);

// *** END of TEST with TTreeReader

return 0;

}[/code]

I think i got it running…
But I don’t know what was the problem…
I just commented out all the command that had to do with SetBranchAddress…

At the moment I don’t know how to apply a certain selection, in the variable detn let’s say… I want only the enties that have detn == 6, for instance but I can’t seem to be able to do it…

If I use an if(*detnFIGM==6) statement right after trFIMG.Next(), I get no entries.

If I use a second condition in the BunchNumber while loop (&& *detnFIMG == 6), I still get 0 entries.

And if I use an if statement, right after the while loop (in the BunchNumber) it is running forever…

Any idea on that, because I very puzzled on how the TTReader and .Next() is working… My code is the following

[code]#include “header_c.h”
#include “header_root.h”
#include “TTreeReader.h”
#include “TTreeReaderValue.h”
#include “header_global_variables.h”
#include “header_functions.h”

/*************************

  1. Load .L Deltat.C++
  2. Run Delta(runNumber)
    *************************/

/** To use ROOT6 run these commands and then execute the macro using root -b -q extrema_root_forum.C

source /afs/cern.ch/sw/lcg/external/gcc/4.8/x86_64-slc6/setup.csh
source /afs/cern.ch/sw/lcg/app/releases/ROOT/6.02.02/x86_64-slc6-gcc48-opt/root/bin/thisroot.csh
*/

int PvsF(int run){

// (1) Define isolethargic Binning
int min = 0;// 1e0 ns
int max = 8;// 1e7 ns
int bpd = 100; // bins per decade
int ndec = max - min;// number of decades
int nbins = (int) ndec*bpd;// number of bins
double step = (double) ndec / nbins;
double tbins[nbins+1];

for(int i=0; i <= nbins; i++) {
tbins[i] = (float) pow(10., step * (double) i + min);
}

// (2) Define histograms
//FIMG
//TH1F *htflash2 = new TH1F(“htflash1”,“Tflash Det. #2 - 238U ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash3 = new TH1F(“htflash2”,“Tflash Det. #3 - 240Pu ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash4 = new TH1F(“htflash3”,“Tflash Det. #4 - 240Pu ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash5 = new TH1F(“htflash4”,“Tflash Det. #5 - 240Pu ; Time (ns); Counts”,nbins, tbins);
//TH1F *htflash6 = new TH1F(“htflash5”,“Tflash Det. #6 - 235U ; Time (ns); Counts”,nbins, tbins);

//PKUP
//TH1F *htflashPKUP = new TH1F(“htflashPKUP”,“Tflash - PKUP ; Time (ns); Counts”,nbins, tbins);
TH1F *htflashPKUP = new TH1F(“htflashPKUP”,“Tflash - PKUP ; Time (ns); Counts”, 1.6e7, 0, 1.6e7);

// PKUP vs FIMG
TH2F *htflashPKUPvsFIMG2 = new TH2F(“htflashPKUPvsFIMG2”,“PKUP vs. tflash 238U; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG3 = new TH2F(“htflashPKUPvsFIMG3”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG4 = new TH2F(“htflashPKUPvsFIMG4”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG5 = new TH2F(“htflashPKUPvsFIMG5”,“PKUP vs. tflash 240Pu; time (ns); time (ns);”,nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG6 = new TH2F(“htflashPKUPvsFIMG6”,“PKUP vs. tflash 235U; time (ns); time (ns);”,nbins, tbins, nbins, tbins);

//TFile *foo = TFile::Open(TString::Format(“run_%d_in.root”, run));
TFile *foo = TFile::Open(TString::Format(“run%d.root”, run));

//TTree TFIMG = (TTree)foo->Get(“FIMG”); TTree TPKUP = (TTree)foo->Get(“PKUP”);
//int entriesFIMG = TFIMG->GetEntries(); int entriesPKUP = TPKUP->GetEntries();
//TFIMG->SetBranchStatus("",0); TPKUP->SetBranchStatus("",0);

// variables for reading the TTrees
//double tflash;
//int BunchNumber;//, detn;

// variables for the analysis
//double tflashFIMG, tflashPKUP;
//double tflash2, tflash3, tflash4, tflash5, tflash6;

// FIMG
//TFIMG->SetBranchStatus(“tflash”,1); TFIMG->SetBranchAddress(“tflash”,&tflashFIMG);
//TFIMG->SetBranchStatus(“BunchNumber”,1); TFIMG->SetBranchAddress(“BunchNumberFIMG”,&BunchNumber);

// PKUP
//TPKUP->SetBranchStatus(“tflash”,1); TPKUP->SetBranchAddress(“tflash”,&tflashPKUP);
//TPKUP->SetBranchStatus(“BunchNumber”,1); TPKUP->SetBranchAddress(“BunchNumberPKUP”,&BunchNumber);

// Usefull variables
//int BunchNumber_old;

//*** TEST with TTreeReader ***
TTreeReader trFIMG(“FIMG”);
TTreeReaderValue BunchNumberFIMG(trFIMG, “BunchNumber”);
TTreeReaderValue detnFIMG(trFIMG, “detn”);
TTreeReaderValue tFlashFIMG(trFIMG, “tflash”);

TTreeReader trPKUP(“PKUP”);
TTreeReaderValue BunchNumberPKUP(trPKUP, “BunchNumber”);
TTreeReaderValue tFlashPKUP(trPKUP, “tflash”);

trFIMG.Next(); // “Load” the first entry
cout << *detnFIMG << " " ;
//if (*detnFIMG==6){
while (trPKUP.Next()) { // loop over all entries
//cout << "detn = " << *detnFIMG << endl;
while (*BunchNumberPKUP == *BunchNumberFIMG) {//&& *detnFIMG == 6
//if(*detnFIMG == 6){
//std::cout << *BunchNumberPKUP << " " << *tFlashPKUP << " " << *tFlashFIMG << " " << *detnFIMG << std::endl;
htflashPKUPvsFIMG6->Fill(*tFlashPKUP, *tFlashFIMG);
if (!(trFIMG.Next())) break; // “load” the next entry
//}
}
}
//}
//delete foo;

//htflashPKUP->Draw(“histo”);
htflashPKUPvsFIMG6->Draw(“colz”);

// *** END of TEST with TTreeReader
return 0;

}[/code]

[code]#include “TFile.h”
#include “TTree.h”
#include “TH1.h”
#include “TH2.h”
#include “TTreeReader.h”
#include “TTreeReaderValue.h”

#include
#include

int PvsF(const int run = 200919) {
// (1) Define isolethargic Binning
const int min = 3; // 1e3 ns
const int max = 5; // 1e5 ns
const int bpd = 10; // bins per decade
const int ndec = max - min; // number of decades
const int nbins = ndec * bpd; // number of bins
const double step = ((double)ndec) / nbins;
double tbins[(nbins + 1)];

for(int i = 0; i <= nbins; i++) {
tbins[i] = std::pow(10.0, step * i + ((double)min));
}

// (2) Define histograms
// FIMG
//TH1F *htflash2 = new TH1F(“htflash1”, “Tflash Det. #2 - 238U ; Time (ns); Counts”, nbins, tbins);
//TH1F *htflash3 = new TH1F(“htflash2”, “Tflash Det. #3 - 240Pu ; Time (ns); Counts”, nbins, tbins);
//TH1F *htflash4 = new TH1F(“htflash3”, “Tflash Det. #4 - 240Pu ; Time (ns); Counts”, nbins, tbins);
//TH1F *htflash5 = new TH1F(“htflash4”, “Tflash Det. #5 - 240Pu ; Time (ns); Counts”, nbins, tbins);
//TH1F *htflash6 = new TH1F(“htflash5”, “Tflash Det. #6 - 235U ; Time (ns); Counts”, nbins, tbins);

// PKUP
//TH1F *htflashPKUP = new TH1F(“htflashPKUP”, “Tflash - PKUP ; Time (ns); Counts”, nbins, tbins);
TH1F *htflashPKUP = new TH1F(“htflashPKUP”, “Tflash - PKUP ; Time (ns); Counts”, 1.6e7, 0, 1.6e7);

// PKUP vs FIMG
TH2F *htflashPKUPvsFIMG2 = new TH2F(“htflashPKUPvsFIMG2”, “PKUP vs. tflash 238U; time (ns); time (ns);”, nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG3 = new TH2F(“htflashPKUPvsFIMG3”, “PKUP vs. tflash 240Pu; time (ns); time (ns);”, nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG4 = new TH2F(“htflashPKUPvsFIMG4”, “PKUP vs. tflash 240Pu; time (ns); time (ns);”, nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG5 = new TH2F(“htflashPKUPvsFIMG5”, “PKUP vs. tflash 240Pu; time (ns); time (ns);”, nbins, tbins, nbins, tbins);
TH2F *htflashPKUPvsFIMG6 = new TH2F(“htflashPKUPvsFIMG6”, “PKUP vs. tflash 235U; time (ns); time (ns);”, nbins, tbins, nbins, tbins);

// TFile *foo = TFile::Open(TString::Format(“run_%d_in.root”, run));
TFile *foo = TFile::Open(TString::Format(“run%d.root”, run));

TTree *TFIMG; foo->GetObject(“FIMG”, TFIMG);
TTree *TPKUP; foo->GetObject(“PKUP”, TPKUP);

// *** TEST with TTreeReader ***
TTreeReader trFIMG(TFIMG);
TTreeReaderValue BunchNumberFIMG(trFIMG, “BunchNumber”);
TTreeReaderValue detnFIMG(trFIMG, “detn”);
TTreeReaderValue tFlashFIMG(trFIMG, “tflash”);

TTreeReader trPKUP(TPKUP);
TTreeReaderValue BunchNumberPKUP(trPKUP, “BunchNumber”);
TTreeReaderValue tFlashPKUP(trPKUP, “tflash”);

trFIMG.Next(); // Load the first entry
while (trPKUP.Next()) { // loop over all entries
while (*BunchNumberPKUP == *BunchNumberFIMG) {
if (*detnFIMG == 6) {
std::cout << *BunchNumberPKUP << " " << *tFlashPKUP << " " << *tFlashFIMG << " " << *detnFIMG << std::endl;
htflashPKUPvsFIMG6->Fill(*tFlashPKUP, *tFlashFIMG);
}
if (!(trFIMG.Next())) break; // “load” the next entry
}
}
// *** END of TEST with TTreeReader

delete foo; // automatically deletes “TFIMG” and “TPKUP”

//htflashPKUP->Draw(“histo”);
htflashPKUPvsFIMG6->Draw(“colz”);

return 0;
}[/code]
BTW. If you explicitly call:
TFIMG->SetBranchStatus("",0); TPKUP->SetBranchStatus("",0);
then why do you afterwards complain that branches are not read?