Save output in the TTree ROOT

Hi,

I’m trying to run the main112.cc in the PYTHIA 8.230 (Heavy Ions) and save the output in the TTree.

In the main112 (that worked very well before) I put the command line to make the TTree and the file to save.

What I did:

// ROOT, for saving Pythia events as trees in a file.
#include "TTree.h"
#include "TFile.h"

int main(){
  
  // Set up the ROOT TFile and TTree.
  TFile *file = TFile::Open("softQCD.root","recreate");
  //TTRee " T " with the variable of the event
  TTree *T = new TTree("T","Tree");
    
  double Eta;
  //The Branchs of the TTree "T".
  T->Branch("Eta",&Eta);
  
  Eta = eta;

  // **************************
  //Fill the TTRee "T"
  T->Fill();
  // *************************

  // Write tree.
  T->Write();
  delete file;
}

put this in the main112.cc, thus, I have the code below:

// main112.cc is a part of the PYTHIA event generator.
// Copyright (C) 2017 Torbjorn Sjostrand.
// PYTHIA is licenced under the GNU GPL version 2, see COPYING for details.
// Please respect the MCnet Guidelines, see GUIDELINES for details.

// This test program will generate p-Pb collisions at sqrt(S_NN)=5TeV
// using Angantyr model for Heavy Ion collisions. The analysis will
// divide the event in centrality classes and measure the charged
// pseudo-rapidity distribution in each class in the way described in
// the ATLAS analysis in arXiv:1508.00848 [hep-ex].

//http://home.thep.lu.se/Pythia/pythia82html/HeavyIons.html

#include "Pythia8/Pythia.h"

// ROOT, for saving Pythia events as trees in a file.
#include "TTree.h"
#include "TFile.h"

// You need to include this to get access to the HIInfo object for
// HeavyIons.
#include "Pythia8/HeavyIons.h"

using namespace Pythia8;

int main() {

  Pythia pythia;

  // Setup the beams.
  pythia.readString("Beams:idA = 2212");  // proton.
  pythia.readString("Beams:idB = 1000822080"); // Lead.
  //pythia.readString("softQCD:singleDiffractive = on");
  //pythia.readString("Beams:idB = 1000060120"); // Carbon. 
  pythia.readString("Beams:eA = 4000");
  pythia.readString("Beams:eB = 1570");
  pythia.readString("Beams:frameType = 2");

  // Initialize the Angantyr model to fit the total and semi-includive
  // cross sections in Pythia within some tolerance.
  pythia.readString("HeavyIon:SigFitErr = "
                    "0.02,0.02,0.1,0.05,0.05,0.0,0.1,0.0");
  // These parameters are typicall suitable for sqrt(S_NN)=5TeV
  pythia.readString("HeavyIon:SigFitDefPar = "
                    "17.24,2.15,0.33,0.0,0.0,0.0,0.0,0.0");
  // A simple genetic algorithm is run for 20 generations to fit the
  // parameters.
  pythia.readString("HeavyIon:SigFitNGen = 20");

  // There will be eight centrality bins based on the sum transverse
  // emergy in a rapidity interval between -4.9 and -3.2. The borders
  // between the classes have been read off the plot in the paper:
  double explim[] = {90.0, 66.0, 53.0, 41.0, 32.0, 24.0, 13.0, 6.0};
  // Alternatively we can obtain the borders from the generated
  // transverse energy spectrum. The default settings should give
  // approximately the following:
  double genlim[] = {77.5, 54.5, 44.4, 34.1, 27.6, 22.5, 14.2, 3.5};
  // If you change any parameters these should also be changed.

  // The upper edge of the correponding percentiles:
  double pclim[] = {0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.6, 0.9};

  // Book the pseudorapidity histograms and get counter for sum of
  // event weights:
  typedef map<double,int,std::greater<double> > MapIdx;
  MapIdx expetaidx, genetaidx;
  vector<Hist*> expetadist(8), genetadist(8);
  string expetaname("EtadistCexp"), genetaname("EtadistCgen");
  vector<double> expsumw(8, 0.0), gensumw(8, 0.0);
  for ( int i = 0; i < 8; ++i ) {
    expetaidx[explim[i]] = i;
    expetadist[i] = new Hist(expetaname + char('0' + i), 54, -2.7, 2.7);
    genetaidx[genlim[i]] = i;
    genetadist[i] = new Hist(genetaname + char('0' + i), 54, -2.7, 2.7);
  }

  // Book histogram for the centrality measure.
  Hist sumet("SumETfwd", 100, 0.0, 200.0);

  // Also make a map of all weight to check the generated centrality
  // classes.
  multimap<double,double> gencent;

  // Book a histogram for the distribution of number of wounded
  // nucleons.
  Hist wounded("Nwounded", 60, -0.5, 59.5);

  // Sum up the weights of all generated events.
  double sumw = 0.0;

  // Initialise Pythia.
  pythia.init();
  
  // ********************************************************************
  
  // Set up the ROOT TFile and TTree.
    TFile *file = TFile::Open("softQCD.root","recreate");
    
    //TTRee " T " with the variable of the event
    TTree *T = new TTree("T","Tree");
    
    double Eta;
    
    
    //The Branchs of the TTree "T".
    T->Branch("Eta",&Eta);
    
   // ******************************************************************

  // Loop over events.
  int nEvents = 1000;
  for ( int iEvent = 0; iEvent < nEvents; ++iEvent ) {
    if ( !pythia.next() ) continue;

    // First sum up transverse energy for centrality measure and also
    // check that the trigger requiring ar least one charged particle
    // forward and backward.
    double etfwd = 0.0;
    bool trigfwd = false;
    bool trigbwd = false;
    for (int i = 0; i < pythia.event.size(); ++i) {
      Particle & p = pythia.event[i];
      if ( p.isFinal() ) {
        double eta = p.eta();
        
        Eta = eta;
        
        
        if ( p.isCharged() && p.pT() > 0.1 && eta < -2.09 && eta > -3.84 )
          trigfwd = true;
        if ( p.isCharged() && p.pT() > 0.1 && eta > 2.09 && eta < 3.84 )
          trigbwd = true;
        if ( p.pT() > 0.1 && eta < -3.2 && eta > -4.9 )
          etfwd += p.eT();
        
        // **************************
        //Fill the TTRee "T"
        T->Fill();
        // *************************
      }
    }
    
    // *********************************************************************************
    //T->Print(); // Print the screen (terminal) the information about the TTRee T
    
    // Write tree.
    T->Write();
  
    delete file;
    
    // *********************************************************************************
    
    // Skip if not triggered
    if ( !(trigfwd && trigbwd) ) continue;

    // Keep track of the sum of waights
    double weight = pythia.info.weight();
    sumw += weight;

    // Histogram and save the summed Et.
    sumet.fill(etfwd, weight);
    gencent.insert(make_pair(etfwd, weight));

    // Also fill the number of (absorptively and diffractively)
    // wounded nucleaons.
    int nw = pythia.info.hiinfo->nAbsTarg() +
             pythia.info.hiinfo->nDiffTarg();
    wounded.fill(nw, weight);

    // Find the correct centrality histograms.
    MapIdx::iterator expit =  expetaidx.upper_bound(etfwd);
    int expidx = expit== expetaidx.end()? -1: expit->second;
    MapIdx::iterator genit = genetaidx.upper_bound(etfwd);
    int genidx = genit== genetaidx.end()? -1: genit->second;

    // Sum the weights in the centrality classes, skip if not in a class.
    if ( expidx < 0 && genidx < 0 ) continue;
    if ( expidx >= 0 ) expsumw[expidx] += weight;
    if ( genidx >= 0 ) gensumw[genidx] += weight;

    // Go through the event again and fill the eta distributions.
    for (int i = 0; i < pythia.event.size(); ++i) {
      Particle & p = pythia.event[i];
      if ( p.isFinal() && p.isCharged() &&
           abs(p.eta()) < 2.7 && p.pT() > 0.1 ) {
        if ( expidx >= 0 ) expetadist[expidx]->fill(p.eta(), weight);
        if ( genidx >= 0 ) genetadist[genidx]->fill(p.eta(), weight);
      }
    }
  }

  // The run is over, so we write out some statistics.


  // Now, we just have to normalize and prtint out the histograms. We
  // choose to print the histograms to a file that can be read by
  // eg. gnuplot.
  ofstream ofs("main112.dat");

  sumet /= sumw*2.0;
  ofs << "# " << sumet.getTitle() << endl;
  sumet.table(ofs);

  wounded /= sumw;
  ofs << "\n# " << wounded.getTitle() << endl;
  wounded.table(ofs);


  // Print out the centrality binned eta distributions and delete the
  // heap-allocate histograms.
  for ( int idx = 0; idx < 8; ++idx ) {
    *expetadist[idx] /= expsumw[idx]*0.1;
    ofs << "\n# " << expetadist[idx]->getTitle() << endl;
    expetadist[idx]->table(ofs);
    delete expetadist[idx];
    *genetadist[idx] /= gensumw[idx]*0.1;
    ofs << "\n# " << genetadist[idx]->getTitle() << endl;
    genetadist[idx]->table(ofs);
    delete genetadist[idx];
  }

  // Befor we end, we want to check that our generated centrality
  // classes were the same as we guessed.
  double curr = 0.0;
  double prev = 0.0;
  double acc = 0.0;
  int idxa = 7;
  double lim = sumw*(1.0 - pclim[idxa]);
  vector<double> newlim(8);
  for ( multimap<double, double>::iterator it = gencent.begin();
        it != gencent.end(); ++it ) {
    prev = curr;
    curr = it->first;
    double w = it->second;
    if ( acc < lim && acc + w >= lim ) {
      newlim[idxa--] = prev + (curr - prev)*(lim - acc)/w;
      lim = sumw*(1.0 - pclim[idxa]);
    }
    acc += w;
  }

  cout << "The generated limits between centrality classes in this run:\n"
       << "   %   assumed    actual      data\n";
  for ( int idx = 0; idx < 8; ++idx )
    cout << setw(4) << int(pclim[idx]*100.0 + 0.5)
         << setw(10) << fixed << setprecision(1) << genlim[idx]
         << setw(10) << fixed << setprecision(1) << newlim[idx]
         << setw(10) << fixed << setprecision(1) << explim[idx] << endl;
         
     

  // And we're done!
  return 0;
}

I’m using:

  • ROOT 6.13/01 Built for linuxx8664gcc From heads/master@v6-11-02-2013-gd639b6a, Mar 09 2018, 10:15:08

in the folder example in the pythia 8230,
I run the main112.cc

Error:

main112.cc:(.text.startup+0xa82): undefined reference to `TFile::Open(char const*, char const*, char const*, int, int)'
main112.cc:(.text.startup+0xa8e): undefined reference to `TDirectory::CurrentDirectory()'
main112.cc:(.text.startup+0xa9e): undefined reference to `TStorage::ObjectAlloc(unsigned long)'
main112.cc:(.text.startup+0xabf): undefined reference to `TTree::TTree(char const*, char const*, int, TDirectory*)'
main112.cc:(.text.startup+0xace): undefined reference to `TDataType::GetType(std::type_info const&)'
main112.cc:(.text.startup+0xad9): undefined reference to `TBuffer::GetClass(std::type_info const&)'
main112.cc:(.text.startup+0xb01): undefined reference to `TTree::BranchImpRef(char const*, TClass*, EDataType, void*, int, int)'
main112.cc:(.text.startup+0x1fb4): undefined reference to `TObject::operator delete(void*)'
/tmp/cc06yBsM.o: In function `_GLOBAL__sub_I_main112.cc':
main112.cc:(.text.startup+0x3b): undefined reference to `TVersionCheck::TVersionCheck(int)'
collect2: error: ld returned 1 exit status
Makefile:61: recipe for target 'main112' failed
make: *** [main112] Error 1

in the Makefile (Error line 61):

# Examples without external dependencies.
main% : main%.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ $(CXX_COMMON)

File: libpythia8.a

andre@andre-Inspiron-5548:~/pythia8230/lib$ ls
libpythia8.a

How I can fix this error?

Cheers, Andre

I had progress on this issue…

In the Makefile I put the following FLAGS:
(This give the information to the compiler where looking for the library )

FLAGS = -w -I/opt/root6/include -I../include -O2  -pedantic -W -Wall -Wshadow -fPIC -L../lib -Wl,-rpath,../lib -lpythia8 -ldl  `root-config --cflags` -Wl,-rpath,./ -Wl,-rpath,/opt/root6/lib `/opt/root6/bin/root-config --glibs`

# Examples without external dependencies.
main% : main%.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ $(CXX_COMMON) $(FLAGS)

And the following error

no particle data has been changed from its default value 

 --------  End PYTHIA Particle Data Table  -----------------------------------------------------------------------------------------


 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007fdef1f6507a in __GI___waitpid (pid=16182, stat_loc=stat_loc
entry=0x7ffeb749e200, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007fdef1eddfbb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007fdef366cbdd in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x21ed570) at /opt/root6/root6_src/core/unix/src/TUnixSystem.cxx:2118
#3  TUnixSystem::StackTrace (this=0x21ed570) at /opt/root6/root6_src/core/unix/src/TUnixSystem.cxx:2412
#4  0x00007fdef366f21c in TUnixSystem::DispatchSignals (this=0x21ed570, sig=kSigSegmentationViolation) at /opt/root6/root6_src/core/unix/src/TUnixSystem.cxx:3643
#5  <signal handler called>
#6  0x000000000047db8f in main ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum.
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x000000000047db8f in main ()
===========================================================

Cheers, Andre

On the first question: you seem to be missing to link libRIO from ROOT in your Makefile.

On the second one, I will need some more information for example, which is the line of code that triggers the failure.

Hi @vvassilev

The first question, above just put the part that appeared the error.

first question: you seem to be missing to link libRIO from ROOT in your Makefile. This is in the Makefile (complete).

Makefile (complete):

# Makefile is a part of the PYTHIA event generator.
# Copyright (C) 2017 Torbjorn Sjostrand.
# PYTHIA is licenced under the GNU GPL version 2, see COPYING for details.
# Please respect the MCnet Guidelines, see GUIDELINES for details.
# Author: Philip Ilten, September 2014.
#
# This is is the Makefile used to build PYTHIA examples on POSIX systems.
# Example usage is:
#     make main01
# For help using the make command please consult the local system documentation,
# i.e. "man make" or "make --help".

################################################################################
# VARIABLES: Definition of the relevant variables from the configuration script.
################################################################################

FLAGS = -w -I/opt/root6/include -I../include -O2  -pedantic -W -Wall -Wshadow -fPIC -L../lib -Wl,-rpath,../lib -lpythia8 -ldl  `root-config --cflags` -Wl,-rpath,./ -Wl,-rpath,/opt/root6/lib `/opt/root6/bin/root-config --glibs`

# Set the shell.
SHELL=/usr/bin/env bash

# Include the configuration.
-include Makefile.inc

# Handle GZIP support.
ifeq ($(GZIP_USE),true)
  CXX_COMMON+= -DGZIPSUPPORT -I$(GZIP_INCLUDE)
  CXX_COMMON+= -L$(GZIP_LIB) -Wl,-rpath,$(GZIP_LIB) -lz
endif

# Check distribution (use local version first, then installed version).
ifneq ("$(wildcard ../lib/libpythia8.*)","")
  PREFIX_LIB=../lib
  PREFIX_INCLUDE=../include
endif
CXX_COMMON:=-I$(PREFIX_INCLUDE) $(CXX_COMMON)
CXX_COMMON+= -L$(PREFIX_LIB) -Wl,-rpath,$(PREFIX_LIB) -lpythia8 -ldl 

################################################################################
# RULES: Definition of the rules used to build the PYTHIA examples.
################################################################################

# Rules without physical targets (secondary expansion for specific rules).
.SECONDEXPANSION:
.PHONY: all clean

# All targets (no default behavior).
all:
	@echo "Usage: make mainXX"

# The Makefile configuration.
Makefile.inc:
	$(error Error: PYTHIA must be configured, please run "./configure"\
                in the top PYTHIA directory)

# PYTHIA libraries.
$(PREFIX_LIB)/libpythia8.a :
	$(error Error: PYTHIA must be built, please run "make"\
                in the top PYTHIA directory)

# Examples without external dependencies.
main% : main%.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ $(CXX_COMMON) $(FLAGS)

# MixMax.
main23: $$@.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ -std=c++11 -w $(CXX_COMMON)

# GZIP (required).
main34: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(GZIP_USE),true)
	$(CXX) $< -o $@ $(CXX_COMMON)
else
	@echo "Error: $@ requires GZIP"
endif

# HEPMC2.
main41 main42 main43 main85 main86 main87 main88 main89: $$@.cc\
	$(PREFIX_LIB)/libpythia8.a
ifeq ($(HEPMC2_USE),true)
	$(CXX) $< -o $@ -I$(HEPMC2_INCLUDE) $(CXX_COMMON)\
	 -L$(HEPMC2_LIB) -Wl,-rpath,$(HEPMC2_LIB) -lHepMC
else
	@echo "Error: $@ requires HEPMC2"
endif

# PROMC.
main46: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(PROMC_USE),true)
	$(CXX) $< -o $@ -I$(PROMC_INCLUDE)/src -I$(PROMC_INCLUDE)/include\
	 $(CXX_COMMON) -DPROMC=\"$(PROMC_INCLUDE)\" -Wno-long-long\
	 -L$(PROMC_LIB) -Wl,-rpath,$(PROMC_LIB) -lpromc -lprotoc -lprotobuf\
	 -lprotobuf-lite -lcbook
else
	@echo "Error: $@ requires PROMC"
endif

# EVTGEN (and HEPMC2).
main48: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(EVTGEN_USE)$(HEPMC2_USE)$(ENABLE_SHARED),truetruetrue)
	$(CXX) $< -o $@ -I$(EVTGEN_INCLUDE) $(CXX_COMMON)\
	 -DEVTGEN_PYTHIA -DEVTGEN_EXTERNAL -Wl,-rpath,$(HEPMC2_LIB)\
	 -L$(EVTGEN_LIB) -Wl,-rpath,$(EVTGEN_LIB) -lEvtGenExternal -lEvtGen
else
	@echo "Error: $@ requires EVTGEN and HEPMC2"
endif

# FASTJET3.
main71 main72 main75: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(FASTJET3_USE),true)
	$(CXX) $< -o $@ -I$(FASTJET3_INCLUDE) $(CXX_COMMON)\
	 -L$(FASTJET3_LIB) -Wl,-rpath,$(FASTJET3_LIB) -lfastjet
else
	@echo "Error: $@ requires FASTJET3"
endif

# FASTJET3 with modified Mass-Drop Tagger.
main74: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(FASTJET3_USE),true)
	$(CXX) $< -o $@ -I$(FASTJET3_INCLUDE) $(CXX_COMMON)\
	 -L$(FASTJET3_LIB) -Wl,-rpath,$(FASTJET3_LIB) -lfastjet -lRecursiveTools
else
	@echo "Error: $@ requires FASTJET3"
endif

# FASTJET3 and HEPMC2.
main81 main82 main83 main84: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(FASTJET3_USE)$(HEPMC2_USE),truetrue)
	$(CXX) $< -o $@ -I$(FASTJET3_INCLUDE) -I$(HEPMC2_INCLUDE) $(CXX_COMMON)\
	 -L$(HEPMC2_LIB) -Wl,-rpath,$(HEPMC2_LIB) -lHepMC\
	 -L$(FASTJET3_LIB) -Wl,-rpath,$(FASTJET3_LIB) -lfastjet
else
	@echo "Error: $@ requires FASTJET3 and HEPMC2"
endif

# ROOT (turn off all warnings for readability).
main91: $$@.cc $(PREFIX_LIB)/libpythia8.a
ifeq ($(ROOT_USE),true)
	$(CXX) $< -o $@ -w -I$(ROOT_INCLUDE) $(CXX_COMMON)\
	 `$(ROOTBIN)root-config --cflags`\
	 -Wl,-rpath,$(ROOT_LIB) `$(ROOT_BIN)root-config --glibs`
else
	@echo "Error: $@ requires ROOT"
endif
main92: $$@.cc $(PREFIX_LIB)/libpythia8.a main92.so
ifeq ($(ROOT_USE),true)
	$(CXX) $< main92.so -o $@ -w -I$(ROOT_INCLUDE) $(CXX_COMMON)\
	 `$(ROOTBIN)root-config --cflags` -Wl,-rpath,./\
	 -Wl,-rpath,$(ROOT_LIB) `$(ROOT_BIN)root-config --glibs`
else
	@echo "Error: $@ requires ROOT"
endif
main92.so: main92Dct.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ -w -I$(ROOT_INCLUDE) $(CXX_SHARED) $(CXX_COMMON)\
	 `$(ROOTBIN)root-config --cflags`
main92Dct.cc: main92.h main92LinkDef.h
	export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(ROOT_LIB);\
	 $(ROOT_BIN)rootcint -f $@ -c -I$(PREFIX_INCLUDE) $^

# User-written examples for tutorials, without external dependencies.
mymain% : mymain%.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ $(CXX_COMMON)

# Internally used tests, without external dependencies.
test% : test%.cc $(PREFIX_LIB)/libpythia8.a
	$(CXX) $< -o $@ $(CXX_COMMON)

# Clean.
clean:
	@rm -f main[0-9][0-9]; rm -f out[0-9][0-9];\
	rm -f main[0-9][0-9][0-9]; rm -f out[0-9][0-9][0-9];\
	rm -f mymain[0-9][0-9]; rm -f myout[0-9][0-9];\
	rm -f test[0-9][0-9][0-9]; rm -f *.dat\
	rm -f weakbosons.lhe; rm -f Pythia8.promc; rm -f hist.root;\
	rm -f *~; rm -f \#*; rm -f core*; rm -f *Dct.*; rm -f *.so;

I’m trying to use a library from ROOT : TTree . When I do make main112:

root@andre-Inspiron-5548:/home/andre/pythia8230/examples# make main112
g++ main112.cc -o main112 -I../include -O2  -pedantic -W -Wall -Wshadow -fPIC -L../lib -Wl,-rpath,../lib -lpythia8 -ldl  -w -I/opt/root6/include -I../include -O2  -pedantic -W -Wall -Wshadow -fPIC -L../lib -Wl,-rpath,../lib -lpythia8 -ldl  `root-config --cflags` -Wl,-rpath,./ -Wl,-rpath,/opt/root6/lib `/opt/root6/bin/root-config --glibs`
root@andre-Inspiron-5548:/home/andre/pythia8230/examples#

It worked (pythia), but I think the problem is when the compiler call the library TTree
from root to save the file in the .root

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