Convert a TH2F histogram to TH1F from the command line


ROOT Version: 5.34.36
_Platform:_UBUNTU 16.04
Compiler: Not Provided


Good Morning! When I run a file in GAMOS, a root file was generated (attached)testSIConExGALAIR35_tree_1000.root.tar.gz (1.2 MB).

So in this file there is a tree with some branches constructed in TH1F. In the command line I created a hist whith some branches (difference the KE(final and initial) per distance (using the eq: sqrt(sqrt(pow((Step_FinalPosX),2)+pow((Step_FinalPosY),2)+pow((Step_FinalPosZ),2)))) that generated a TH2F histogram. So, How can I selected a region of the distance and I generated a TH1F of difference KE?

I don’t know if I was clear, it’s possible understand?

Thanks!

can you post the exact TTree command you apply on the tree (testSIConExGALAIR35) ?

This is the command:

root -x testSIConExGALAIR35_tree_100.root

Later:

testSIConInGALAIR35->Draw("(Step_InitialKineticEnergy-Step_FinalKineticEnergy):(sqrt(pow((Step_FinalPosX),2)+pow((Step_FinalPosY),2)+pow((Step_FinalPosZ),2)))","","hist C");

Thanks

I’ doing this code in c++:

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TROOT.h"
#include "TBranch.h"
#include "TH1.h"
#include "TH2.h"
#include "Rtypes.h"


void test()
{
  TFile *f = TFile::Open("testSIConInGALAIR35_tree_1000.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  f->ls();
  TTree *t; f->GetObject("testSIConInGALAIR35", t);
  if (!t) { delete f; return; } // just a precaution
  t->Print();
  
  gROOT->cd();
  // create one 3D histogram

  TH2F *h = new TH2F("EXD", "Energy versus distance from the center",
                     100, 0.0, 13.,
                     100, 0., 0.03849);
  
#if 1 /* 0 or 1 */
  //Double_t SQ = sqrt(pow((Step_FinalPosX),2)+pow((Step_FinalPosY),2)+pow((Step_FinalPosZ),2));
  //Double_t delta_KIN = (Step_InitialKineticEnergy-Step_FinalKineticEnergy);
  t->Project("EXD", "(Step_InitialKineticEnergy-Step_FinalKineticEnergy):(sqrt(pow((Step_FinalPosX),2)+pow((Step_FinalPosY),2)+pow((Step_FinalPosZ),2)))");
#else /* 0 or 1 */
  Double_t Step_InitialKineticEnergy, Step_FinalKineticEnergy, Step_FinalPosX, Step_FinalPosY, Step_FinalPosZ;
  
  t->SetBranchAddress("Step_InitialKineticEnergy", &Step_InitialKineticEnergy);
  t->SetBranchAddress("Step_FinalKineticEnergy", &Step_FinalKineticEnergy);
  t->SetBranchAddress("Step_FinalPosX", &Step_FinalPosX);
  t->SetBranchAddress("Step_FinalPosY", &Step_FinalPosY);
  t->SetBranchAddress("Step_FinalPosZ", &Step_FinalPosZ);
 
  
  //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(Step_InitialKineticEnergy,
            Step_FinalKineticEnergy,
	    Step_FinalPosX,
	    Step_FinalPosY,
	    Step_FinalPosZ
           );
  }
#endif /* 0 or 1 */
  // This is to selected for each bin of X the sum of the values in Y and then draw it
  int n2 = GetBin();
   for(int i=0;i<n2;i++){
       float y[i] = h->sum(GetYAxis(i));
	for(int j=0;j<n2;j++)){
	  float y1[i]= y1[i] + y[i]; 
  	}
   }
  // h->SetDirectory(gROOT); // (gROOT) ... or ... (0)
  //h->Draw("hist C");
  h->Draw("hist C");
  //TProfile *h1 = h->ProfileY("EXD_1D", 2, 97); // where firstXbin = 0 and lastXbin = 9
  //h1->Draw("hist C");
  
  // t->ResetBranchAddresses(); // "disconnect" from local variables
  delete f; // automatically deletes "t", too
}

But it is an error always in the function GetBin().

Thanks

Try:

TH1D *h_px = h->ProjectionX();
h_px->Draw();

Thanks @Wile_E_Coyote! The graphic that I obtain was: enerxd.pdf (14.2 KB)

But what I wanted is a graphic Energy versus distance ( (Step_InitialKineticEnergy-Step_FinalKineticEnergy):(sqrt(pow((Step_FinalPosX),2)+pow((Step_FinalPosY),2)+pow((Step_FinalPosZ),2)))) in TH1F.

Cheers

Well, “some_y : some_x” means a 2-dimensional histogram (and that is your “h”).

Yes you are right!

What I meant is: Is possible convert the TH2F obtain in TH1F in the root? Or obtain this TH2F in lines instead of a histogram of points?

Thanks for the help!

It seems you miss the description of drawing options supported for 2D histograms.

Thanks @Wile_E_Coyote!

I 'm really having forgotten.

May I ask other question: I need is to for each bin of the X axis I want to calculated the sum of the values in Y and then put this in a new Th1f histogram, how could I do it?

Best Regards!

How could I selected a range in X and see the graphic with the corresponding in Y in the histogram th2f?

// Note: you can "reset" the range when you set xmin >= xmax
h->GetXaxis()->SetRangeUser(xmin, xmax);
h->Draw();

Thanks @Wile_E_Coyote!

How could I selected for each bin in X the sum of every value of Y? I think in something like this:

   for(Float_t i=0;i<100;i++){
       Float_t Esutmp = 0;
       Float_t jmax = h->GetEntry(i);
     for(Float_t j=0;j<jmax;j++)){       
        Esutmp += h->GetBinContent(i);
      }
   hnew->SetBinContent(i,Esutmp);
   }

That is ok? Because the result when I draw give me:

Error: Can’t call TH2F::GetEntry(i) in current scope test.cpp:60:
Possible candidates are…
(in TH2F)
(in TH2)
(in TH1)

Cheers

h_px->Print("all"); // "h_px" from my previous post here

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.