Hi,
(we are new to root)
We are working on getting a large number of strings called “creatorProcesses” to be printed to screen from our root macro file. Currently with the .root file that the values are written to we are able to see that the correct strings are in the tree, but when we try and “cout” using tree->GetEntry(i), we are getting numbers printed to screen. What is the appropriate function call to be able to do this from the macro?
This looks very similar to some of the (many) ROOT tutorials, for example this one. Can you try and compare with what you do? And if you still don’t find the issue, please post your code and your data (or a subset of it), and not a screenshot, it is not really convenient…
#include <string>
#include <TRandom3.h>
#include <RtypesCore.h>
/*#include <fstream>
#include <iomanip>
#include <cmath>
#include <random>
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/G4AutoLock.hh"
#include "/Users/fritschlab/Desktop/Dossier/GREG_detector/include/Analysis.hh"
//#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/globals.hh"
#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/G4SystemOfUnits.hh"
#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/G4Track.hh"
#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/G4ios.hh"
#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/G4SteppingManager.hh"
#include "/opt/local/include/Geant4/Geant4.10.6/Geant4/G4ThreeVector.hh"*/
void rootAnalysis()
{
//Creator Proccesses are able to be written to root file but not able to read out correctly from branch
Char_t creatorProcesses[100];
FILE *fp = fopen("GREG_12_11_1_3.000000default.txt","r" ); //need to change this to automate which file is opened to write creator processes to!!!
char line[81];
// create a new ROOT file
TFile *e = new TFile("experiment.root","RECREATE");[GREG_12_11_1_3.000000default.txt|attachment](upload://26I03uP2pGAGeLUXBFhjRALIr5B.txt) (15.3 KB)
// create a TTree
TTree *tree = new TTree("T","creatorProcessesBranch");
// create one branch with all information from the stucture
tree->Branch("creatorProcesses", creatorProcesses, "creatorProcesses/C");
// fill the tree from the values in ASCII file
while (fgets(line,80,fp)) {
sscanf(&line[0],"%s",&creatorProcesses);
tree->Fill();
//cout << line << endl;
}
//check what the tree looks like
fclose(fp);
e->Write();
for(int i = 0; i < tree->GetEntries(); i++) {
cout << tree->GetEntry(i) << endl;
}
}
Previously the file I attached was a .out but I changed it to a .txt in the code and the name so that I could attach it here! Let me know if you think of anything!
void rootAnalysis()
{
//Creator Proccesses are able to be written to root file but not able to read out correctly from branch
Char_t creatorProcesses[100];
FILE *fp = fopen("GREG_12_11_1_3.000000default.txt", "r"); //need to change this to automate which file is opened to write creator processes to!!!
char line[81];
// create a new ROOT file
TFile *e = new TFile("experiment.root","RECREATE");
// create a TTree
TTree *tree = new TTree("T","creatorProcessesBranch");
// create one branch with all information from the stucture
tree->Branch("creatorProcesses", creatorProcesses, "creatorProcesses/C");
// fill the tree from the values in ASCII file
while (fgets(line,80,fp)) {
sscanf(&line[0], "%s", creatorProcesses);
tree->Fill();
}
//check what the tree looks like
fclose(fp);
e->Write();
Char_t creatorProcessesRead[100];
tree->SetBranchAddress("creatorProcesses", &creatorProcessesRead);
cout << "************ Reading the tree ************" << endl;
for (Long64_t i = 0; i < tree->GetEntries(); i++) {
tree->GetEntry(i);
cout << creatorProcessesRead << endl;
}
}
And if you read the documentation of TTree::GetEntry(), you’ll see that tree->GetEntry(i); returns the number of bytes read…