[SOLVED] Fill a TClonesArray

Hello, I’m trying to fill a TClonesArray, I’ve looked for a lot of examples, and now I don’t know where could be the error.

I get this error during compilig the attached macro:

/simulation.C:184:21: error: expected a type new(hits1[j]) Hits1(xDir,yDir,zDir); ^ /simulation.C:192:21: error: expected a type new(hits2[j]) Hits2(xDir,yDir,zDir); ^

Thank you for your help.
simulation.C (7.13 KB)

Try:
root-config --cxx --cflags -O2 -W -Wall -c simulation.C

See also:
http://root.cern.ch/root/html/TClonesArray.html
http://www.cplusplus.com/reference/std/new/operator%20new/
http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/

Note: both, “Hits1” and “Hits2”, should be class names, I believe (from the source code that you posted, it’s not clear where these classes are defined). That’s why the compiler “expected a type” in both these places.

Thanks for the reply. Now my macro’s compiling, but when I run it I obtains a Segmentation violation with this error (I’ll attach the new version of the macro):

[code]root [1] simulation()
Error in TClonesArray::SetClass: called with a null pointer
Error in TClonesArray::SetClass: called with a null pointer
Error in TTree::Bronch: TClonesArray with no class defined in branch: Hits1
Error in TTree::Bronch: TClonesArray with no class defined in branch: Hits2
Error in TClonesArray::operator[]: invalid class specified in TClonesArray ctor

*** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:

Thread 1 (process 1325):
#0 0x00007fff8d5f86ac in wait4 ()
#1 0x00007fff8b32203a in system ()
#2 0x00000001051866af in TUnixSystem::StackTrace ()
#3 0x00000001051844f7 in TUnixSystem::DispatchSignals ()
#4
#5 0x00000001050dbf5b in TObject::TObject ()
#6 0x0000000106f4241e in Direction::Direction ()
#7 0x0000000106f423e3 in Direction::Direction ()
#8 0x0000000108659e80 in simulation ()
#9 0x000000010865ae9c in G__simulation_C_ACLiC_dict__0_1970 ()
#10 0x000000010596efbf in Cint::G__ExceptionWrapper ()
#11 0x0000000105a1eb5a in G__execute_call ()
#12 0x0000000105a1f13b in G__call_cppfunc ()
#13 0x00000001059f3623 in G__interpret_func ()
#14 0x00000001059dd59f in G__getfunction ()
#15 0x00000001059d195a in G__getitem ()
#16 0x00000001059d0ae6 in G__getexpr ()
#17 0x0000000105a50ccc in G__exec_statement ()
#18 0x00000001059b3268 in G__exec_tempfile_core ()
#19 0x00000001059b2e26 in G__exec_tempfile_fp ()
#20 0x0000000105a5c9da in G__process_cmd ()
#21 0x000000010514c3e8 in TCint::ProcessLine ()
#22 0x00000001050b63b5 in TApplication::ProcessLine ()
#23 0x0000000106099cea in TRint::HandleTermInput ()
#24 0x00000001060983a7 in TTermInputHandler::Notify ()
#25 0x000000010609a55d in TTermInputHandler::ReadNotify ()
#26 0x00000001051839c6 in TUnixSystem::CheckDescriptors ()
#27 0x0000000105183211 in TUnixSystem::DispatchOneEvent ()
#28 0x000000010510d4aa in TSystem::InnerLoop ()
#29 0x000000010510d2dd in TSystem::Run ()
#30 0x00000001050b6d34 in TApplication::Run ()
#31 0x00000001060996a0 in TRint::Run ()
#32 0x00000001050ada5f in main ()

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug 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.

#5 0x00000001050dbf5b in TObject::TObject ()
#6 0x0000000106f4241e in Direction::Direction ()
#7 0x0000000106f423e3 in Direction::Direction ()
#8 0x0000000108659e80 in simulation ()
===========================================================[/code]
simulation.C (7.14 KB)

What was in the line “root [0]”?

It seems to me that CINT still does not know what “Hits1” and “Hits2” are (you need to create ROOT-CINT dictionary entries for both of these classes, if you want to have them in a “TClonesArray”, I believe -> lines 80 and 82 in your new “simulation.C” file need them).

This is the line root[0]:

root [0] .x compile.C Info in <TUnixSystem::ACLiC>: creating shared library /Users/cheip/Dropbox/examTANS/exam/simulation/VertexSimulator_cxx.so Info in <TUnixSystem::ACLiC>: creating shared library /Users/cheip/Dropbox/examTANS/exam/simulation/PMultiplicity_cxx.so Info in <TUnixSystem::ACLiC>: creating shared library /Users/cheip/Dropbox/examTANS/exam/simulation/Direction_cxx.so Info in <TUnixSystem::ACLiC>: creating shared library /Users/cheip/Dropbox/examTANS/exam/simulation/simulation_C.so

compile.C is attached.
compile.C (316 Bytes)

Try:
root [0] .x compile.C
root [1] .class Hits1
root [2] .class Hits2
If you do not get details of both classes, then you need to call “CompileMacro” for both of them (e.g. for “Hits1.cxx” and “Hits2.cxx”, or any other source code file that defines them).

Ok, now I’ve solved! Hits1 and Hits2 aren’t classes, I’ve changed the name in the TClonesArray declaration!

Thank you for your help!!! :smiley:

I assume you changed the lines 80 and 82 of your new “simulation.C” file (I guess you changed both, “Hits1” and “Hits2”, into “Direction” -> that was the change that you did in the first place, in order to get the source code “compiled”).
Note: these are not “TClonesArray declarations”. These are calls to the “TClonesArray constructor” (and you actually also “define” two new variables, “hits1” and “hits2”, in these lines -> you do not just “declare” them).

BTW. If you want to mark your topic as “[color=#00CC00]SOLVED[/color]”, you do not need to change its “[color=#425067]Subject:[/color]”. When you read your topic, you should see a small green button “[color=#00CC00]/[/color]” (called “Accept this answer”, next to “[color=#BF0000]EDIT[/color]”) which can mark this topic as “[color=#00CC00]SOLVED[/color]”.