Export data ( .root -> .txt)

Hi,
I get a .root file in the example Geant4

how can export data from .root to .txt ?
What am I doing wrong?

I try this way and get the error (see figure 2):

TFile *f =TFile::Open("yz_t1.root");
.ls
Th1D *yz; file->GetObject("yz-distributions/yz.1",yz);
.>abc.txt
->Print("all");
.>

error message:
input_line_35:2:3: error: use of undeclared identifier 'Th1D'
 (Th1D * ((*(class TTree **)0x7f0b6e99b010)))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (Th1D * ((*(class TTree **)0x7f0b6e99b010))).
Execution of your code was aborted.


Please read tips for efficient and successful posting and posting code

ROOT Version: ROOT 6.22/03
Platform: Ubuntu 18
Compiler: Not Provided


Hello,

You get that error because the h in TH1D must be uppercase.

Thank you for your answer.
But there was another error in the terminal

root [3] TH1D *yz; file->GetObject("yz-distributions/yz.1",yz);ls
input_line_39:2:3: error: use of undeclared identifier 'file'
 (file->GetObject("yz-distributions/yz.1", ((*(class TH1D **)0x7f616506b020))))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (file->GetObject("yz-distributions/yz.1", ((*(class TH1D **)0x7f616506b020)))).
Execution of your code was aborted.

file is not defined, the name of the variable should be f in f->GetObject.

auto f = new TFile("yz_t1.root");
TH1D *yz = (TH1D*)f->Get("yz-distributions/yz.1");
.>abc.txt
yz->Print("all");
.>

Thank you for your help,
I’m just learning, so I have a lot of problems here. And I’m so sorry

Gean4 generates multiple .root files
1.)yz.root
2.)yz_t0.root
3.) yz_t1

Using your macrocode, I’ve tested exporting data for each file.

In all cases, the same error occurs. (See picture)
What am I doing wrong?

files

yz.root (13.7 KB) yz_t0.root (31.9 KB) yz_t1.root (32.0 KB)

In your file yz_t1.root you do not have a folder yz-distributions containing a TH1D named yz. You have a TTree named xy:

% root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.23/01                        https://root.cern |
  | (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Oct 14 2020, 08:50:48                      |
  | From heads/master@v6-23-01-1567-g6f8fd46266                      |
  | With Apple clang version 12.0.0 (clang-1200.0.32.2)              |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] auto f = new TFile("yz_t1.root");
root [1] f->ls()
TFile**		yz_t1.root	
 TFile*		yz_t1.root	
  KEY: TTree	yz;1	yz-distributions
root [2] yz->Print()
******************************************************************************
*Tree    :yz        : yz-distributions                                       *
*Entries :        6 : Total =            5389 bytes  File  Size =       1098 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :radius    : Double_t yz                                            *
*Entries :        6 : Total  Size=        725 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :eventID   : Double_t yz                                            *
*Entries :        6 : Total  Size=        730 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :nbHits    : Double_t yz                                            *
*Entries :        6 : Total  Size=        725 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    3 :nbScoredHits : Double_t yz                                         *
*Entries :        6 : Total  Size=        755 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    4 :y         : Double_t yz                                            *
*Entries :        6 : Total  Size=        700 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    5 :z         : Double_t yz                                            *
*Entries :        6 : Total  Size=        700 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    6 :Einc      : Double_t yz                                            *
*Entries :        6 : Total  Size=        715 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*