Help me with TFile

generate three sets of random numbers

between 20-1000 - A
between -1 to 1 -B
between 0-6 - C
Create a file with three branches (A,B and C ) and one record in file then will have three fields.Generate 1000 records.Then
a:) the histograms of each field.
b:) a 2D plot between B and C .


As I already told you, you should look at the ROOT User Guides and Manuals, and I would start with the ROOT Primer and the ROOT tutorials

Cheers, Bertrand.

sounds a lot like homework :slight_smile:

here is how I’d do it in Go:

package main

import (

func main() {
	f, err := os.Create("data.txt")
	if err != nil {
	defer f.Close()

	for i := 0; i < 1000; i++ {
		a := rand.Float64()*(1000-20) + 20 // transform a [0,1] interval into [20,1000]
		b := rand.Float64()*2 - 1          // transform a [0,1] interval into [-1,+1]
		c := rand.Float64() * 6
		fmt.Fprintf(f, "%+e %+e %+e\n", a, b, c)
	err = f.Close()
	if err != nil {
		log.Fatalf("could not close file %q: %v", f.Name(), err)
$> go run ./gen.go
$> head data.txt
+6.125671e+02 +8.810182e-01 +3.987360e+00
+4.489599e+02 -1.507250e-01 +4.120938e+00
+8.432428e+01 -6.869615e-01 +5.818171e-01
+3.148936e+02 +3.042526e-02 +4.881840e+00
+2.299786e+02 -2.386856e-01 +1.908349e+00
+4.795120e+02 -4.339317e-01 +1.758611e+00
+6.855030e+02 -5.628939e-01 +1.219121e+00
+3.736540e+02 +1.413466e-01 +5.174949e+00
+3.072520e+02 -4.058349e-01 +4.515438e+00
+2.224510e+02 +7.306700e-01 +4.180315e+00

don’t forget to change the seeds of the random number generators, otherwise you’ll waste a lot of CPU.

1 Like

Yepp . Thanks for answering .
but the loop is not working.

error: expected expression
m := rand.Float64()*(1000-20) + 20
ROOT_prompt_15:1:10: error: member reference base type ‘int () throw()’ is not a structure or union

I wrote it using Go.
ROOT’s prompt expects C++ code.

you’ll have to either install and compile my Go code with the Go toolchain:
or translate the Go code to C++.

let me try it friend !!
Thanks !


with TDataFrame, which is part of ROOT, the job is very easy: the example below is in C++, works on the prompt and can be run in parallel mode for more performance.
It creates the histograms and daws them and persistify on disk your random numbers as well.

   // Uncomment the following line if you want to parallelise the execution on all of your cores
   // ROOT::EnableImplicitMT();

   ROOT::Experimental::TDataFrame d(1000);
   auto d0 = d.Define("A", [](){return gRandom->Uniform(20, 1000);})
              .Define("B", [](){return gRandom->Uniform(-1, 1);})
              .Define("C", [](){return gRandom->Uniform(0, 6);});
    auto hA = d0.Histo1D("A");
    auto hB = d0.Histo1D("B");
    auto hC = d0.Histo1D("C");
    auto hBC = d0.Histo2D({"hBC", "B vs C", 128, -1, 1, 128, 0, 6}, "B", "C");
    // this last line saves the dataset you just created on disk
    d0.Snapshot("t", "myFile.root");

    TCanvas c;


1 Like

Thanks bro !!
You made my day !!!

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