Tree filled with string variables draw in histogram

Hello,

I am running a Monte Carlo simulation where I fill in a tree with isotopes names as string variables. All fine, but at the end of the simulation, instead of writing the large tree to the root file, I would like to draw/project the tree onto a TH1 histogram and write just the histogram to the file, so the final size of the root files can be kept small.

But I do not know the range of the string variables (isotopes names), so I cannot really pre-define a histogram. I am trying to do this in my class destructor (executed at the end of the simulation):

TH1F* hist;

tree->Draw(“isotope_name”);
hist = (TH1F*)htemp->Clone(“hist_coil_QCA”);

But I get a compilation warning: error: use of undeclared identifier ‘htemp’

I also tried:

tree->Draw(“isotope_name>>hist”);

but the code crashes without filling the histogram.

I would be very grateful for any suggestions.

Thank you,

Cristian

$ROOTSYS/tree/cerstaff.root

Is a file containing a tree with character variables. I used it to simulate the problem you encounter here:


$ root cernstaff.root
   ----------------------------------------------------------------
  | Welcome to ROOT 6.07/03                    http://root.cern.ch |
  |                                   (c) 1995-2016, The ROOT Team |
  | Built for macosx64                                             |
  | From heads/master@v6-07-02-331-g55ed62b, Feb 15 2016, 13:19:29 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'     |
   ----------------------------------------------------------------
root [0] _file0->ls()
TFile**		cernstaff.root	
 TFile*		cernstaff.root	
  KEY: TTree	T;1	CERN 1988 staff data
root [1] T->Draw("Nation");
root [2] TH1F *hist = (TH1F*)htemp->Clone("hist");
root [3] hist->Draw()

As you can see it seems fine… (ROOT version 6.07/03 on Mac)]

Hello,

Thank you for your reply. Yes, within Root it is fine, I was using this in the past. But now I am trying to do it in my Monte Carlo code (Geant4), where I have access to root header files, the code knows about TTree and TH1F and creates the TTree object, and fills it and can write it to a root file at the end of the run. But the root file is huge, and there are many simulation runs to add the output files from …

So I was trying to use the same technique, which works inside the Root session, to draw the tree into a histogram and write only the histogram to the root file at the end, without the tree object. But Geant4, while it knows about TTree and TH1F classes and objects, has no idea what “htemp” is. So I cannot use this method. Is it possible though to draw or project the tree containing strings to a histogram (outside root)?

Thank you,

Cristian

may be:

  nt->Draw("..");
  TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp");    

Now I get this during the compilation, so not even running yet:

SteppingAction.cc:59:25: error: use of undeclared identifier ‘gPad’. The code knows and can use/fill/draw TTree and TH1F objects though.

I was hoping it would be possible to draw the tree into a TH1F histogram directly using those string variables… Probably not possible? With numbers/data it would be easy, as I could define a histogram with a large range, but for characters/stings…

Thank you,

Cristian

You should include TPad.h

Thank you. Now it compiled fine, but at the end of the run it crashed right at the line

TH1* htemp = (TH1*)gPad->FindObject("htemp");

which I tested with cout messages after each line. Well, I guess I should give up and live with the large tree objects. Thank you for all your replies and wanting to help with this.

If anything else comes to mind though, please let me know of course.

Best regards,

Cristian

Have you tried with TH1F or TH1D instead of TH1 ? (like in the example I sent you).

Yes, and no difference. But I have noticed something odd:

if I define a canvas before, the code goes one step further:

 TCanvas*c1 = new TCanvas();
G4cout << "1" << G4endl;
 tree_coil_QCA->Draw("iso","w");
G4cout << "2" << G4endl;
TH1F* htemp = (TH1F*)gPad->FindObject("htemp");
G4cout << "3" << G4endl;
hist_coil_QCA = (TH1F*)htemp->Clone("hist_coil_QCA");
G4cout << "4" << G4endl;

and the run produces:


Graphics systems deleted.
Visualization Manager deleting…
G4 kernel has come to Quit state.
1
2
3

*** Break *** segmentation violation
Generating stack trace…
0x000000010b631269 in SteppingAction::~SteppingAction() (in g4root_ESS_isotopes) + 25
0x000000010f1cfc6b in G4SteppingManager::~G4SteppingManager() (in libG4tracking.dylib) (G4SteppingManager.cc:110)
0x000000010f1d9eea in G4TrackingManager::~G4TrackingManager() (in libG4tracking.dylib) (G4TrackingManager.cc:62)
0x000000010f127fb3 in G4EventManager::~G4EventManager() (in libG4event.dylib) (G4EventManager.cc:84)
0x000000010f0b340e in G4RunManagerKernel::~G4RunManagerKernel() (in libG4run.dylib) (G4RunManagerKernel.cc:283)
0x000000010f0b399e in G4RunManagerKernel::~G4RunManagerKernel() (in libG4run.dylib) (G4RunManagerKernel.cc:262)
0x000000010f0a58c7 in G4RunManager::~G4RunManager() (in libG4run.dylib) (G4RunManager.cc:224)
0x000000010f0a5a5e in G4RunManager::~G4RunManager() (in libG4run.dylib) (G4RunManager.cc:184)
0x000000010b61c4c3 in main (in g4root_ESS_isotopes) + 6595
0x00007fff8c3ce5ad in start (in libdyld.dylib) + 1
Cristians-iMac:ESS_isotopes-build cristian$

According to the traceback you sent it seems to crash in Geant4.
May be you should contact the Geant4 support.

The crash is in SteppingAction indeed, but where the tree is used to produce the histogram. Without this, the root file is produced fine with the tree written to it. But these lines are in the Geant4 code indeed.

Also adding htemp->Write():

 TCanvas*c1 = new TCanvas();
G4cout << "1" << G4endl;
 tree_coil_QCA->Draw("iso","w");
G4cout << "2" << G4endl;
TH1F* htemp = (TH1F*)gPad->FindObject("htemp");
htemp->Write();
G4cout << "3" << G4endl;
hist_coil_QCA = (TH1F*)htemp->Clone("hist_coil_QCA");
G4cout << "4" << G4endl;

will result in the code not reaching the cout “3” stage:

Graphics systems deleted.
Visualization Manager deleting…
G4 kernel has come to Quit state.
1
2

*** Break *** segmentation violation
Generating stack trace…
0x000000010f902269 in SteppingAction::~SteppingAction() (in g4root_ESS_isotopes) + 25

But I guess that this is indeed not a ROOT problem, as Root is fine. I will try to contact the Geant4 support then…

Thank you for your replies,

Best wishes,

Cristian

Yes… try that. If they do not have a solution you can come back here.