First an apology for asking about this (probably fairly trivial) issue, I’ve spent the last 2 days chipping away at this, but having reached the point where the macro executes without any errors but gives wildly unexpected values I thought I’d ask for help.
Anyway, my code reads in threshold scan data for many pixels on an ITS chip, and stores it in a tree containing integers (row, column, chipID, Npoints, etc…), floats (threshold, response) and integer arrays (for pulse height (going from 100->000), and hits (the corresponding pixel response to a pulse of size pulseheight). As far as I can tell, this works fine (TTree::Print() displays the correct number of entries and sensible looking data sizes etc…).
My problems occur when trying to read the “PlsH” and “Hits” arrays from this tree, such that these data can be used in a fitting process in order to extract the pixel’s threshold (and then fill that respective branch), I have used a (what seems to be from my research) a fairly standard means: create new TBranch objects and then use tree->GetBranch() with these to read the arrays into ‘intermediate’ arrays. When I print these arrays in a loop, however, I get values which are neither my data or hex memory addresses.
Full macro code:
#include <fstream>
#include <iostream>
#include "TFile.h"
#include "TTree.h"
#if defined(__CINT__) && !defined(__MAKECINT__)
#include "..\test\libEvent.so"
#else
#include "..\test\Event.h"
#endif
//setup data structure
void ReadScanFile(){
//declare data type variables
Int_t ScanPoints = 100;
const Int_t Npoints=101;
Int_t ChipID;
Int_t Row;
Int_t Col;
Int_t PlsH[Npoints];
Int_t Hits[Npoints];
Float_t Threshold;
Float_t Response;
//instatiate TFile and TTree
TFile* f = new TFile("thresholdscan.root", "RECREATE","ROOT file for all pixels in pALPIDE-3 threshold scan");
TTree* tree = new TTree("chip","");
//initialise branches
tree->Branch("Npoints",&Npoints,"Npoints/I");
tree->Branch("ChipID",&ChipID,"ChipID/I");
tree->Branch("Row",&Row,"Row/I");
tree->Branch("Col",&Col,"Col/I");
tree->Branch("ChipID",&ChipID,"ChipID/I");
tree->Branch("PlsH",PlsH,"PlsH[Npoints]/I");
tree->Branch("Hits",Hits,"Hits[Npoints]/I");
tree->Branch("Threshold",&Threshold,"Threshold/F");
tree->Branch("Response",&Response,"Response/F");
//reading reference variables
int linecount=1; //must be 1 so file splits correctly
int arrayindex=0;
//open .dat file in filestream
ifstream file;
file.open("testshort.txt");
if (file.is_open())
{
// Body info.
while (file.good()) // read the file until its end.
{
string line0;
getline (file,line0);
char * cstr, * p;
cstr = new char [line0.size()+1];
strcpy (cstr, line0.c_str());
//split line by spaces and assign to variables
p=strtok (cstr," ");
if(p==NULL)break;
PlsH[arrayindex] = atoi(p);
p=strtok (NULL," ");
Hits[arrayindex] = atoi(p);
p=strtok (NULL," ");
ChipID = atoi(p);
p=strtok (NULL," ");
Row = atoi(p);
p=strtok (NULL," ");
Col = atoi(p);
arrayindex++;
//write and split to next pixel if line is multiple of 101
if (linecount%101==0){
tree->Fill(); //plsH,hits,chipID,row,col
cout << "pixel " << linecount/101 <<" ends at line: "<<linecount << endl;
arrayindex=0; //reset arrayindex (start filling new entry)
}
linecount++;
}
file.close();
tree->Print();
Int_t entries = tree->GetEntries();
cout<<entries<<" entries in tree"<<endl;
//extract arrays from tree for fitting
Int_t *hit[Npoints] ,*pls [Npoints] ;//= new Int_t [101];
TBranch *Thit=tree->GetBranch("Hits");
TBranch *Tpls=tree->GetBranch("PlsH");
Thit->SetAddress(hit); //is & pointer needed here
Tpls->SetAddress(pls); //for array access from tree
//(seems to crash root w/ &)
pls = Tpls->GetEntry(8); //fill intermediate arrays
hit = Thit->GetEntry(8); //with data for pixel 8
//read
for(int j=0;j<=100;j++){
int x=Tpls[j];
int y=Thit[j];
cout<< x <<" "<< y<<" " <<endl;
}
}
}
data file for 10 pixels:
testshort.txt (13.1 KB)
Again apologies if my mistakes/ignorance are painfully obvious, my computer science knowlegde is fairly limited, and my only experience with c++ is learning root!
Many thanks in advance.