# Taking a random subsample of a histogram

**URL:** <https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [February 8, 2021, 9:13am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436 "2021-02-08T09:13:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@Vangi](https://root-forum.cern.ch/u/Vangi)\
**Post date:** [February 8, 2021, 9:13am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/1 "2021-02-08T09:13:59Z")

</div>

Dear ROOTers,  
I have a histogram of a variable and I want to split it randomly into two histograms, one containing a random x% of the events (of the total sample, not splitting each bin into x% and 100-x%), and the other containing the remaining 100-x%. I am not sure if there is a built in function for what I need and if not, which would be the easiest way of implementing it.  
Cheers,  
Vangi

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 10, 2021, 9:09am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/2 "2021-02-10T09:09:13Z")

</div>

Hi @Vangi ,  
I think we need @moneta here, let’s ping him.

---

<div class="post-metadata">

**Author:** ![Vangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@Vangi](https://root-forum.cern.ch/u/Vangi)\
**Post date:** [February 10, 2021, 9:31am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/3 "2021-02-10T09:31:20Z")

</div>

Just to clarify, I don’t want the value of x to be random (I want to be able to set its value manually), but the events chosen to be random - e.g. if I choose 10% the subset chosen is different each time I run my script.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 10, 2021, 9:40am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/4 "2021-02-10T09:40:11Z")

</div>

Are your original data stored in histograms or in trees ? If they are stored in histograms that might be a bit tricky to pick up “some events” as the individual events are lost one a bin is filled. You will be able to pick only the bins contents.

---

<div class="post-metadata">

**Author:** ![Haidar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@Haidar](https://root-forum.cern.ch/u/Haidar)\
**Post date:** [February 10, 2021, 9:50am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/5 "2021-02-10T09:50:57Z")

</div>

Hi couet,  
I also have the same problem. But for my case it is 2D histogram I want randomly split the histogram into two new histograms say 20% and 80% of the original histogram. Is it feasible?

---

<div class="post-metadata">

**Author:** ![Vangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@Vangi](https://root-forum.cern.ch/u/Vangi)\
**Post date:** [February 10, 2021, 10:01am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/6 "2021-02-10T10:01:44Z")

</div>

Hello,  
Yes, the events are currently stored as histograms, unfortunately. I guess I could change this and store the value and weight of each event into tree branches instead. In that case, how would one proceed?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 10, 2021, 10:25am UTC](https://root-forum.cern.ch/t/taking-a-random-subsample-of-a-histogram/43436/7 "2021-02-10T10:25:05Z")

</div>

I guess you can look at [this example](https://root.cern/doc/master/h1analysis_8C.html)
