Macro files while executed, while copying line by line in prompt works

Dear all,
I have a script - in attachment - which fails as below when run.
If instead I open a root session and I copy line by line the commands everything works.

Any insight will be greatly appreciated.

Best regards,
Marco Bomben
prepareTree.C (366 Bytes)

root [0] .x prepareTree.C 
/eos/home-m/mbomben/Digitizer/ITk/allpix_squared_simulations/DepositionPointChargeTests_irradiated_magnetic/./prepareTree.C:7:3: error: Syntax error
  auto tree = RadDamVarTree(file0, "detector1");
  ^
FunctionDecl 0x29e2108 <input_line_8:1:1, /eos/home-m/mbomben/Digitizer/ITk/allpix_squared_simulations/DepositionPointChargeTests_irradiated_magnetic/./prepareTree.C:12:1> input_line_8:1:6 __cling_Un1Qu30 'void (void *)'
|-ParmVarDecl 0x29e2050 <col:22, col:28> col:28 vpClingValue 'void *'
|-CompoundStmt 0x2b4b5b0 <col:42, /eos/home-m/mbomben/Digitizer/ITk/allpix_squared_simulations/DepositionPointChargeTests_irradiated_magnetic/./prepareTree.C:12:1>
| |-DeclStmt 0x2b42918 <line:3:3, col:64>
| | `-VarDecl 0x29ec770 <col:3, col:63> col:10 used file0 'TFile *' cinit
| |   `-CXXNewExpr 0x2b428d8 <col:18, col:63> 'TFile *' CXXMethod 0x2b40af0 'operator new' 'void *(size_t)'
| |     `-CXXConstructExpr 0x2b423f0 <col:22, col:63> 'TFile' 'void (const char *, Option_t *, const char *, Int_t)'
| |       |-ImplicitCastExpr 0x2b42378 <col:28> 'const char *' <ArrayToPointerDecay>
| |       | `-StringLiteral 0x29ec858 <col:28> 'const char [34]' lvalue "output/trees_100um_4e15_600V.root"
| |       |-CXXDefaultArgExpr 0x2b42390 <<invalid sloc>> 'const char *'
| |       |-CXXDefaultArgExpr 0x2b423b0 <<invalid sloc>> 'const char *'
| |       `-CXXDefaultArgExpr 0x2b423d0 <<invalid sloc>> 'Int_t':'int'
| |-CXXMemberCallExpr 0x2b4a2a8 <line:4:3, col:121> 'int'
| | |-MemberExpr 0x2b4a180 <col:3, col:12> '<bound member function type>' ->Load 0x2b49d88
| | | `-ImplicitCastExpr 0x2b49d68 <col:3> 'TSystem *' <LValueToRValue>
| | |   `-DeclRefExpr 0x2b49d48 <col:3> 'TSystem *' lvalue Var 0x2b42938 'gSystem' 'TSystem *'
| | |-ImplicitCastExpr 0x2b4a2e0 <col:17> 'const char *' <ArrayToPointerDecay>
| | | `-StringLiteral 0x2b4a228 <col:17> 'const char [103]' lvalue "/cvmfs/clicdp.cern.ch/software/allpix-squared/2.3.2/x86_64-centos7-clang12-opt/lib/libAllpixObjects.so"
| | |-CXXDefaultArgExpr 0x2b4a2f8 <<invalid sloc>> 'const char *'
| | `-CXXDefaultArgExpr 0x2b4a318 <<invalid sloc>> 'Bool_t':'bool'
| |-CXXMemberCallExpr 0x2b4ad08 </cvmfs/sft.cern.ch/lcg/releases/ROOT/6.24.06-a63a9/x86_64-centos7-clang12-opt/include/TROOT.h:406:15, /eos/home-m/mbomben/Digitizer/ITk/allpix_squared_simulations/DepositionPointChargeTests_irradiated_magnetic/./prepareTree.C:5:39> 'Int_t':'int'
| | |-MemberExpr 0x2b4ac28 </cvmfs/sft.cern.ch/lcg/releases/ROOT/6.24.06-a63a9/x86_64-centos7-clang12-opt/include/TROOT.h:406:15, /eos/home-m/mbomben/Digitizer/ITk/allpix_squared_simulations/DepositionPointChargeTests_irradiated_magnetic/./prepareTree.C:5:10> '<bound member function type>' ->LoadMacro 0x2b4a7d8
| | | `-ParenExpr 0x2b4a7b0 </cvmfs/sft.cern.ch/lcg/releases/ROOT/6.24.06-a63a9/x86_64-centos7-clang12-opt/include/TROOT.h:406:15, col:31> 'TROOT *'
| | |   `-CallExpr 0x2b4a790 <col:16, col:30> 'TROOT *'
| | |     `-ImplicitCastExpr 0x2b4a778 <col:16, col:22> 'TROOT *(*)()' <FunctionToPointerDecay>
| | |       `-DeclRefExpr 0x2b4a720 <col:16, col:22> 'TROOT *()' lvalue Function 0x2b4a358 'GetROOT' 'TROOT *()'
| | |-ImplicitCastExpr 0x2b4ad40 </eos/home-m/mbomben/Digitizer/ITk/allpix_squared_simulations/DepositionPointChargeTests_irradiated_magnetic/./prepareTree.C:5:20> 'const char *' <ArrayToPointerDecay>
| | | `-StringLiteral 0x2b4acd8 <col:20> 'const char [18]' lvalue "RadDamVarTree.C++"
| | |-CXXDefaultArgExpr 0x2b4ad58 <<invalid sloc>> 'Int_t *'
| | `-CXXDefaultArgExpr 0x2b4ad78 <<invalid sloc>> 'Bool_t':'bool'
| |-DeclStmt 0x2b4b160 <line:6:3, col:64>
| | `-VarDecl 0x2b4add8 <col:3, col:63> col:8 file 'TFile *':'TFile *' cinit
| |   `-CXXNewExpr 0x2b4b078 <col:15, col:63> 'TFile *' CXXMethod 0x2b40af0 'operator new' 'void *(size_t)'
| |     `-CXXConstructExpr 0x2b4b030 <col:19, col:63> 'TFile' 'void (const char *, Option_t *, const char *, Int_t)'
| |       |-ImplicitCastExpr 0x2b4afc0 <col:25> 'const char *' <ArrayToPointerDecay>
| |       | `-StringLiteral 0x2b4aeb8 <col:25> 'const char [25]' lvalue "outputRadDamVarTree.root"
| |       |-ImplicitCastExpr 0x2b4afd8 <col:53> 'const char *' <ArrayToPointerDecay>
| |       | `-StringLiteral 0x2b4af68 <col:53> 'const char [9]' lvalue "RECREATE"
| |       |-CXXDefaultArgExpr 0x2b4aff0 <<invalid sloc>> 'const char *'
| |       `-CXXDefaultArgExpr 0x2b4b010 <<invalid sloc>> 'Int_t':'int'
| |-DeclStmt 0x2b4b4e0 <line:7:3, col:48>
| | `-VarDecl 0x2b4b190 <col:3, col:47> col:8 used tree 'auto' cinit
| |   `-CallExpr 0x2b4b490 <col:15, col:47> '<dependent type>'
| |     |-DeclRefExpr 0x2b4b368 <col:15> '<dependent type>' lvalue Var 0x2b4b2d0 'RadDamVarTree' '<dependent type>'
| |     |-DeclRefExpr 0x2b4b3a8 <col:29> 'TFile *' lvalue Var 0x29ec770 'file0' 'TFile *'
| |     `-StringLiteral 0x2b4b468 <col:36> 'const char [10]' lvalue "detector1"
| |-CallExpr 0x2b4b580 <line:8:3, col:15> '<dependent type>'
| | `-CXXDependentScopeMemberExpr 0x2b4b538 <col:3, col:9> '<dependent type>' lvalue ->Write
| |   `-DeclRefExpr 0x2b4b4f8 <col:3> 'auto' lvalue Var 0x2b4b190 'tree' 'auto'
| |-NullStmt 0x2b4b5a0 <line:10:1>
| `-NullStmt 0x2b4b5a8 <line:11:1>
|-AnnotateAttr 0x2b4b268 <<invalid sloc>> R"ATTRDUMP(__ResolveAtRuntime)ATTRDUMP"
`-AnnotateAttr 0x2b4b338 <<invalid sloc>> R"ATTRDUMP(__ResolveAtRuntime)ATTRDUMP"
<<<NULL>>>

ROOT Version: 6.24/06
Platform: CentOS7
Compiler: clang version 12.0.0


Try something like this:

TTree *RadDamVarTree(TFile *, const char *);

void prepareTree()
{ 
  TFile* file0 = new TFile("output/trees_100um_4e15_600V.root");
  gSystem->Load("/cvmfs/clicdp.cern.ch/software/allpix-squared/2.3.2/x86_64-centos7-clang12-opt/lib/libAllpixObjects.so");
  gROOT->LoadMacro("RadDamVarTree.C++");
  auto file = new TFile("outputRadDamVarTree.root", "RECREATE");
  auto tree = RadDamVarTree(file0, "detector1");
  tree->Write();
} 

Thanks for your feedback but that did not solve the problem.
But I have found a solution:

#include "RadDamVarTree.C"

before:

void prepareTree(){

and comment the line:

//gROOT->LoadMacro("RadDamVarTree.C++");

So now the macro is:

#include <Math/DisplacementVector2D.h>
#include <Math/Vector2D.h>
#include <Math/Vector3D.h>
#include <TFile.h>
#include <TTree.h>

#include <memory>

#include <fstream>
#include "TSystem.h"
#include "TROOT.h"


#include "RadDamVarTree.C"

//std::shared_ptr<TTree> RadDamVarTree(TFile* file, std::string dut);

void prepareTree(){
  TFile* file0 = new TFile("output/trees_100um_4e15_600V.root");
  gSystem->Load("/cvmfs/clicdp.cern.ch/software/allpix-squared/2.3.2/x86_64-centos7-clang12-opt/lib/libAllpixObjects.so");
  //gROOT->LoadMacro("RadDamVarTree.C++");
  //gSystem->Load("RadDamVarTree_C.so");
  auto file = new TFile("outputRadDamVarTree.root", "RECREATE");
  std::shared_ptr<TTree>  tree = RadDamVarTree(file0, "detector1");
  tree->Write();
}

and I can call it inside an unnamed script to run it:

{
  gROOT->Reset();
  gROOT->LoadMacro("prepareTree.C++");
  prepareTree();
}

So I will mark this solved.

Regards,
Marco Bomben

1 Like