The usage of vector type in ROOT


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi there,

Could you please point me out to the wrong lines about vector initialization and assignments in general?

The short code is just doing calculations with kinetic energy input. Before I turned everything into an array, it was doing alright.

Code will be reattached.

Cheers.

Hi @spyhunter01 ,

Your program throws many compilation errors, try to reduce them first to pinpoint your actual problem.
Just as an example, the very first error that appears to me when compiling your code:

g++ -O3 -o reproducer.o reproducer.cpp `root-config --cflags --glibs`
reproducer.cpp: In function 'int roottest()':
reproducer.cpp:54:21: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'std::vector<float>')
   54 |     cout <<"hello: "<< myresult << endl;
      |     ~~~~~~~~~~~~~~~~^~ ~~~~~~~~

Which refers to the fact that you cannot print a full vector in C++ with std::cout, you have to print its elements one by one (maybe create a helper function to do this so you don’t have to repeat this type of code many times).
Please try to address these simple problems first, then get back to us if any other issue is left.
Cheers,
Vincenzo

grep -r -l -E -e '(include|std|using).*vector' ${ROOTSYS}/t[eu]*

I guess you mean C++ tutorials?

https://en.cppreference.com/w/cpp/container/vector

google → c++ vector tutorial

@vpadulan

Can you tell me why I can’t compile this little code? Is there anything wrong in the root installation on my Mac?

Code is attached
roottest.cpp (2.1 KB)
.

The error I am having is below:

root [0]

Processing roottest.cpp++…

Info in TMacOSXSystem::ACLiC: creating shared library /Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/./roottest_cpp.so

clang: error: no such file or directory: ‘/usr/lib/libfakelink.dylib /usr/lib/libenergytrace.dylib /usr/lib/libz.1.dylib /usr/lib/libapple_nghttp2.dylib /usr/lib/libcompression.dylib /usr/lib/libnetwork.dylib /usr/lib/libsqlite3.dylib /usr/lib/libMobileGestalt.dylib /usr/lib/libcoretls.dylib /usr/lib/libcoretls_cfhelpers.dylib /usr/lib/libxar.1.dylib /usr/lib/libarchive.2.dylib /usr/lib/libxml2.2.dylib /usr/lib/swift/libswiftCore.dylib /usr/lib/swift/libswiftCoreFoundation.dylib /usr/lib/swift/libswiftDarwin.dylib /usr/lib/swift/libswiftDispatch.dylib /usr/lib/swift/libswiftIOKit.dylib /usr/lib/swift/libswiftObjectiveC.dylib /usr/lib/swift/libswiftXPC.dylib /usr/lib/swift/libswift_Concurrency.dylib /usr/lib/swift/libswift_StringProcessing.dylib /usr/lib/swift/libswiftos.dylib /usr/lib/libpcap.A.dylib /usr/lib/libdns_services.dylib /usr/lib/liblzma.5.dylib /usr/lib/libbz2.1.0.dylib /usr/lib/libiconv.2.dylib /usr/lib/libcharset.1.dylib /usr/lib/swift/libswift_RegexParser.dylib /usr/lib/libheimdal-asn1.dylib /usr/lib/libCheckFix.dylib /usr/lib/libmecab.dylib /usr/lib/libgermantok.dylib /usr/lib/libThaiTokenizer.dylib /usr/lib/libutil.dylib /usr/lib/libapp_launch_measurement.dylib /usr/lib/libmis.dylib /usr/lib/libxslt.1.dylib /usr/lib/libcmph.dylib /usr/lib/libTLE.dylib /usr/lib/libspindump.dylib /usr/lib/libate.dylib /usr/lib/libexpat.1.dylib /usr/lib/libAudioStatistics.dylib /usr/lib/libSMC.dylib /usr/lib/libperfcheck.dylib /usr/lib/libIOReport.dylib’

clang: error: no such file or directory: ‘/usr/lib/libncurses.5.4.dylib’

Error in : Executing 'cd “/Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++” ; clang++ -fPIC -c -O3 -DNDEBUG -std=c++17 -Wc++11-narrowing -Wsign-compare -Wsometimes-uninitialized -Wconditional-uninitialized -Wheader-guard -Warray-bounds -Wcomment -Wtautological-compare -Wstrncat-size -Wloop-analysis -Wbool-conversion -m64 -pipe -W -Woverloaded-virtual -fsigned-char -fno-common -Qunused-arguments -pthread -stdlib=libc++ -I/opt/homebrew/Cellar/root/6.

I meant if there was any in root website. I’d like to see an implementation of it.

This one works for me:

#include <iostream>
#include <stdio.h>
#include<locale.h>
#include "TCanvas.h"
#include "TFitResult.h"
#include "TDirectory.h"
#include "TMath.h"
#include "TROOT.h"
#include <vector>
#include"TString.h"
#include <cmath>
#define PI 3.14159265

using namespace std;

int roottest(){
    std::vector<float>T_energyLIST{20., 19., 18., 17., 16.};
    for (unsigned int i = 0; i < T_energyLIST.size(); i++ ){
        cout << "T_energy values: " << T_energyLIST[i] << endl;
    }
    float z_particle, M_particle, rho_material, A_material, Z_material, I_material;
    float area= TMath::Power(10.,-28.); //m^2, 4.pi.r^2
    float mc2= 0.511; //MeV
    float NA= 6.022*TMath::Power(10.,23.); //atoms / mole
    float c2= 931.5 ; // MeV/u
    float N_material =  (rho_material * NA )/ A_material ;
    //Arrays
    std::vector<float> gamma;
    std::vector<float> Beta;
    std::vector<float> V_particle;
    std::vector<float> AverageEnergyLossPerUnitDistance_O;
    std::vector<float> AverageEnergyLossPerUnitDistance_I;
    std::vector<float> result;

    std::cout<<"done"<<std::endl;
    return 0;
}

It gives:

 % root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.29/01                        https://root.cern |
  | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Jan 20 2023, 07:55:09                      |
  | From heads/master@v6-29-01-247-g8a998b5fda                       |
  | With Apple clang version 14.0.0 (clang-1400.0.29.202)            |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] .x roottest.cpp++
Info in <TMacOSXSystem::ACLiC>: creating shared library /Users/couet/Downloads/./roottest_cpp.so
ld: warning: ld: warning: ld: warning: ld: warning: dylib (/Users/couet/git/couet-root-bin/lib/libThread.so) was built for newer macOS version (12.6) than being linked (12.0)ld: warning: dylib (/Users/couet/git/couet-root-bin/lib/libCore.so) was built for newer macOS version (12.6) than being linked (12.0)dylib (/Users/couet/git/couet-root-bin/lib/libCling.so) was built for newer macOS version (12.6) than being linked (12.0)ld: warning: 
dylib (/Users/couet/git/couet-root-bin/lib/libRint.so) was built for newer macOS version (12.6) than being linked (12.0)

dylib (/Users/couet/git/couet-root-bin/lib/libRIO.so) was built for newer macOS version (12.6) than being linked (12.0)
dylib (/Users/couet/git/couet-root-bin/lib/libMathCore.so) was built for newer macOS version (12.6) than being linked (12.0)

ld: warning: dylib (/Users/couet/git/couet-root-bin/lib/libImt.so) was built for newer macOS version (12.6) than being linked (12.0)
ld: warning: dylib (/Users/couet/git/couet-root-bin/lib/libMultiProc.so) was built for newer macOS version (12.6) than being linked (12.0)
ld: warning: dylib (/Users/couet/git/couet-root-bin/lib/libNet.so) was built for newer macOS version (12.6) than being linked (12.0)
T_energy values: 20
T_energy values: 19
T_energy values: 18
T_energy values: 17
T_energy values: 16
done
(int) 0
root [1] 

or:

root [0] .x roottest.cpp
T_energy values: 20
T_energy values: 19
T_energy values: 18
T_energy values: 17
T_energy values: 16
done
(int) 0
root [1] 

sh: -c: line 0: unexpected EOF while looking for matching `"’

sh: -c: line 1: syntax error: unexpected end of file

It looks like there is a syntax error in the file. May be the copy/paste went wrong. Can you post the file you execute I will try it?

#include <iostream>

int spy(){
   std::cout << "done" << std::endl;
   return 0;
}
% root spy.C++

@spyhunter01 Uncle Google → Discourse code formatting

Does the extension of a file matter? I mean, can it be .cpp or .C when I compile.

I clearly have an independent compilation problem to solve.

no it doesn’t

@couet
Code file is here:
roottest.cpp (4.4 KB)

The code I attach now works if I run with .x rootlets.cpp. However, it doesn’t compile when I run with .x rootlets.cpp++.

That’s the message I get. Any conclusion why it gives “no such a directory” message. Should I reinstall ROOT on a Mac again?

spyhunter0066@ilkers-MacBook-Air c++ % root -l -q roottest.cpp++

Processing roottest.cpp++…

Info in TMacOSXSystem::ACLiC: creating shared library /Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/./roottest_cpp.so

clang: error: no such file or directory: ‘/usr/lib/libfakelink.dylib /usr/lib/libenergytrace.dylib /usr/lib/libz.1.dylib /usr/lib/libapple_nghttp2.dylib /usr/lib/libcompression.dylib /usr/lib/libnetwork.dylib /usr/lib/libsqlite3.dylib /usr/lib/libMobileGestalt.dylib /usr/lib/libcoretls.dylib /usr/lib/libcoretls_cfhelpers.dylib /usr/lib/libxar.1.dylib /usr/lib/libarchive.2.dylib /usr/lib/libxml2.2.dylib /usr/lib/swift/libswiftCore.dylib /usr/lib/swift/libswiftCoreFoundation.dylib /usr/lib/swift/libswiftDarwin.dylib /usr/lib/swift/libswiftDispatch.dylib /usr/lib/swift/libswiftIOKit.dylib /usr/lib/swift/libswiftObjectiveC.dylib /usr/lib/swift/libswiftXPC.dylib /usr/lib/swift/libswift_Concurrency.dylib /usr/lib/swift/libswift_StringProcessing.dylib /usr/lib/swift/libswiftos.dylib /usr/lib/libpcap.A.dylib /usr/lib/libdns_services.dylib /usr/lib/liblzma.5.dylib /usr/lib/libbz2.1.0.dylib /usr/lib/libiconv.2.dylib /usr/lib/libcharset.1.dylib /usr/lib/swift/libswift_RegexParser.dylib /usr/lib/libheimdal-asn1.dylib /usr/lib/libCheckFix.dylib /usr/lib/libmecab.dylib /usr/lib/libgermantok.dylib /usr/lib/libThaiTokenizer.dylib /usr/lib/libutil.dylib /usr/lib/libapp_launch_measurement.dylib /usr/lib/libmis.dylib /usr/lib/libxslt.1.dylib /usr/lib/libcmph.dylib /usr/lib/libTLE.dylib /usr/lib/libspindump.dylib /usr/lib/libate.dylib /usr/lib/libexpat.1.dylib /usr/lib/libAudioStatistics.dylib /usr/lib/libSMC.dylib /usr/lib/libperfcheck.dylib /usr/lib/libIOReport.dylib’

clang: error: no such file or directory: ‘/usr/lib/libncurses.5.4.dylib’

Error in : Executing ‘cd “/Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++” ; clang++ -fPIC -c -O3 -DNDEBUG -std=c++17 -Wc++11-narrowing -Wsign-compare -Wsometimes-uninitialized -Wconditional-uninitialized -Wheader-guard -Warray-bounds -Wcomment -Wtautological-compare -Wstrncat-size -Wloop-analysis -Wbool-conversion -m64 -pipe -W -Woverloaded-virtual -fsigned-char -fno-common -Qunused-arguments -pthread -stdlib=libc++ -I/opt/homebrew/Cellar/root/6.26.06_1/include/root -isysroot “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/” -I"/opt/homebrew/Cellar/root/6.26.06_1/etc/root" -I"/opt/homebrew/Cellar/root/6.26.06_1/etc/root/cling" -I"/opt/homebrew/Cellar/root/6.26.06_1/etc/root/cling/plugins/include" -I"/opt/homebrew/Cellar/root/6.26.06_1/include/root" -I"/opt/homebrew/Cellar/root/6.26.06_1/include" -D__ACLIC__ “/Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/roottest_cpp_ACLiC_dict.cxx” ; clang++ -O3 -DNDEBUG “/Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/roottest_cpp_ACLiC_dict.o” -dynamiclib -Wl,-headerpad_max_install_names -m64 -single_module -Wl,-dead_strip_dylibs -undefined dynamic_lookup “/opt/homebrew/Cellar/zstd/1.5.2/lib/libzstd.1.dylib” “/opt/homebrew/Cellar/zlib/1.2.13/lib/libz.1.dylib” “/opt/homebrew/Cellar/lz4/1.9.4/lib/liblz4.1.dylib” “/opt/homebrew/Cellar/xz/5.2.7/lib/liblzma.dylib” “/opt/homebrew/Cellar/pcre/8.45/lib/libpcre.dylib” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libRint.so” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libCore.so” “/opt/homebrew/Cellar/pcre/8.45/lib/libpcre.1.dylib” “/opt/homebrew/Cellar/xz/5.2.7/lib/liblzma.5.dylib” “/opt/homebrew/Cellar/lz4/1.9.4/lib/liblz4.1.9.4.dylib” “/opt/homebrew/Cellar/zlib/1.2.13/lib/libz.1.2.13.dylib” “/opt/homebrew/Cellar/zstd/1.5.2/lib/libzstd.1.5.2.dylib” “/usr/lib/libfakelink.dylib /usr/lib/libenergytrace.dylib /usr/lib/libz.1.dylib /usr/lib/libapple_nghttp2.dylib /usr/lib/libcompression.dylib /usr/lib/libnetwork.dylib /usr/lib/libsqlite3.dylib /usr/lib/libMobileGestalt.dylib /usr/lib/libcoretls.dylib /usr/lib/libcoretls_cfhelpers.dylib /usr/lib/libxar.1.dylib /usr/lib/libarchive.2.dylib /usr/lib/libxml2.2.dylib /usr/lib/swift/libswiftCore.dylib /usr/lib/swift/libswiftCoreFoundation.dylib /usr/lib/swift/libswiftDarwin.dylib /usr/lib/swift/libswiftDispatch.dylib /usr/lib/swift/libswiftIOKit.dylib /usr/lib/swift/libswiftObjectiveC.dylib /usr/lib/swift/libswiftXPC.dylib /usr/lib/swift/libswift_Concurrency.dylib /usr/lib/swift/libswift_StringProcessing.dylib /usr/lib/swift/libswiftos.dylib /usr/lib/libpcap.A.dylib /usr/lib/libdns_services.dylib /usr/lib/liblzma.5.dylib /usr/lib/libbz2.1.0.dylib /usr/lib/libiconv.2.dylib /usr/lib/libcharset.1.dylib /usr/lib/swift/libswift_RegexParser.dylib /usr/lib/libheimdal-asn1.dylib /usr/lib/libCheckFix.dylib /usr/lib/libmecab.dylib /usr/lib/libgermantok.dylib /usr/lib/libThaiTokenizer.dylib /usr/lib/libutil.dylib /usr/lib/libapp_launch_measurement.dylib /usr/lib/libmis.dylib /usr/lib/libxslt.1.dylib /usr/lib/libcmph.dylib /usr/lib/libTLE.dylib /usr/lib/libspindump.dylib /usr/lib/libate.dylib /usr/lib/libexpat.1.dylib /usr/lib/libAudioStatistics.dylib /usr/lib/libSMC.dylib /usr/lib/libperfcheck.dylib /usr/lib/libIOReport.dylib” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libRIO.so” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libThread.so” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libCling.so” “/usr/lib/libncurses.5.4.dylib” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libMathCore.so” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libImt.so” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libMultiProc.so” “/opt/homebrew/Cellar/tbb/2021.7.0/lib/libtbb.12.7.dylib” “/opt/homebrew/Cellar/root/6.26.06_1/lib/root/libNet.so” “/opt/homebrew/Cellar/openssl@1.1/1.1.1q/lib/libssl.1.1.dylib” “/opt/homebrew/Cellar/openssl@1.1/1.1.1q/lib/libcrypto.1.1.dylib” -o “/Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/./roottest_cpp.so”’ failed!

You are missing:

#include "TAxis.h"
#include "TPaveText.h"

That’s right. I added, but it seems I still miss some details. I checked the quotation marks, and there is not unmatched mark at all. Not sure if I have still missing header file. Not sure about syntax error too.

New error:

root [0] .x roottest.cpp++

Info in TMacOSXSystem::ACLiC: creating shared library /Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/./roottest_cpp.so

sh: -c: line 0: unexpected EOF while looking for matching `"’

sh: -c: line 1: syntax error: unexpected end of file

See my previous reply about that. Very likely you have the wrong quotes …

Let me check. I have one one quotation mark on my keyboard, but I check again now anyways.

@couet

I am certain that my quotation marks are ok and matched with each other. I have color coding to help me with that in Xcode.

Can you quickly check or run this code if you end up with same error?

Thanks.

roottest.cpp (5.3 KB)

Error I am having :

root [0] .x roottest.cpp++

Info in TMacOSXSystem::ACLiC: creating shared library /Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++/./roottest_cpp.so

sh: -c: line 0: unexpected EOF while looking for matching `"’

sh: -c: line 1: syntax error: unexpected end of file

Error in : Executing 'cd “/Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/c++” ; clang++ -fPIC -c -O3 -DNDEBUG -std=c++17 -Wc++11-narrowing -Wsign-compare -Wsometimes-uninitialized -Wconditional-uninitialized -Wheader-guard -Warray-bounds -Wcomment -Wtautological-compare -Wstrncat-size -Wloop-analysis -Wbool-conversion -m64 -pi

The file you posted is fine. It runs fine on my Mac.
Are you sure you really runs that file ?

@couet
Unfortunately, yes.

I have one quick question. It may solve my issue.

I want to simply read a file full of float values. Then, I want to pass those values in to a vector with float type (surely). However, entire web does not have any example. All they do is to store values in a string and then read it with get line() function in a while loop. Since it is a string, a conversion issue occurs for me in the end.

Code and input files are attached. I just need to adapt the code to read the file and fill a vector of float type.
readMyFile.cpp (940 Bytes)

EnergyList.txt (287 Bytes)

Thanks in advance.