How to extract a data from dna.root file?

Good day for everyone! I need help!

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.



dna.root (45 KB)

//
// 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 *fname = "dna.root",
          const char *nname = "ntuple")
{
  if (!fname || !(*fname) || !nname || !(*nname)) return; // just a precaution
  
  TFile *f = TFile::Open(fname, "READ");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  
  TTree *t; f->GetObject(nname, t);
  if (!t) { delete f; return; } // just a precaution
  
  // See:
  // http://root.cern.ch/root/html/TTreePlayer.html#TTreePlayer:Scan
  // http://root.cern.ch/root/html/TTree.html#TTree:Scan
  t->SetScanField(0);
  t->Scan("*");
  
  // See:
  // http://root.cern.ch/root/html/TObject.html#TObject:SaveAs
  t->SaveAs("dump.xml");
  // t->SaveAs(TString::Format("%s.xml", nname));
  
  delete f; // no longer needed (automatically deletes "t")
}
1 Like

Thanks for reply!
I have created dump.cxx in a directory containig dna.root

Execute in terminal and recieve next message:

localhost.localdomain:/dna-b < 91 >root [0] .x dump.cxx(); > dump.txt Badly placed ()'s.

“root [0]” means interactive ROOT command line.

See also:

I am sorry, may be i do something wrong? Now I received this:


Do NOT type "root [0] " yourself (this is the ROOT interactive prompt).

Make familiar with:

[quote=“Wile E. Coyote”]Do NOT type "root [0] " yourself (this is the ROOT interactive prompt).

Do read:
ROOT Primer
ROOT User’s Guide[/quote]
Many, many thanks! Everything is works!

************************************************************************************************************
*    Row   * flagParti * flagProce *         x *         y *         z *      edep *      size *   diffKin *
************************************************************************************************************
*        0 *         2 *        19 *         0 *         0 *         0 *     10.79 * 215439.17 *         0 *
*        1 *         3 *        21 *         0 *         0 * 0.2154391 *     13.39 * 3115361.3 * 14.596182 *
*        2 *         3 *        21 *         0 *         0 * 3.3308005 *     13.39 * 4162531.8 * 14.448767 *
*        3 *         3 *        22 *         0 *         0 * 7.4933323 *         0 * 831263.13 *         0 *
*        4 *         1 *        11 *         0 *         0 * 8.3245955 * 0.5300324 *         0 * 0.5300324 *
*        5 *         1 *        11 *         0 *         0 * 7.4933323 * 1.0587679 *         0 * 1.0587679 *
*        6 *         1 *        11 *         0 *         0 * 3.3308005 * 1.2061821 *         0 * 1.2061821 *
*        7 *         2 *        19 *         0 *         0 * 8.3245955 *     10.79 * 104402.88 *         0 *
*        8 *         3 *        21 *         0 *         0 * 8.4289984 *     13.39 * 589709.29 * 14.626472 *
*        9 *         3 *        21 *         0 *         0 * 9.0187077 *     16.05 * 822017.44 * 18.047898 *
*       10 *         3 *        22 *         0 *         0 * 9.8407251 *         0 * 2492888.8 *         0 *
*       11 *         1 *        11 *         0 *         0 * 12.333614 * 0.5060763 *         0 * 0.5060763 *
*       12 *         1 *        11 *         0 *         0 * 9.8407251 * 1.9978981 *         0 * 1.9978981 *
*       13 *         1 *        11 *         0 *         0 * 9.0187077 * 1.2364721 *         0 * 1.2364721 *
*       14 *         2 *        19 *         0 *         0 * 12.333614 *     10.79 * 70228.643 *         0 *
*       15 *         3 *        21 *         0 *         0 * 12.403842 *     13.39 * 1111412.5 * 15.388224 *
*       16 *         3 *        21 *         0 *         0 * 13.515255 *     16.05 * 241050.94 * 17.721315 *
*       17 *         3 *        21 *         0 *         0 * 13.756306 *     10.79 * 7302.8799 * 11.120379 *
*       18 *         3 *        21 *         0 *         0 * 13.763609 *     13.39 * 238368.93 * 14.158694 *
*       19 *         3 *        21 *         0 *         0 * 14.001978 *     13.39 * 893221.23 * 14.266538 *
*       20 *         3 *        21 *         0 *         0 * 14.895199 *     13.39 * 2021842.9 * 14.553518 *
*       21 *         3 *        22 *         0 *         0 * 16.917042 *         0 * 721422.97 *         0 *
*       22 *         1 *        11 *         0 *         0 * 17.638465 * 0.4524330 *         0 * 0.4524330 *
*       23 *         1 *        11 *         0 *         0 * 16.917042 * 1.1635187 *         0 * 1.1635187 *
*       24 *         1 *        11 *         0 *         0 * 14.895199 * 0.8765386 *         0 * 0.8765386 *
*       25 *         1 *        11 *         0 *         0 * 14.001978 * 0.7686945 *         0 * 0.7686945 *
*       26 *         1 *        11 *         0 *         0 * 13.763609 * 0.3303796 *         0 * 0.3303796 *
*       27 *         1 *        11 *         0 *         0 * 13.756306 * 1.6713156 *         0 * 1.6713156 *
*       28 *         1 *        11 *         0 *         0 * 13.515255 * 1.9982239 *         0 * 1.9982239 *
*       29 *         2 *        19 *         0 *         0 * 17.638465 *     10.79 * 58291.305 *         0 *
*       30 *         3 *        22 *         0 *         0 * 17.696756 *         0 * 2561872.2 *         0 *
*       31 *         1 *        11 *         0 *         0 * 20.258628 * 0.4463142 *         0 * 0.4463142 *
*       32 *         2 *        19 *         0 *         0 * 20.258628 *     10.79 * 209844.09 *         0 *
*       33 *         3 *        20 *         0 *         0 * 20.468472 *     12.91 * 724637.32 *     12.91 *
*       34 *         3 *        22 *         0 *         0 *  21.19311 *         0 * 1901871.4 *         0 *
*       35 *         1 *        11 *         0 *         0 * 23.094981 * 0.4331677 *         0 * 0.4331677 *
*       36 *         2 *        19 *         0 *         0 * 23.094981 *     10.79 * 218167.75 *         0 *
*       37 *         3 *        21 *         0 *         0 * 23.313149 *     13.39 * 107944.07 * 13.464029 *
*       38 *         3 *        21 *         0 *         0 * 23.421093 *     13.39 * 321397.11 * 13.561393 *
*       39 *         3 *        21 *         0 *         0 * 23.742490 *     10.79 * 290393.20 * 12.400589 *
*       40 *         3 *        21 *         0 *         0 * 24.032883 *     10.79 * 611615.49 * 12.394458 *
*       41 *         3 *        21 *         0 *         0 * 24.644499 *     10.79 *  11490785 * 11.501690 *
*       42 *         3 *        21 *         0 *         0 * 36.135284 *     13.39 * 2728386.2 * 14.590160 *
*       43 *         3 *        21 *         0 *         0 * 38.863670 *     10.79 * 5953196.4 * 11.185971 *
*       44 *         3 *        21 *         0 *         0 * 44.816866 *     10.79 * 860503.87 * 10.928753 *
*       45 *         3 *        22 *         0 *         0 * 45.677370 *         0 * 747634.37 *         0 *
*       46 *         1 *        11 *         0 *         0 * 46.425005 * 0.3725830 *         0 * 0.3725830 *
*       47 *         1 *        11 *         0 *         0 * 45.677370 * 0.1387533 *         0 * 0.1387533 *
*       48 *         1 *        11 *         0 *         0 * 44.816866 * 0.3959717 *         0 * 0.3959717 *
*       49 *         1 *        11 *         0 *         0 * 38.863670 * 1.2001601 *         0 * 1.2001601 *
*       50 *         1 *        11 *         0 *         0 * 36.135284 * 0.7116909 *         0 * 0.7116909 *
*       51 *         1 *        11 *         0 *         0 * 24.644499 * 1.6044580 *         0 * 1.6044580 *
*       52 *         1 *        11 *         0 *         0 * 24.032883 * 1.6105898 *         0 * 1.6105898 *
*       53 *         1 *        11 *         0 *         0 * 23.742490 * 0.1713931 *         0 * 0.1713931 *
*       54 *         1 *        11 *         0 *         0 * 23.421093 * 0.0740294 *         0 * 0.0740294 *
*       55 *         2 *        19 *         0 *         0 * 46.425005 *     10.79 * 25229.709 *         0 *
*       56 *         3 *        21 *         0 *         0 * 46.450234 *     13.39 * 939068.59 * 14.458238 *
*       57 *         3 *        21 *         0 *         0 * 47.389303 *     13.39 * 2749950.3 * 13.601135 *
*       58 *         3 *        21 *         0 *         0 * 50.139253 *     10.79 * 4179778.8 * 11.938533 *
*       59 *         3 *        21 *         0 *         0 * 54.319032 *     10.79 * 1104110.3 * 11.990252 *
*       60 *         3 *        21 *         0 *         0 * 55.423142 *     13.39 * 1918698.5 * 14.176235 *
*       61 *         3 *        21 *         0 *         0 * 57.341841 *     10.79 * 1594010.2 * 10.902289 *
*       62 *         3 *        21 *         0 *         0 * 58.935851 *     10.79 * 1770371.4 * 11.413828 *
*       63 *         3 *        20 *         0 *         0 * 60.706223 *     10.13 * 1331088.3 *     10.13 *
*       64 *         3 *        21 *         0 *         0 * 62.037311 *     16.05 * 733549.86 * 16.975841 *
*       65 *         3 *        20 *         0 *         0 * 62.770861 *      14.5 * 437498.65 *      14.5 *
*       66 *         3 *        22 *         0 *         0 * 63.208359 *         0 * 1327494.0 *         0 *
*       67 *         1 *        11 *         0 *         0 * 64.535853 * 0.2956605 *         0 * 0.2956605 *
*       68 *         1 *        11 *         0 *         0 * 62.770861 * 0.9258411 *         0 * 0.9258411 *
*       69 *         1 *        11 *         0 *         0 * 60.706223 * 0.6238281 *         0 * 0.6238281 *
*       70 *         1 *        11 *         0 *         0 * 58.935851 * 0.1122891 *         0 * 0.1122891 *
*       71 *         1 *        11 *         0 *         0 * 57.341841 * 0.7862357 *         0 * 0.7862357 *
*       72 *         1 *        11 *         0 *         0 * 55.423142 * 1.2002522 *         0 * 1.2002522 *
*       73 *         1 *        11 *         0 *         0 * 54.319032 * 1.1485337 *         0 * 1.1485337 *
*       74 *         1 *        11 *         0 *         0 * 50.139253 * 0.2111353 *         0 * 0.2111353 *
*       75 *         1 *        11 *         0 *         0 * 47.389303 * 1.0682382 *         0 * 1.0682382 *
*       76 *         2 *        19 *         0 *         0 * 64.535853 *     10.79 * 26615.535 *         0 *
*       77 *         3 *        21 *         0 *         0 * 64.562469 *     13.39 * 2694328.2 * 13.584737 *
*       78 *         3 *        21 *         0 *         0 * 67.256797 *     16.05 * 9084080.6 * 17.146362 *
*       79 *         3 *        22 *         0 *         0 * 76.340878 *         0 * 2369318.1 *         0 *
*       80 *         1 *        11 *         0 *         0 * 78.710196 * 0.2728904 *         0 * 0.2728904 *
*       81 *         1 *        11 *         0 *         0 * 76.340878 * 1.0963626 *         0 * 1.0963626 *
*       82 *         1 *        11 *         0 *         0 * 67.256797 * 0.1947375 *         0 * 0.1947375 *
*       83 *         2 *        19 *         0 *         0 * 78.710196 *     10.79 * 26575.968 *         0 *
*       84 *         3 *        21 *         0 *         0 * 78.736772 *     10.79 * 5516320.4 * 11.017054 *
*       85 *         3 *        21 *         0 *         0 * 84.253093 *     16.05 * 3188945.4 * 16.424238 *
*       86 *         3 *        22 *         0 *         0 * 87.442038 *         0 * 6756938.1 *         0 *
*       87 *         1 *        11 *         0 *         0 * 94.198976 * 0.2519244 *         0 * 0.2519244 *
*       88 *         1 *        11 *         0 *         0 * 87.442038 * 0.3742385 *         0 * 0.3742385 *
*       89 *         1 *        11 *         0 *         0 * 84.253093 * 0.2270541 *         0 * 0.2270541 *
*       90 *         2 *        19 *         0 *         0 * 94.198976 *     10.79 * 146671.78 *         0 *
*       91 *         3 *        21 *         0 *         0 * 94.345648 *     13.39 * 387629.74 * 13.740585 *
*       92 *         3 *        21 *         0 *         0 * 94.733278 *     10.79 * 825310.69 * 11.529662 *
*       93 *         3 *        21 *         0 *         0 * 95.558588 *     13.39 * 2513850.9 * 13.669665 *
*       94 *         3 *        22 *         0 *         0 * 98.072439 *         0 * 299584.96 *         0 *
*       95 *         1 *        11 *         0 *         0 * 98.372024 * 0.2247074 *         0 * 0.2247074 *
*       96 *         1 *        11 *         0 *         0 * 98.072439 * 0.2796656 *         0 * 0.2796656 *
*       97 *         1 *        11 *         0 *         0 * 95.558588 * 0.7396627 *         0 * 0.7396627 *
*       98 *         1 *        11 *         0 *         0 * 94.733278 * 0.3505850 *         0 * 0.3505850 *
*       99 *         2 *        19 *         0 *         0 * 98.372024 *     10.79 * 234895.31 *         0 *
*      100 *         3 *        21 *         0 *         0 *  98.60692 *     16.05 * 1127643.5 * 16.333734 *
*      101 *         3 *        21 *         0 *         0 * 99.734563 *     13.39 * 8001311.5 * 13.585833 *
*      102 *         3 *        21 *         0 *         0 * 107.73587 *     10.79 * 1726944.4 * 11.457160 *
*      103 *         3 *        21 *         0 *         0 * 109.46282 *     13.39 * 1331764.7 * 13.581384 *
*      104 *         3 *        21 *         0 *         0 * 110.79458 *     13.39 * 2092830.3 * 13.537113 *
*      105 *         3 *        21 *         0 *         0 * 112.88741 *      32.3 * 778207.91 * 32.455213 *
*      106 *         3 *        21 *         0 *         0 * 113.66562 *     10.79 * 1180491.1 * 11.362828 *
*      107 *         3 *        21 *         0 *         0 * 114.84611 *     13.39 * 3535426.9 * 13.891004 *
*      108 *         3 *        21 *         0 *         0 * 118.38154 *     10.79 * 2465080.1 * 11.093422 *
*      109 *         3 *        21 *         0 *         0 * 120.84662 *     16.05 * 8618171.8 * 16.434264 *
*      110 *         3 *        21 *         0 *         0 * 129.46479 *     10.79 * 279583.91 * 11.073360 *
*      111 *         3 *        22 *         0 *         0 * 129.74437 *         0 * 2641570.6 *         0 *
*      112 *         1 *        11 *         0 *         0 * 132.38594 * 0.1289569 *         0 * 0.1289569 *
*      113 *         1 *        11 *         0 *         0 * 129.74437 * 0.2833604 *         0 * 0.2833604 *
*      114 *         1 *        11 *         0 *         0 * 129.46479 * 0.3842641 *         0 * 0.3842641 *
*      115 *         1 *        11 *         0 *         0 * 120.84662 * 0.3034226 *         0 * 0.3034226 *
*      116 *         1 *        11 *         0 *         0 * 118.38154 * 0.5010049 *         0 * 0.5010049 *
*      117 *         1 *        11 *         0 *         0 * 114.84611 * 0.5728281 *         0 * 0.5728281 *
*      118 *         1 *        11 *         0 *         0 * 113.66562 * 0.1552131 *         0 * 0.1552131 *
*      119 *         1 *        11 *         0 *         0 * 112.88741 * 0.1471137 *         0 * 0.1471137 *
*      120 *         1 *        11 *         0 *         0 * 110.79458 * 0.1913844 *         0 * 0.1913844 *
*      121 *         1 *        11 *         0 *         0 * 109.46282 * 0.6671605 *         0 * 0.6671605 *
*      122 *         1 *        11 *         0 *         0 * 107.73587 * 0.1958329 *         0 * 0.1958329 *
*      123 *         1 *        11 *         0 *         0 * 99.734563 * 0.2837345 *         0 * 0.2837345 *
*      124 *         2 *        19 *         0 *         0 * 132.38594 *     10.79 * 60396.270 *         0 *
*      125 *         3 *        21 *         0 *         0 * 132.44634 *     10.79 * 1417514.5 * 10.946709 *
*      126 *         3 *        21 *         0 *         0 * 133.86385 *     10.79 * 1697430.4 * 10.910124 *
*      127 *         3 *        21 *         0 *         0 * 135.56128 *     13.39 * 4209922.0 * 13.400704 *
*      128 *         3 *        21 *         0 *         0 * 139.77121 *     16.05 * 4763348.7 * 16.341884 *
*      129 *         3 *        21 *         0 *         0 * 144.53455 *      32.3 * 8344732.5 * 32.323317 *
*      130 *         3 *        21 *         0 *         0 * 152.87929 *     10.79 * 6534382.2 * 10.937910 *
*      131 *         3 *        22 *         0 *         0 * 159.41367 *         0 * 2140478.9 *         0 *
*      132 *         1 *        11 *         0 *         0 * 161.55415 * 0.0713516 *         0 * 0.0713516 *
*      133 *         1 *        11 *         0 *         0 * 159.41367 * 0.1479106 *         0 * 0.1479106 *
*      134 *         1 *        11 *         0 *         0 * 152.87929 * 0.0233173 *         0 * 0.0233173 *
*      135 *         1 *        11 *         0 *         0 * 144.53455 * 0.2918841 *         0 * 0.2918841 *
*      136 *         1 *        11 *         0 *         0 * 139.77121 * 0.0107049 *         0 * 0.0107049 *
*      137 *         1 *        11 *         0 *         0 * 135.56128 * 0.1201246 *         0 * 0.1201246 *
*      138 *         1 *        11 *         0 *         0 * 133.86385 * 0.1567098 *         0 * 0.1567098 *
*      139 *         2 *        19 *         0 *         0 * 161.55415 *     10.79 * 20289.797 *         0 *
*      140 *         3 *        22 *         0 *         0 * 161.57444 *         0 * 11580309. *         0 *
*      141 *         1 *        11 *         0 *         0 * 173.15475 * 0.0654403 *         0 * 0.0654403 *
*      142 *         2 *        19 *         0 *         0 * 173.15475 *     10.79 * 67579.780 *         0 *
*      143 *         3 *        22 *         0 *         0 * 173.22233 *         0 * 11242195. *         0 *
*      144 *         1 *        11 *         0 *         0 * 184.46452 * 0.0595323 *         0 * 0.0595323 *
*      145 *         2 *        18 *         0 *         0 * 184.46452 * 95.710462 * 24846162. * 95.710462 *
************************************************************************************************************

Got Error

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’ */
~~^

In ROOT 6, you need to:

root [0] .> dump.txt
.x dump.cxx
.>
root [3] 

Note also that, in many simple cases, you should be able to use the operating system redirection, e.g.:

[...]$ root -l -q dump.cxx > dump.txt

Wil_E_Coyote,

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();

Thank you everyone. Problem was solved.

In my case, after command:

root [0] .> dump.txt

Nothing happened, ROOT stacks and i can see only cursor lighting…Trying to wait 3 hour - nothing changed

Tryies this solution - How to convert .root to .txt file?
and get this Errors;

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);

dump.txt:


| Welcome to ROOT 6.16/00 https://root.cern |
| (c) 1995-2018, The ROOT Team |
| Built for linuxx8664gcc on Jan 23 2019, 09:06:13 |
| From tags/v6-16-00@v6-16-00 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/‘.q’

Processing dump.cxx…

Indeed in that example ‘fTree’ should have been just ‘t’ …

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);

" f " as in file is pointing to the TFile … you are looking for " t " as in tree.

Sorry. Change f to t and get this:

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);

sorry I am lost. Can you copy/paste the entire code?

d2.cxx:

// 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);
 ^

At least one “;” is missing (in the end of the line with “SetScanRedirect”).