Help me with TFile

Hi,

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;
    c.Divide(2,2);
    c.cd(1);
    hA->Draw();
    c.cd(2);
    hB->Draw();
    c.cd(3);
    hC->Draw();
    c.cd(4);
    hBC->Draw();
}

Cheers,
D

1 Like