# Help on Method/s to create Tree from multi-fold gamma-ray coincidence data

**URL:** <https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704>\
**Category:** ROOT\
**Created:** [July 9, 2018, 7:06am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704 "2018-07-09T07:06:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 7:06am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/1 "2018-07-09T07:06:20Z")

</div>

* * *

\_ROOT Version: 5.34/09  
\_Platform: Linux 3.11.10-100.fc18.x86\_64  
\_Compiler: gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)

* * *

Hello,

I need a help on the topic mentioned in the subject line. Here is the description of the problem:

I am working on the analysis of the data generated in a gamma-spectroscopy experiment. In the experiment, we had 24 detectors. We get ENERGY and TIME information from ONLY those detectors which have detected a gamma-ray in a given event, which also defines fold of an event (i.e. number of detectors which have registered the events). Also, the fold varies for different events, and I have about 100 data files.

First, I want to convert the data into ROOT Tree format for all the files, and later I will use this tree to generate different 1-D, 2-D, and 3-D histograms, with different conditions (using timing information).

Can someone please help me with guidelines/recipe to generate a Tree out of “Energy, Time and Event”? What is the best way to convert the above data into ROOT Tree? What should be the structure of the tree?

I am sorry, if this sounds too obvious. Please note that - this is my first attempt to try data analysis using ROOT, and to create Tree!

Any help/guidance is highly appreciated.

With best regards,

Ajay

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 9, 2018, 7:18am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/2 "2018-07-09T07:18:40Z")

</div>

Hi Ajay,

can you share one of the files you have?  
As a side comment: could you move to ROOT 6.14? ROOT 5.34 is not developed any more.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 7:27am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/3 "2018-07-09T07:27:24Z")

</div>

Thank you very much Danilo.

I plan to move to ROOT 6.14 soon.

I am sorry, but the data files which I have are in a customize binary format. I had already written a code which will extract “ENERGY and TIME” for number of detectors in “FOLD”. For example, if we consider a specific event has FOLD = 3, then I will have 3 ENERGY and 3 TIME signals.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 9, 2018, 7:29am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/4 "2018-07-09T07:29:13Z")

</div>

Hi Ajay,

I see. Do you have code to read these binary files (Python, C, C++)? This “adapter” will be needed irrespective from the ROOT part of the solution 🙂

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 7:33am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/5 "2018-07-09T07:33:33Z")

</div>

Thanks for the prompt reply.

I am poor in programming! The code which I have is mostly(!) in C++. _Mostly_ - because I said I am a poor programmer and some parts of it may also have some “C” in it 🙂

Ajay

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 9:40am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/6 "2018-07-09T09:40:53Z")

</div>

Hello Danilo,

Do you need anything else from me? Waiting for your reply.

Regards,

Ajay

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 9, 2018, 10:41am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/7 "2018-07-09T10:41:11Z")

</div>

I guess you would need to provide one of your binary data files and the source code which you have for reading it.

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 10:59am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/8 "2018-07-09T10:59:05Z")

</div>

I will be happy to share all the required information with you. But please note that the data files are of 300MB. Can you suggest me a way to share? Can I simply upload it here?

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 11:09am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/10 "2018-07-09T11:09:35Z")

</div>

Hello,

I am sharing a link to a .tgz file which contain all the information needed. But please forgive me for my poor coding techniques.

Regards,

Ajay

> **[can2root.tgz](https://www.dropbox.com/s/dhry431uyzdz8ug/can2root.tgz?dl=0)**
>
> Shared with Dropbox

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 9, 2018, 11:21am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/11 "2018-07-09T11:21:11Z")

</div>

Ajay,

I think the best way is to try to take your code which reads your binary format and marry it with [RDataFrame](https://root.cern.ch/doc/v614/classROOT_1_1RDataFrame.html). More in detail, the skeleton could be:

```auto
auto N = 123 ; // <- Here the number of events
ROOT::RDataFrame d(N);
d.Define("Energy", yourFunctionReturningEnergy).Define("Time", yourFunctionReturningTime).Snapshot("mytree", "mydataset.root");

```

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 11:38am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/12 "2018-07-09T11:38:10Z")

</div>

Danilo,

Thank you.

But I am looking for a simple and traditional way of generating a Tree. As you might have noticed from the code that - I am already extracting the information and filling histograms as follows:

(1) Filling timing spectra : his\_TDC[n]-\>Fill(CLT[n]);  
(2) Filling energy spectra: his\_AB[n]-\>Fill(Eclab[n]);  
(3) Getting multiplicity with: clov\_mult = cl\_mult.count();

I am not sure how to use RDataFrame, and also won’t be able to spend time on it at the moment.

Regards,

Ajay

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 9, 2018, 12:40pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/13 "2018-07-09T12:40:09Z")

</div>

Are you able to open the files?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 10, 2018, 1:33am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/19 "2018-07-10T01:33:15Z")

</div>

The attached source code shows how you can save / retrieve “clovers” in / from a TTree (note: if you want a “non-zero-suppressed” TTree, add `-D_ZERO_SUPPRESSION_LEVEL_=0` when compiling “canSort\_new.cxx”).

It seems that ROOT 5.34 (using the ZLIB level 1 by default) achieves about three times better compression than ROOT 6.14 (using the LZ4 level 4 by default) without any significant difference in execution times so, with ROOT 6 one should always remember to enforce the compression algorithm and its level (which is usually not needed with ROOT 5), e.g.:

```auto
f = new TFile(hisfile, "RECREATE", "", 101); // 101 = 100 * ROOT::kZLIB + 1

```

One can further improve the compression by another factor two if one enforces LZMA (unfortunately, this will also double the execution time but, it may really be worth to bear it), e.g.:

```auto
f = new TFile(hisfile, "RECREATE", "", 201); // 201 = 100 * ROOT::kLZMA + 1

```

Note: The “man lzma” says that the “compression preset level” can be “-0 … -9”. It seems to me that ROOT is unable to use the “-0” (i.e. the “–fast”) level. When one tries “compress = 200” (= 100 \* ROOT::kLZMA + 0), one gets uncompressed ROOT files. Manual trials show that ROOT files compressed with “-0” are almost the same size as with “-1” but the time needed is around 20% shorter (at least). So, it would really make sense to modify ROOT so that it recognizes “compress = 200” as “LZMA @ -0” (or maybe one could set the “actual LZMA level” = “ROOT LZMA compression - 1”, so that “201” would be “-0”, “202” would be “-1” and so on).

[canSort\_new.cxx](https://root-forum.cern.ch/uploads/default/original/3X/a/7/a7f4b14a17aa35dca852958204e2720e56ea2d02.cxx) (45.6 KB)  
[canSortTTreeDump.cxx](https://root-forum.cern.ch/uploads/default/original/3X/b/6/b6112f5a39b21e0d732ef7eb25f06d00a37d8c8f.cxx) (2.8 KB)

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 10, 2018, 4:53am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/20 "2018-07-10T04:53:00Z")

</div>

Your help and time is highly appreciated. It will certainly help me. I can see that you have been working on this all the time since I have posted the code 🙂

Regards,

Ajay

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 10, 2018, 6:45am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/21 "2018-07-10T06:45:59Z")

</div>

Hello,

I was just wondering - why is there “&” before “_clov\_mult_” and “_No\_Clover_” when you define Branches, while it is missing in others.

May be it is a stupid question?

Ajay

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 10, 2018, 8:43am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/22 "2018-07-10T08:43:26Z")

</div>

That’s “simple variables” versus “arrays” (and anyhow, for these arrays, writing `&array` is equivalent to writing simply `array`, you can try if you want).

Note that in the above “canSort\_new.cxx” source code `_clov_mult_ = clov_mult;` so I could use the original `clov_mult` variable everywhere. But I decided to introduce a separate `_clov_mult_`, just for the TTree, in case you wanted to change its value / meaning in future (so that the original `clov_mult` could remain unchanged).

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 10, 2018, 9:37am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/23 "2018-07-10T09:37:26Z")

</div>

Okay. Thank you once again.

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 11, 2018, 3:01pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/24 "2018-07-11T15:01:31Z")

</div>

Wow! That’s great.

I have modified the code so that the histograms are filled in a specific way. I am attaching the modified version of the code, just in case you plan to work on it further.

Thank you very much.

[can2root.cxx](https://root-forum.cern.ch/uploads/default/original/3X/4/a/4ad540298fdf6953cd035ff523aad72eaa84215f.cxx) (43.7 KB)

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [July 11, 2018, 3:27pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/25 "2018-07-11T15:27:21Z")

</div>

I think we have a small problem:

I have compiled the code with -D\_ZERO\_SUPPRESSION\_LEVEL\_=2, and then ran it to generate a root file.  
After this, when I dumped “non-zero” entries only, I still get zeros as shown below:

1 : 1 : 1 : ( 8 : 0 , 458 ) \<======== here CLT[8] = 0

75 : 1 : 2 : ( 2 : 2885 , 0 ) ( 12 : 2902 , 245 ) \<======== here Eclab[2] = 0

Ideally, all those events are valid in which both CLT[j] and Eclab[j] are non-zero.

Attaching, “modified” version of the code again.

Ajay

[can2root.cxx](https://root-forum.cern.ch/uploads/default/original/3X/4/a/4ad540298fdf6953cd035ff523aad72eaa84215f.cxx) (43.7 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 11, 2018, 3:50pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/26 "2018-07-11T15:50:15Z")

</div>

You seem to miss the point … see what’s after the line `#if (_ZERO_SUPPRESSION_LEVEL_) > 1`.

If you want to require that both “CLT” and “Eclab” are non-zero, replace the line (“pass” if non-zero “CLT” OR “Eclab”):

```auto
if ((CLT[n] == 0) && (Eclab[n] == 0)) continue;

```

with (“pass” if non-zero “CLT” AND “Eclab”):

```auto
if ((CLT[n] == 0) || (Eclab[n] == 0)) continue;

```

BTW. Note that “clov\_mult” seems to be strictly related to non-zero “Eclab” only (i.e. it doesn’t care about “CLT”).

[Next page](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704.md?page=2)
