I have simulated a proton particle track using Geant4dna software. The output file is dna.root - see attach 1.
I can visualise this file with a macro “Plot.C”. I can see a file structure using Root Explorer. But i cannot extract data from this file - see attach 2.
I want to convert file dna.root to a simple txt file like:
N1 X1 Y1 Z1 E1 TYPE1
…
Nn Xn Yn Zn En TYPEn
Where are:
Ni - number of I event
Xi, Yi, Zi - 3D coordinates of particle-water interaction
Ei - deposited energy
TYPEn - type of interaction (ionisation or exitation).
Briefly, I want to convert dna.root to txt or csv file.
input_line_41:2:9: error: expected expression
dump();>dump.txt /* invoking function corresponding to ‘.x’ */
^
input_line_41:2:14: error: member reference base type 'void (const char *, const
char )’ is not a structure or union
dump();>dump.txt / invoking function corresponding to ‘.x’ */
~~^
My question is kind of related to this thread. I have two .root files, one contains a tree having an array of values two of which I need to pull out.
The second contains addition of .root files of good runs. I would like to use the two values from first .root files along with the good runs .root files to plot histogram of invariant mass and pT.
How can I get these two values from the file hadron.root to use?
Same code:
//macro to plot the new mass and pt plots
void plotmass(const char *fname = "newgood.root")// file containing the good runs
{
//std::vector<string> runnumber;
gStyle->SetOptStat
//gDirectory->
// Opening a TFile
TFile *tfile = new TFile(fname,"READ");
// Get TTree tree from TFile
TTree *tree = (TTree*)tfile->Get("tree");
// Get ntval from tree that contains the mass and pt values store in root file hadron.root
Float_t ntval[35];// number of variables in tree equal 35.
TBranch *ntv_branch = tree->GetBranch("ntval");
ntv_branch->SetAddress(&ntval);
// histograms
TH1 *h_mass = new TH1F("h_mass","upc e+/e- mass",10,2.0,4.0);
TH1 *h_pt = new TH1F("h_pt","upc e+/e- pt",40,0.,4.0);
Int_t nevents = tree->GetEntries();
//nevents = 10; // for debugging only, comment this out if you want all events
for (Int_t ievt=0; ievt<nevents; ievt++)
{
tree->GetEvent(ievt);
//cout << ievt << "\t" << ntval[10] << endl;//10 is mass value
//loop over good runs
//int run = ntval[17];
//for( int i =0; i<runnumber.size(); i++){ if (run != runnumber.at(i) continue;)
Float_t mass = ntval[10];
Float_t pt = ntval[11];// pt value
if ( ntval[7]<5 && fabs(ntval[0])<30.0 && ntval[12]==0 )
{
h_mass->Fill( mass );
h_pt->Fill( pt );
}
}
TFile *f=new TFile("testfile.root", "NEW");
TCanvas *C = new TCanvas("C","",850,500);
h_mass->Draw("ehist");
h_mass->Write();
h_pt->Draw("ehist");
h_pt->Write();
In file included from input_line_10:1:
/home/local1/dnaphysics-build/d2.cxx:21:18: error: use of undeclared identifier 'fTree'
((TTreePlayer *)fTree->GetPlayer())->SetScanRedirect(kTRUE)
^
/home/local1/dnaphysics-build/d2.cxx:22:18: error: use of undeclared identifier 'fTree'
((TTreePlayer *)fTree->GetPlayer())->SetScanFileName(outputname);
Thanks for your reply. No i am getting this error:
/home/local1/dnaphysics-build/d2.cxx:21:21: error: no member named 'GetPlayer' in 'TFile'
((TTreePlayer *)f->GetPlayer())->SetScanRedirect(kTRUE)
~ ^
/home/local1/dnaphysics-build/d2.cxx:22:21: error: no member named 'GetPlayer' in 'TFile'
((TTreePlayer *)f->GetPlayer())->SetScanFileName(outputname);
In file included from input_line_10:1:
/home/local1/dnaphysics-build/d2.cxx:22:2: error: called object type 'void' is not a function or function pointer
((TTreePlayer *)t->GetPlayer())->SetScanFileName(outputname);
// Name this file “dump.cxx” and use as:
//
// root [0] .x dump.cxx("dump.txt")
//
// Produces “dump.txt” and “dump.xml” files.
//
void dump(const char *outputname = "dump.txt", const char *fname = "dna.root",
const char *nname = "ntuple")
{
if (!fname || !(*fname) || !nname || !(*nname)) return; // just a precaution
TFile *f = TFile::Open(fname, "READ");
if (!f) return; // just a precaution
TTree *t; f->GetObject(nname, t);
if (!t) { delete f; return; } // just a precaution
t->SetScanField(0);
((TTreePlayer *)t->GetPlayer())->SetScanRedirect(kTRUE)
((TTreePlayer *)t->GetPlayer())->SetScanFileName(outputname);
t->Scan("*");
t->SaveAs("dump.xml");
delete f; // no longer needed (automatically deletes "t")
}
Usage:
$root
root [0] .x d2.cxx("dna.txt")
Result:
In file included from input_line_10:1:
/home/local1/dnaphysics-build/d2.cxx:22:2: error: called object type 'void' is not a function or function pointer
((TTreePlayer *)t->GetPlayer())->SetScanFileName(outputname);
^