A two dimensional histogram

Hello!

I am trying to produce a two dimensional histogram.
It is booked as shown below.

#################
void qghbb::book() {
theFile = new TFile(“ndim.root”,“RECREATE”);
theFile->cd();
h2 = new TH2F(“h2”, “MD vs OpA”, 100, -1., 1., 100, 0., 1000.);
}
#################

And then it is filled by, h2->Fill(de1, de2);
There is no compiler error.
But it crashed exactly at this line during runtime.
The root file “ndim.root” is not created either.
Can anybody give me some hint to solve this problem?

Cheers, Sangryul Ro

Send a small macro reproducing the problem or/and look at the many examples doing that in $ROOTSYS/tutorials, for instance hsimple.C.

The source code is embeded in a large program.
And it is difficult to make a simple macro file.
It would be nice if one can point out an obvious or possible mistake
in the code quoted.
The example file is quite useful though.

Cheers, Sangryul Ro

have a look at hsimple.C, it does exactly what you want and is less than 50 lines long.

The line,
TFile theFile = (TFile)gROOT->FindObject(“ndim.root”);
seems to be the only place of suspect.
When I used this line instead of “TFile* theFile;” in my program,
there was an error message shown below.

###############################################
/afs/cern.ch/user/s/sro/sw/ORCA_8_7_1/src/Workspace/qgbbh.cpp:175: invalid in-class initialization of static data member of non-integral type `TFile*’

How can I implement this line in my C++ source code?

S.

Ok, … here is a working example:

{
  hfile = new TFile("hsimple.root","RECREATE","Demo ROOT file with histograms");

  hpxpy  = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);


// Fill histograms randomly
  gRandom->SetSeed();
  Float_t px, py, pz;
  const Int_t kUPDATE = 1000;
  for (Int_t i = 0; i < 25000; i++) {
     gRandom->Rannor(px,py);
     pz = px*px + py*py;
     Float_t random = gRandom->Rndm(1);
     hpxpy->Fill(px,py);
  }

  hfile->Write();
}

Thanks for the preparation of the sample program.
But it still has problem.
My program is the following.

########################
class qghbb : public EventAnalyser {

private:

void upDate(G3EventProxy* ev) {
if (ev != 0) analysis(ev);
}
void book(); //!< book the histograms
void hend(); //!< save the histograms to file
TFile* theFile;
TH2F *h2;
};

void qghbb::book() {
theFile = new TFile(“ndim.root”,“RECREATE”, “Demo ROOT file with histograms”);
// theFile->cd();
h2 = new TH2F(“h2”, “The Higgs Momentum Distribution as a
Function of the Opening Angle”, 100, -1., 1., 100, 0., 1000.);
}
void qghbb::hend() {
// theFile->cd();
theFile->Write();
theFile->Close();
}

void qghbb::analysis(G3EventProxy* ev) {

h2->Fill(de1, de2);

}
###############

The de1 was 0.64 and the de2 was 68.80.
But it crashed at the last line.
Is there anything wrong?
S.

That’s really difficult for me to help you more without a running example. I do not see anything wrong in the few lines you sent. May be somebody else in the forum will see the problem just looking at them … Personally I don’t…
Cheers, Olivier

I hope that anybody give me a slightest hint for this problem.
S.

Please provide a short but running test case reproducing the problem.

Rene

Hi Sangryul Ro,

How on earth do you expect somebody to debug your problem
when you only show a snippet of your code ??

Which version of ROOT ??

Some observations on your code :

  • constructor does NOT initialize its data members

  • a destructor is also not to be seen in miles . You should have one
    with lines like :

    if (theFile) delete theFile;

  • before analysis is called, are you sure that “book” is called
    so that h2 is set ?

Eddy

Hi

Before I go on, please remember I’m used to C, not C++, so things about classes are a bit foreign for me. Anyway, here goes.

In this snippet

void qghbb::analysis(G3EventProxy* ev) {

    h2->Fill(de1, de2);

}

it seems that, unless de1 and de2 are global variables, then it should not know what values they have. But since they are not pointer, there should be no problem…

  1. Have you tried compiling the code with de1 and de2 replaced with actual numeric values (like 0.68 and 62.39) ? Does it still crash?

  2. Have you tried doing some stuff with the .root file? Like a file->Print() for example?

  3. Remember to close the file when you no longer need it… It’s standard C procedure and perhaps ROOT’s C++ is intelligent enough to deal with it, but you should create good habits.

Hope this helps.

This is a good point.
It once occurred to me to use numerical values for the de1 and de2.
But I didn’t try because the values should be changing each time
in the loop.
When I used the numerical values, it still crashed on the same line.
I haven’t tried doing any stuff with the .root file except filling a root tree
for one dimensional histograms only.
The root tree is successfully filled and resulted proper one dimensional histograms.

Cheers, Sangryul Ro