TH3F/TH2F Histogram generated empty

Please provide the following information:


ROOT Version (e.g. 5.34.36):
Platform, compiler (e.g. Ubuntu 14.04, gcc3.13):


I’m a beginner in root program. I have used the example tree1.C to produce my script that has the goal to read the tree (“Energy_354a.root”) and fill a new TH3F histogram with three branches of that tree. The code produced run but generated a histogram empty. Could you help me: what I did wrong?

code based on example tree1.C
void ener354a(){

	TFile *f = new TFile("Energy_354a.root");
	TTree *GmDataTTree = (TTree*)f->Get("GmDataTTree");
	Float_t Event_InitialKinecticEnergy, Event_EnergyAccumulatedLost;
	Int_t Event_EventID;

	GmDataTTree->SetBranchAddress("Event_InitialKinecticEnergy", &Event_InitialKinecticEnergy);
	GmDataTTree->SetBranchAddress("Event_EnergyAccumulatedLost", &Event_EnergyAccumulatedLost);
	GmDataTTree->SetBranchAddress("Event_EventID", &Event_EventID);
	
	// create one 3D histogram
	TH3F *ener354a = new TH3F("ener354", "Energy 0.03549MeV",1000,0.03049,0.04049,1000,0.03049,0.08049, 1000, 0, 10000);

	//read all entries and fill the histograms
	Int_t nentries = (Int_t)GmDataTTree->GetEntries();
	for (Int_t i = 0; i<nentries; i++) {
		GmDataTTree->GetEntry(i);
		ener354a->Fill(Event_InitialKinecticEnergy, Event_EnergyAccumulatedLost, Event_EventID);
	}
}

The file Energy354a.root was produced by GAMOS.5.1.0 I’ve attached this file to the link next

https://drive.google.com/file/d/1FOHF-_vCd04R7N7a7SVqjrhU_cmHBgvA/view?usp=sharing

I appreciate every help!

David

Thanks for the answer. I already make the root file public. The histogram I can’t change the size because I believe there are many data. Maybe I can improve the code to generate a histogram smaller. And I edit the comment.

Thank you!

{
  TFile *f = TFile::Open("Energy_354a.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  f->ls();
  TTree *t; f->GetObject("GmDataTTree", t);
  if (!t) { delete f; return; } // just a precaution
  t->Print();
  
  gROOT->cd();
  // create one 3D histogram
  // TH3F *h = new TH3F("ener354a", "Energy 0.03549MeV",1000,0.03049,0.04049,1000,0.03049,0.08049, 1000, 0, 10000);
  TH3F *h = new TH3F("ener354a", "Energy 0.03549MeV",
                     100, 0.03049, 0.04049,
                     100, 0.03049, 0.08049,
                     100, 0., 10000.);
  
#if 1 /* 0 or 1 */
  t->Project("ener354a", "Event_EventID:Event_AccumulatedEnergyLost:Event_InitialKineticEnergy");
#else /* 0 or 1 */
  Double_t Event_InitialKinecticEnergy, Event_EnergyAccumulatedLost;
  Int_t Event_EventID;
  
  t->SetBranchAddress("Event_InitialKineticEnergy", &Event_InitialKinecticEnergy);
  t->SetBranchAddress("Event_AccumulatedEnergyLost", &Event_EnergyAccumulatedLost);
  t->SetBranchAddress("Event_EventID", &Event_EventID);
  
  //read all entries and fill the histograms
  Long64_t n = t->GetEntries();
  for (Long64_t i = 0; i < n; i++) {
    t->GetEntry(i);
    h->Fill(Event_InitialKinecticEnergy,
            Event_EnergyAccumulatedLost,
            Event_EventID);
  }
#endif /* 0 or 1 */
  
  // h->SetDirectory(gROOT); // (gROOT) ... or ... (0)
  h->Draw();
  
  // t->ResetBranchAddresses(); // "disconnect" from local variables
  delete f; // automatically deletes "t", too
}

Thanks, Wile E. Coyote, works well.

Just so I understand: What I was doing wrong? The t->Project is used for what?

Thanks for everything!

God bless all!

Again I want to thank both for the help.

But I have one doubt if I have put in one histogram a multiple root file and normalize the histogram how should I proceed?

For example, open the files Energy_354a.root, Energy_355.root e Energy_356.root and place them in a normalize histogram TH3F. Could anyone help me?

Thanks.

{
  gROOT->cd();
  TH3F *h = new TH3F("ener35x", "Energy 0.03549MeV",
                     100, 0.03049, 0.04049,
                     100, 0.03049, 0.08049,
                     100, 0., 10000.);
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
  const char *files[] = { "Energy_354a.root",
                          "Energy_355.root",
                          // ...
                          "Energy_356.root" };
  const int n = sizeof(files) / sizeof(char*);
  for (int i = 0; i < n; i++) {
    TFile *f = TFile::Open(files[i]);
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    TTree *t; f->GetObject("GmDataTTree", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    t->Project("+ener35x", // "+" = "append data"
               "Event_EventID:Event_AccumulatedEnergyLost:Event_InitialKineticEnergy");
    delete f; // automatically deletes "t", too
  }
  // https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582
  // h->Scale(...);
  h->Draw();
}

Thanks for the help!

Now I run what you have show and place already the normalization like the example that you pass in the site, like:

{
gROOT->cd();
  TH3F *h = new TH3F("energy", "Energies",
                     100, 0.03249, 0.04049,
                     100, -1, 1.5,
                     100, 0., 10000.);
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
  const char *files[] = { "Energy_354a.root",
                          "Energy_355.root",
                          // ...
                          "Energy_356.root" };
  const int n = sizeof(files) / sizeof(char*);
  for (int i = 0; i < n; i++) {
    TFile *f = TFile::Open(files[i]);
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    TTree *t; f->GetObject("GmDataTTree", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    t->Project("+energy", // "+" = "append data"
               "Event_EventID:Event_AccumulatedEnergyLost:Event_InitialKineticEnergy");
    delete f; // automatically deletes "t", too
  }
  // https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582
  // h->Scale(...);
  Double_t scale = h->GetYaxis()->GetBinWidth(1)/(h->GetIntegral());
  h->Scale(scale);
  h->Draw();
}

When I do it this above, my histogram shows empty. What I want is that the data was normalized the energy lost with the initial energy. Example: the entry of each data of energy lost with initial energy of 0.03549MeV (Energy_354a.root) mormailized with 0.03549 and so on. Is this clear? What I have done isn’t this?

Thanks again!

Try something like:

    t->Project("+ener35x", // "+" = "append data"
               "Event_EventID:Event_AccumulatedEnergyLost:Event_InitialKineticEnergy",
               "1. / Event_InitialKineticEnergy"); // "event weight"

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

I understand what you said me about the code, thanks for notice me. I will make sure that my next posts are correct.
About the code proposed the data was normalized but I want that the result stay between 0 e 1, only the data of Event_Accumulated_Lost. Some suggestion?

Thanks

Maybe you want something like this:

    t->Project("+ener35x", // "+" = "append data"
               "Event_EventID : (Event_AccumulatedEnergyLost / Event_InitialKineticEnergy) : Event_InitialKineticEnergy");

Yes I think so. But when I do this my histogram shows empty again.

I run again corrected something that I write wrong and make right. The histogram was normalized.

Thanks again.

God bless all

Hi everyone!

Good afternoon!

I have a question about TH2F histogram. I have the code next based on the code above:

  TH2F *h = new TH2F("energy", "Energies 0.03509 a 0.13509MeV with Ti 0.05mm",
                     100, 0.2, 1.8,
                     100, 0., 10000.);
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
  const char *files[] = { "Energy_350f.root",
                          "Energy_360f.root",
                          // ...
                          "Energy_370f.root", 
			  "Energy_400f.root",
                          //...
			  "Energy_450f.root", 
			  "Energy_550f.root",
                          //...
			  "Energy_850f.root",
			  "Energy_1350f.root"};
  const int n = sizeof(files) / sizeof(char*);
  for (int i = 0; i < n; i++) {
    TFile *f = TFile::Open(files[i]);
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    TTree *t; f->GetObject("GmDataTTree", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    t->Project("+energy", // "+" = "append data"
               "Event_EventID : (Event_AccumulatedEnergyLost / Event_InitialKineticEnergy)");
    delete f; // automatically deletes "t", too
  }
  // https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582
  // h->Scale(...);
  
  h->GetXaxis()->SetTitle("ELost/EKin");
  h->GetYaxis()->SetTitle("Counts");

  h->SetLineWidth(6);
  
  h->Draw("L");

  h->GetXaxis()->SetTitleOffset(1.8);
  h->GetYaxis()->SetTitleOffset(2.0);
   
} 

How I may do an histogram like above but for each energy I have one color for each line linking the same energy points? And I need do a legend, how can I do this too?

Thanks for the help!

Maybe search for “Projections of histograms” in the TH1 class description.

Thanks, I will look in this part of the TH1 class.

With this code, I get the th2f histogram but appears in dots and I want lines besides I also want different colors for each energy read in the if loop. There are any other ideas? Maybe a for loop inside the if loop?

Thanks again!

I see the “Projections of histogram” but what I get it, isn’t exactly what I think to do.

Thanks so much any way @Wile_E_Coyote!

If you have another idea why I can’t think of a lot of things, I appreciate it.

Thanks

Unfortunately, I have no wildest idea what you want to achieve. Maybe @couet can help.

My point was that you could create some TH2 and/or TH3 histogram(s) and then use their various “Project…” and/or “Profile…” methods to “extract” some 1-D histograms (which you then can draw with any available drawing options / colours that you want).

Well, let see if I may myself clearer!

So, In this code above I read root files and select 3 histograms inside them (there is always the same name). And with the data of these 3 histograms (Event_EventID, Event_AccumulatedEnergyLost, and Event_InitialKineticEnergy) and I fill a new histogram, in this case, TH2F type. But this I think that is clear. My idea is to do a histogram TH2F in style line and where for each root files selected are attributed one color.

Is clearer now or not?

Sorry to bother you! And thanks for all attention!

Maybe you should create as many histograms as you have root files, or you could “encode” the file number as one additional histogram axis.