Re: Unable to use a class containing TLorentzVector...

From: Axel Naumann <Axel.Naumann_at_cern.ch>
Date: Sun, 02 Sep 2007 12:27:24 +0200


Hi John,

you probably exhaust your program's stack space. You request all your data to be allocated on the stack, and there is simply not enough space. Use "new" to allocate it on the heap.

Also:
* Don't use fixed-size C arrays; use std::vectors<LorentzVector> instead. * Consider creating a class that contains one entry each, and then create a vector of that class, instead of doing it the other way around. * _Please_ use ROOT I/O. Your binary I/O will break _much_ sooner than you think. E.g. when we change TObject (not that we will, but we could) you will be unable to read any of your data back. With ROOT I/O this can't happen. There are plenty more examples of how and when your primitive binary I/O will break (32 vs. 64 bit, different OS, different compiler...)

Cheers, Axel.

John Zoidberg wrote:
> I created a class called lotsofdata containing arrays of ints and
> TLorentzvectors.
> For some strange reason, whenever I try to initialize a variable with
> that class, the macro exits ROOT directly without any message.
>
> Here is a simplified version of the problem:
>
> #define MAXBGD 40000
>
> #include <fstream>
> #include <iostream>
> #include "TLorentzVector.h"
> using namespace std;
>
> class lotsofdata
> {
> public:
> TLorentzVector PlBg[MAXBGD];
> TLorentzVector MinBg[MAXBGD];
> int PlBg_PDG[MAXBGD];
> int MinBg_PDG[MAXBGD];
> int PlBg_mamaPDG[MAXBGD];
> int MinBg_mamaPDG[MAXBGD];
>
> public:
> lotsofdata(void);
> ~lotsofdata(void);
> int Save(char* filename); /**< save class as binary */
> int Load(char* filename); /**< load class from binary */
>
> //ClassDef(lotsofdata,1); //special for ROOT
> };
>
> //ClassImp(lotsofdata); //special for ROOT
>
> //constructor
> lotsofdata::lotsofdata()
> {}
>
> //destructor
> lotsofdata::~lotsofdata()
> {}
>
> //save class
> int lotsofdata::Save(char* filename)
> {
> cout<<"SAVING AS BINARY"<<endl;
> ofstream ofs(filename, ios::binary);
> ofs.write((char *)this, sizeof(lotsofdata));
> return(0);
> }
>
> //load class
> int lotsofdata::Load(char* filename)
> {
> cout<<"LOADING FROM BINARY"<<endl;
> ifstream ifs(filename, ios::binary);
>
> // if fstream cannot open file
> if ( !ifs )
> {
> cerr << "WARNING: File could not be opened." << endl;
> return(1);
> } // end if
>
> ifs.read((char *)this, sizeof(lotsofdata));
> return(0);
> }
>
> void mystery(void)
> {
> lotsofdata titi;
> }
>
> I compile it with .L mystery.C+ and then run mystery().
>
> If the TLorentzVector lines are commented, I stay inside ROOT, if not,
> it exits without any message (even if I add cout or printf before
> initializing titi).
>
> What's the problem with including TLorentzVectors in a class and how can
> I solve it?
>
Received on Sun Sep 02 2007 - 12:27:37 CEST

This archive was generated by hypermail 2.2.0 : Sun Sep 02 2007 - 17:50:01 CEST