Defining new classes. ROOT context: public vs private

Hello,

I don’t really know how to properly tittle this question but I have the following problem:

I am defining a new class TRestParticleGenerator at the moment not inheriting from any root class, below is my header file:

#ifndef REST_TRESTPARTICLEGENERATOR_H
#define REST_TRESTPARTICLEGENERATOR_H

#endif  // REST_TRESTPARTICLEGENERATOR_H

#include <TVector3.h>
#include <map>
#include <set>

using namespace std;

class TRestParticleGenerator {
   private:

    TVector3 particlePosition;
    TVector3 particleDirection;
    string particleName;
    // configuration

    enum energyGeneratorTypes {
        // NOT_ASSIGNED, // this is the default value, has to be changed
        TH1D,
        MONO,
        FLAT,
    };

    map<string, energyGeneratorTypes> energyGeneratorTypesMap = {
        {"T1HD", energyGeneratorTypes::TH1D},
        {"MONO", energyGeneratorTypes::MONO},
        {"FLAT", energyGeneratorTypes::FLAT},
    };

    energyGeneratorTypes energyGeneratorType;

   public:

    inline string GetEnergyGeneratorType() {
        for (auto const& pair : energyGeneratorTypesMap) {
            if (pair.second == energyGeneratorType) {
                return pair.first;
            }
        }
    }

    TRestParticleGenerator();
};

However when compiling I am getting some errors such as:

Scanning dependencies of target RestMetadata
[ 19%] Building CXX object source/metadata/CMakeFiles/RestMetadata.dir/geant4/src/TRestG4Metadata.cxx.o
[ 19%] Building CXX object source/metadata/CMakeFiles/RestMetadata.dir/__/__/rootdict/CINT_TRestG4Metadata.cxx.o
[ 20%] Building CXX object source/metadata/CMakeFiles/RestMetadata.dir/geant4/src/TRestG4PrimaryGenerator.cxx.o
[ 20%] Building CXX object source/metadata/CMakeFiles/RestMetadata.dir/__/__/rootdict/CINT_TRestG4PrimaryGenerator.cxx.o
[ 20%] Building CXX object source/metadata/CMakeFiles/RestMetadata.dir/geant4/src/TRestParticleGenerator.cxx.o
[ 21%] Building CXX object source/metadata/CMakeFiles/RestMetadata.dir/__/__/rootdict/CINT_TRestParticleGenerator.cxx.o
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘ROOT::TGenericClassInfo* ROOT::GenerateInitInstanceLocal(const std::map<std::__cxx11::basic_string<char>, const TRestParticleGenerator::energyGeneratorTypes>*)’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:120:62: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
    static TGenericClassInfo *GenerateInitInstanceLocal(const map<string,const TRestParticleGenerator::energyGeneratorTypes>*)
                                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:122:7: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       map<string,const TRestParticleGenerator::energyGeneratorTypes> *ptr = 0;
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:123:69: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       static ::TVirtualIsAProxy* isa_proxy = new ::TIsAProxy(typeid(map<string,const TRestParticleGenerator::energyGeneratorTypes>));
                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:126:26: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
                   typeid(map<string,const TRestParticleGenerator::energyGeneratorTypes>), ::ROOT::Internal::DefineBehavior(ptr, ptr),
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:128:26: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
                   sizeof(map<string,const TRestParticleGenerator::energyGeneratorTypes>) );
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:134:94: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       instance.AdoptCollectionProxyInfo(TCollectionProxyInfo::Generate(TCollectionProxyInfo::MapInsert< map<string,const TRestParticleGenerator::energyGeneratorTypes> >()));
                                                                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: At global scope:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:138:96: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
    static ::ROOT::TGenericClassInfo *_R__UNIQUE_DICT_(Init) = GenerateInitInstanceLocal((const map<string,const TRestParticleGenerator::energyGeneratorTypes>*)0x0); R__UseDummy(_R__UNIQUE_DICT_(Init));
                                                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘TClass* ROOT::maplEstringcOconstsPTRestParticleGeneratorcLcLenergyGeneratorTypesgR_Dictionary()’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:142:66: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       TClass* theClass =::ROOT::GenerateInitInstanceLocal((const map<string,const TRestParticleGenerator::energyGeneratorTypes>*)0x0)->GetClass();
                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘void* ROOT::new_maplEstringcOconstsPTRestParticleGeneratorcLcLenergyGeneratorTypesgR(void*)’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:155:67: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       return  p ? ::new((::ROOT::Internal::TOperatorNewHelper*)p) map<string,const TRestParticleGenerator::energyGeneratorTypes> : new map<string,const TRestParticleGenerator::energyGeneratorTypes>;
                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:155:136: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       return  p ? ::new((::ROOT::Internal::TOperatorNewHelper*)p) map<string,const TRestParticleGenerator::energyGeneratorTypes> : new map<string,const TRestParticleGenerator::energyGeneratorTypes>;
                                                                                                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘void* ROOT::newArray_maplEstringcOconstsPTRestParticleGeneratorcLcLenergyGeneratorTypesgR(Long_t, void*)’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:158:66: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       return p ? ::new((::ROOT::Internal::TOperatorNewHelper*)p) map<string,const TRestParticleGenerator::energyGeneratorTypes>[nElements] : new map<string,const TRestParticleGenerator::energyGeneratorTypes>[nElements];
                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:158:146: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       return p ? ::new((::ROOT::Internal::TOperatorNewHelper*)p) map<string,const TRestParticleGenerator::energyGeneratorTypes>[nElements] : new map<string,const TRestParticleGenerator::energyGeneratorTypes>[nElements];
                                                                                                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘void ROOT::delete_maplEstringcOconstsPTRestParticleGeneratorcLcLenergyGeneratorTypesgR(void*)’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:162:16: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       delete ((map<string,const TRestParticleGenerator::energyGeneratorTypes>*)p);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘void ROOT::deleteArray_maplEstringcOconstsPTRestParticleGeneratorcLcLenergyGeneratorTypesgR(void*)’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:165:19: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       delete [] ((map<string,const TRestParticleGenerator::energyGeneratorTypes>*)p);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx: In function ‘void ROOT::destruct_maplEstringcOconstsPTRestParticleGeneratorcLcLenergyGeneratorTypesgR(void*)’:
/tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:168:15: error: ‘enum TRestParticleGenerator::energyGeneratorTypes’ is private within this context
       typedef map<string,const TRestParticleGenerator::energyGeneratorTypes> current_t;
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /tmp/tmp.VIYVpNjNjf/cmake-build-debug-sultan/rootdict/CINT_TRestParticleGenerator.cxx:40:0:
/tmp/tmp.VIYVpNjNjf/source/metadata/geant4/inc/TRestParticleGenerator.h:33:10: note: declared private here
     enum energyGeneratorTypes {
          ^~~~~~~~~~~~~~~~~~~~
make[2]: *** [source/metadata/CMakeFiles/RestMetadata.dir/__/__/rootdict/CINT_TRestParticleGenerator.cxx.o] Error 1
make[2]: *** Waiting for unfinished jobs....
source/metadata/CMakeFiles/RestMetadata.dir/build.make:464: recipe for target 'source/metadata/CMakeFiles/RestMetadata.dir/__/__/rootdict/CINT_TRestParticleGenerator.cxx.o' failed
make[1]: *** [source/metadata/CMakeFiles/RestMetadata.dir/all] Error 2
make: *** [all] Error 2
CMakeFiles/Makefile2:232: recipe for target 'source/metadata/CMakeFiles/RestMetadata.dir/all' failed
Makefile:127: recipe for target 'all' failed

It seems that there is some error due to public and private members in the class. However this errors seems to be related to ROOT (I don’t really know what’s going on, it guess it’s due to the fact that I am calling this class from another TObject child). The only solution I could find is to declare every member of the class public but this is not a good practise so I am creating this post in order to understand why this is going on.

Thanks.

It is not just the enum definition that needs to be declared public?

If not, should not be the enum definition outside the class definition?

Its true that if I put the enum as a public member this works. But shouldn’t it remain “hidden” as a private member? I believe this should be possible right?

Maybe the correct approach is the store the enum outside the class in a namespace as they do for example here geant4/source/processes/hadronic/models/inclxx/utils/include/G4INCLConfigEnums.hh at db49709b532cdbe4a21775b0b28ce7fdf435e735 · Geant4/geant4 · GitHub

I believe the correct place to store this TRestEnums.h file would be under /source/tools/resttools/inc do you agree?

Yes, probably a common file containing all enum definitions inside a namespace, i.e. REST_Enums, would be the most appropriate.

Edit : Probably the best would be to have a file named TRest.h. Sometime ago I was already thinking about it. We can use this header file to define enums inside a REST_Enums namespace. But we may also use it to define other namespaces, or global definitions.

Then, TRest.h could be included in TRestMetadata.h, TRestEventProcess.h, and TRestEvent.h so that most of the framework related classes have access to it.