Draw histogram with special case

f I have two files.txt (attached), each file has two columns

Hello @Khallod,

Let me rephrase your question:

  • How can I fill a histogram multiple times with the same value?

To solve this, simply read the files, and fill a histogram with the X values. When Y shows 10 entries for a specific value of X, fill this value 10 times (or with a weight of 10).
Then start reading the second file, and fill again. If Y now shows 5 entries for a specific values of X, fill five times. That’s all you need to do because histograms are made for counting the specific values.

The following macro is an attempt to do what you are asking. Devices.C (1.1 KB)

It produces the following plot:

And the following output:

root [0] 
Processing Devices.C...
Computed bin width = 1.00087
Bin width = 1 
Computed bin width = 0.999131
Bin width = 1 
Info in <TH1D::Add>: Attempt to add histograms with different axis limits - trying to use TH1::Merge
Error in <Merge>: Cannot merge histograms - limits are inconsistent:
 first: h1 (8193, -7.233972, 8191.865583), second: h2 (8193, -6.766019, 8178.114006)
root [1] 

As you can see your data do not match along the X axis. You will have to sort this out. That’s up to you to decide what the X axis should be.

I was tried to modify the original files

May be in the Devices.C create the histogram with fixed parameters (if possible). Like that your histograms will be compatible and it will be possible to add them.

It is fine to have slightly different values for te X axes. The only things you need to have in order to be able to add the two histogram are:

  1. same number of bins
  2. same axis limits for the X axis.

If you are able to define you histograms this way you will be able to add them.

For example in your file Data1.txt the mime value along the X axis is -6.733971619 and in the file Data2.txt the minimum value is -9.266038546 … That’s quite different. Which value should be used ? the smallest ? Same thing for the maximum, The values are quite different.
That’s your data you should know which values should be used. You need also to define the proper bin width (or number of bins, that’s equivalent once you have defined the X axis limits).

So do it that way. I seems you know what to do …

So read the data into arrays and reorder them as you wish before filling the histogram.

Try this:
khallod.C (878 Bytes)

The macro I posted is the addition of the data of the two files, as you asked:

  • bin width = 2
  • x>0
  • xmin = min of the two files
  • xmax = max of the two files.

So what do you want next ? implementing the re-order you explained above ? I am not sure I fully understand it. But I think you can do it yourself as you now have the histogram correctly filled from the two files.

What i want next is --> print the Raw data of the output histogram !!

What do you mean exactly by the “Raw data of an histogram” ?
The bins content of the histogram … ?

Here is a new version of the macro.
it creates “result.txt”
khallod.C (1.1 KB)

Thank you , , , , , , , , ,

I am trying to make the

The histogram is booked with:

  • bin width = 2
  • xmin = min of the two files
  • xmax = max of the two files.

Then this histogram is filled with the data read in the two files.

To produce result.txt the bin center and the bin content of each bin are used.

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