Home | News | Documentation | Download

Fill a histogram

Hello guys,
Here I have a code, I wanna create a histogram but I can’t. What’s the matter?

Tips for Efficient and Successful Posting

[Badom.root (1.7 MB)

#include <array>
#include <string>
#include <vector>
#include <math.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include "TApplication.h"
#include "TNtuple.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TH1D.h"
#include "TFile.h"
#include "TLeaf.h"


using namespace std;


	void ReadFromNTTree(){
	const int NRSamples = 249900;
	const int NrNSamplesPerEvent = 4998;
	const int NREvents = NRSamples / NrNSamplesPerEvent;
	int Index = 0;
	const int Baseline_Sample = 50;
	int threshold = 1;
	int timestamp = 0;
	int Gate = 4500;
	int Pregate = 2;
	int num = 0;
	int Num = 0;
	int NUM = 0;
	double t = 1;
	double T = 0.05;
	double Q1 = 0.0;
	double Q2 = 0.0;
	double Q = 0.0;

	double FinalData[NREvents][NrNSamplesPerEvent];


	TFile* MyFile = new TFile("Badom.root");
	TTree* TREE = (TTree*)MyFile->Get("MY TREE");
	
	double Fdata = 0;

	TREE->SetBranchAddress("FDATA", &Fdata);

	TCanvas *Canvas = new TCanvas("Canvas", "Histogram");
	TH1D *EventHisto1 = new TH1D("EventHisto1", "F Histogram", 10.0, 0.0, 750.0);

	
	cout << "Analyzing file ... " << endl << '\n';

	Long64_t NEntries = TREE->GetEntries();
	Long64_t EVENT_it = 0, SAMPLE_it = 0;
	cout << "NrEntries: " << NEntries << endl;
	int num = 0;
	for (Long64_t j = 0; j < NEntries; j++) {
		Index++;
		TREE->GetEntry(j);
		FinalData[EVENT_it][SAMPLE_it] = Fdata;
		SAMPLE_it += 1;
		//cout <<"Event is : "<<'\t'<< EVENT_it <<'\t'<<"sample is : "<<'\t'<< SAMPLE_it <<'\t'<<"data is:"<<'\t'<< Fdata << endl;
		if (Index == NrNSamplesPerEvent) {
			Index = 0;
			EVENT_it += 1; SAMPLE_it = 0;
			cout << "****************** Event Nr is: " << EVENT_it << "*********************" << endl;

			for (int sam1 = 0; sam1 < NrNSamplesPerEvent; sam1++) {

				if (FinalData[EVENT_it-1][sam1]) > threshold) {

				timestamp = sam1;

				break;
				}//if (FinalData[NREvents][NrNSamplesPerEvent][sam1]) > threshold)

			}//for (int sam1 = 0; sam1 < NrNSamplesPerEvent; sam1++)

			if (timestamp <= Pregate)
				Pregate = timestamp;

			for (int sam2 = timestamp - Pregate; sam2 < Gate; sam2++) {


				Q1 += (FinalData[EVENT_it-1][sam2]) * 2;
				Q2 += (FinalData[EVENT_it-1][sam2 + 1]) * 2;
				
			}//for(int sam8=0 ; sam8<NrSamples; sam8++)
				num++;
			Q = (Q1 + Q2) / 2;
			EventHisto1->Fill(Q);
		   }//if (Index == NrNSamplesPerEvent)
			 //TGraph *plot = new TGraph("output1.txt");
	}//for (Long64_t i = 0; i<nentries; i++)
	

	Canvas->Divide();
	Canvas->cd();
	EventHisto1->Draw();
	Canvas->Update();
	
	}//void ReadFromTTree()

What error/s do you get?

Hi Fateme,

When you create a new topic we show you a couple of helpful hints. You seem to have missed those. See for instance Tips for Efficient and Successful Posting

And yes, we need to know how “I can’t” shows in practice - what’s the error you get?

Cheers, Axel.

The problem is that what I can see is just the Canvas without any histogram!

The code you posted doesn’t run (redefinition of int num and one extra parenthesis).
Anyway, once running if you add this

  cout << "Filling with "<<Q<<endl;

just before the histogram fill, you’ll see that Q is huge (order 1e+7), but your histogram only goes from 0 to 750, so all entries are going to the overflow; you can add gStyle->SetOptStat(111111); to see the under/overflow.
By the way, maybe you could do your macro without needing to create the huge 2-D FinalData array (with 249900x4998 doubles!), which takes a lot of memory.

I know that Q is much bigger but even If I don’t fill the histogram so I should see an empty histogram, I can’t. Why? The main problem is here.

Indeed I change the range to small value just for test. I don’t fill the histogram,In the first step I wanna see an empty histogram.

Is that all the code you are using? With your code above I don’t see the same, I do get an empty histogram (testing on Windows, with 2 ROOT versions: 6.22/02 on WSL, and 5.34/38).

Yes, I post the complete code. I test it again but I have same problem :thinking:

If you are using an older ROOT, maybe try a newer one. It could also be a memory issue due to the array I mentioned, but you would probably see other symptoms in this case. Otherwise, I don’t know, I cannot reproduce the problem. I would also remove the canvas “Divide, cd and Update” from your macro as they are not needed (should not affect older versions of root, but I don’t know).

I use root_v5.34.36 version. Anyway thanks for your helping :rose:

Hi,

Please use a newer ROOT version. We fix things :slight_smile:

And try without these lines:

I cannot tell from your script why you divide the canvas. And even less why you then go back to the un-divided canvas?

Cheers, Axel.

Hi,
Thank you :rose:
I will do it. I wanna to create another histogram in following so I divide canvas.

OK, then:

Canvas->Divide();
Canvas->cd(1); // draw into the first sub-pad
EventHisto1->Draw();

should do.

Yes, that’s better :rose: