Reading and Writing Tree with Array Variables

Hello everybody.

I am new here at ROOT Talk so I better introduce myself first.
I am a physics student in particle physics and I have been learning and trying to work
with ROOT for the last few weeks. The problem that I have now concerns
the reading and writing of a tree. I looked through old ROOT Talk discussions
and also found a tutorial by Rene Brun, see root.cern.ch/root/html/tutorials … ee3.C.html
So I basically followed those scripts in order to write the C++ file (correcttree.c) that I
need for my student project.

The thing is that I have a nice tree, called T2, which has a variable, say MySignal,
which gives some signal measured in some detector. The signal has also a
reconstructed position in the detector, whose x and y coordinates are stored
in two other variables in the same tree, say MyPos[0] and MyPos[1] respectively.
MySignal actually is a vector of floats, as it gives several signal “candidates” which are
peaks of the waveform I think. However, this is not important. It is important, that
MySignal is a vector (of about 10 entries), hence I should write MySignal[i], and therefore
MyPos is a 2d vector, MyPos[i][0] is the x position of signal peak i, and MyPos[i][1]
is the y position of signal peak i.

What I now wanna do is to add a correction map to the signal. I have been given a
2d array with the name cmap[i][j], which contains a correction factor for every
xy-position. Cmap is actually binned, which is why I use i and j, so in x-bin i and y-bin j
there is a correction factor for the element in MySignal[k] which is located at the
position. You can see in the code below that I read cmap out of a histogram.
The goal is to create a new vector, NewSignal, which contains the
values of MySignal multiplied with the proper correction factor. Then, NewSignal
shall be stored in a new tree, say T2c, and saved in a new ROOT file.

As I mentioned before I adopted the code from the tutorial and added the reading
tree stuff from several sources I could find. Now, everything is working fine, the tree
is correctly read and the new signal values are correctly calculated. Also
I do not get an error when I let the C++ macro run via root .x correcttree.c
However, the new file which should contain NewSignal is empty :frowning:
So, there is something not working correctly. But I do not have a clue on what
is wrong, neither does my supervisor. Do you have any idea?

Thank you in advance,
Heico

#include
#include “vector”
#include “TTree.h”
#include “TFile.h”
#include “TBranch.h”
#include “TBranchElement.h”
#include “TROOT.h”
#include “TObject.h”
#include “TMath.h”
#include “TRandom.h”
#include “TChain.h”
#include “TH2.h”

void correcttree() {

// Basic Variables

int RunNumber = 10; 
char *OldRootFile = "filenumber";
char *OldRootFolder = "/oldfolder";
char *NewRootFolder = "/newfolder";



// Load Correction Map

char cmfilename[500];
sprintf(cmfilename, "%s/corrmap/weighthist_run%d.root", NewRootFolder, RunNumber);
TFile *cmfile = new TFile(cmfilename, "READ");
TH2F *cmaphist = (TH2F*)cmfile->Get("weighthist");

int const xdim = 16; 
int const ydim = 16; 
float cmap[16][16];

float dx = 320.0f/xdim;
float dy = 320.0f/ydim;

for(int i=0; i<xdim; ++i)
    for(int j=0; j<ydim; ++j)
        cmap[i][j] = cmaphist->GetBinContent(i,j);



// Load Old Tree

char oldfilename[500], newfilename[500];
sprintf(oldfilename, "%s/%s.root", OldRootFolder, OldRootFile);
sprintf(newfilename, "%s/corrected/run_%d/%sc.root", NewRootFolder, RunNumber, OldRootFile);

TChain *T2 = new TChain("T2");
T2->Add(oldfilename);

vector<float> * MySignal;
vector<vector<float> > * MyPos;

T2->SetBranchStatus("*", 0);
T2->SetBranchStatus("MySignal", 1);
T2->SetBranchStatus("MyPos", 1);
T2->SetBranchAddress("MySignal ", &MySignal);
T2->SetBranchAddress("MyPos ", &MyPos);

int num = T2->GetEntries();



// Correct Tree and Save in New File

TFile NewFile(newfilename, "RECREATE");
TTree *T2c = new TTree("T2c", "T2c");

const Int_t num_peaks_max = 10;
Int_t num_peaks;
Float_t NewSignal[num_peaks_max];
T2c->Branch("num_peaks", &num_peaks, "num_peaks/I");
T2c->Branch("NewSignal ", NewSignal, " NewSignal[num_peaks]/F");
int xnum = 0, ynum = 0;


for(int i=0; i<num; ++i) { // correction for every event
    T2->GetEntry(i);
    num_peaks = sizeof(MySignal);
    for(int j=0; j<num_peaks; ++j) { // correction for every peak in MySignal per event
    xnum = (int) (160+(*MyPos)[0][0])/dx; // just two code lines to get the bin number of the position
    ynum = (int) (160+(* MyPos)[0][1])/dy; 
    if(xnum>=xdim || ynum>=ydim) continue;
        NewSignal[j] = (*MySignal)[j]*cmap[xnum][ynum]; // here we do the correction
    std::cout << "entry " << i << "." << j << " with result: " << NewSignal[j] << std::endl; // here I get the correct values of NewSignal
    }
    T2c->Fill();
}

T2c->Print(); // here it also says that there are enough entries in the tree
NewFile.cd();
T2c->Write();

}

Hello.

Hm okay, this is awkward now :slight_smile:
After having tested and corrected my making-the-tree-script so often I forgot to update my read-the-tree-script (during correction I obviously renamed some things but forgot to do so in the read-script)
It does seem fine now. And the file is written properly.
Sorry for bothering you.

heico