Getting String Value From Branch

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?

Thanks so much!!!


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20/04!

Platform: Not Provided
Compiler: Not Provided


Replace Char_t creatorProcesses; by, for example Char_t creatorProcesses[100];

Yeah, we had tried that previously and no luck!

Please show me the code


Here is the code where we are trying to read out from.

Thanks. Note that it would have been simpler to copy & paste the code… Anyway, try:

tree->Branch("creatorProcesses", creatorProcesses, "creatorProcesses/C");

i.e. creatorProcesses instead of &creatorProcesses

Yes, have tried that as well with no luck :frowning:

For every value that is in the tree, the cost << tree->GetEntry(i) line is printing to screen the number 13.

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!

(upload://3gPjRt1fS9dDeV9RxZqBoKKvnN9.root) (5.5 KB)

I need the input (text) file, not the output…

Yes, this is the input text file we read from, it was just named .out from a different code.

GREG_12_11_1_3.000000default.txt (15.3 KB)

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…

Thank you so much, that worked!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.