Segmentation Error

I get an error ; break segmentation. Why ? It doesn’t work ! ](*,)

Cheers

EventData.h script is

[code]#include

class Particle {
public:
Particle() {memset(fTags, 0, sizeof(fTags)); }
double fPosX,fPosY,fPosZ; // particle position nearest to interaction point
double fMomentum; // particle momentum
double fMomentumPhi; // particle direction (phi)
double fMomentumEta; // particle direction (eta)
Long64_t fTags[128]; // particle tags
};

class EventData {
public:
std::vector fParticles; // particles of the event
int fEventSize; // size (in bytes) of the event

void SetSize() {
fEventSize = sizeof(EventData) + fParticles.size() * sizeof(Particle);
}
void Clear() {
fParticles.clear();
}
void AddParticle(const Particle& p) { fParticles.push_back§; }

//ClassDef(EventData,1); // Data for an event
};

#ifdef MAKECINT
#pragma link C++ class Particle+;
#pragma link C++ class EventData+;
#endif[/code]

Macro script is

[code]// Prefer compiled:
#include “TTree.h”
#include “TFile.h”
#include “TRandom.h”
#include “TMath.h”
#include

#include “EventData.h”

void createTree(ULong64_t numEvents = 200) {
TFile* f = new TFile(“eventdata.root”, “RECREATE”);
TTree* tree = new TTree(“EventTree”, “Tutorial tree”);

EventData* event = new EventData();
tree->Branch(“event”, &event);

Particle p;

for (ULong64_t i = 0; i < numEvents; ++i) {
event->Clear();

  // generate event information
  int nParticles = 10 * gRandom->Exp(10.);

  // for every particle in the event generate the particle data 
  for (int ip = 0; ip < nParticles; ++ip) {
     do {
        p.fPosX = gRandom->Gaus(gRandom->PoissonD(0.1), 1.)
           + gRandom->BreitWigner(0.1, 0.1);;
     } while (fabs(p.fPosX) > 10.);
     p.fPosY = gRandom->Gaus(gRandom->PoissonD(0.01), .7);
     p.fPosZ = gRandom->Gaus(gRandom->PoissonD(10), 19.);

     p.fMomentum = gRandom->Exp(12);
     p.fMomentumPhi = gRandom->Uniform(2*TMath::Pi());
     do {
        p.fMomentumEta = gRandom->BreitWigner(0.01, 10.);
     } while (fabs(p.fMomentumEta) > 12.);

     
     event->AddParticle(p);
  }
  event->SetSize();

  tree->Fill();

  if (i % (numEvents/50) == 0) {
     printf("*");
     fflush(stdout);
  }

}
printf("\n");
tree->Write();
delete f;
}[/code]

At first look I can see nothing really wrong.
I tried two approaches: root [0] .x createTree.C++ Info in <TUnixSystem::ACLiC>: creating shared library /..././createTree_C.so ************************************************** root [0] .L EventData.h++ Info in <TUnixSystem::ACLiC>: creating shared library /..././EventData_h.so root [1] .x createTree.C **************************************************
You should add two lines in the “EventData.h”:
#include // for memset
and then (right after “#pragma link C++ class Particle+;”):
#pragma link C++ class std::vector+;

Similarly, in the “createTree.C” you should add:
#include // for fabs
#include // for printf and fflush
and then right before you return in the end, you should:
delete event;

I have tried your opinion ,adding thoose two lines , but it still doesn’t work.
Yes, as you said, this problem have arisen from “not delete pointers in heap” . Although I tried that, it still doesn’t work .

There is the same error again !

Have you tried to repeat my steps EXACTLY (note that “++” is not redundant)?
If yes, what ROOT version and on what operating system are you running?

BTW. You should also protect your “EventData.h”: #if !defined(___EventData_h___) #define ___EventData_h___ // ... the current contents of EventData.h comes here ... #endif /* !defined(___EventData_h___) */

1 Like

Ok.Done !

Thank you very much…

P.S. “++” is required with ROOT 5 (Philippe would need to say why “EventData.h” needs to be precompiled) but it may not be needed with ROOT 6.