Is possible to use TH1F with two variables in the TDataFrame

Hi,

I’m trying to using TH1F in TDF to do a histogram with two
variable like this example below (This work before):

// Energy Distribution
    TH1F *hist1 = new TH1F("hist1","Energy Distribution LAB",100,-20,20);
    hist1->Fill(eta,E); 

// Tcanvas hist1
    TCanvas *c2 = new TCanvas("c2","Histograms 1 ",600,400);
    c2->SetGrid(1,1);
    c1->SetLogx(0);
    c1->SetLogy(1);
    
    hist1->SetLineColor(kRed);
    hist1->GetYaxis()->SetTitle("dE/d#eta [GeV]");
    hist1->GetXaxis()->SetTitle("#eta ");
    hist1->Draw();

but in the TDF I don’t know how to do this or I don’t find one example that explain this.

What I’m trying in my macro:

auto hist1 = d.Histo1D(TH1F("hist1", "Energy distribution in rapidity", 100, -20, 20), "eta", "E");

ROOT 6.13/03 | Built for linuxx8664gcc heads/master@v6-13-02-120-g3c7fa4a, Mar 27 2018, 09:56:12

Cheers, Andre

Hi Andre,

if you mean a weighted histo the syntax would be

auto hist1 = d.Histo1D({"hist1", "Energy distribution in rapidity", 100, -20, 20}, "eta", "E");

as shown here. Does this work for you?

Cheers,
D

Hi Danilo,

What I did:

using doubles = TVec<double>;
        
        auto RapCalc = [](doubles Es, doubles pzs) {
            auto all_RapCalc  = 0.5*log((Es+pzs)/(Es-pzs));
            auto good_RapCalc = all_RapCalc[Es > 200.];
            return good_RapCalc;
        };
        
        auto hist1 = d.Define("rap", RapCalc, {"E", "pz"}).Histo1D({"hist1", "Energy distribution in rapidity", 100, -20, 20}, "rap", "E");
        auto hist2 = d1.Histo1D({"hist2", "", 100, -20, 20}, "Eta", "e");

        // Drawing
        auto c1 = new TCanvas("c1", "c1", 10, 10, 700, 500);
        c1->SetGrid(1,1);
        c1->SetLogx(0); // 0 == scale without Log, 1 == scale with Log
        c1->SetLogy(1);
        hist1->GetYaxis()->SetTitle("dE/dy [GeV]");
        hist1->GetXaxis()->SetTitle("y");
        hist1->DrawClone();
        hist2->SetLineColor(kRed);
        hist2->DrawClone("same");

Error:

terminate called after throwing an instance of 'std::runtime_error'
  what():  Cannot fill histogram with values in containers of different sizes.

Cheers, Andre

Hi Andre,

what are the sizes of the containers stored in column e and column rap ?

Cheers,
D

Hi, Danilo

TTree  Mar 26 21:22 2018 T  "Tree"
  e    "e/D"    10737 (Entries 1334) 

rap is calculate using Es and Pzs that have:

TTree  Mar 24 22:09 2018 Particle  "particles produced"
  pz               "pz[nPart]/D"        94620
  E                "E[nPart]/D"         94616  (Entries 11746)

the size is very different, :sweat_smile:

Cheers, Andre

Hi Andre,

I am not sure what is the reason of this difference. TDataFrame can put in histograms collections and use collections for both values and weights. Unfortunately, having a values collection and a weights collection with different sizes is not a well defined situation, right? This is why TDataFrame complains.
Can you maybe fix this upfront in your code?

Cheers,
D

Hi Danilo,

When the energy cut is applied [Es >200],
the function return nothing (I Think):

using doubles = TVec<double>;
        
        auto RapCalc = [](doubles Es, doubles pzs) {
            auto all_RapCalc  = 0.5*log((Es+pzs)/(Es-pzs));
            auto good_RapCalc = all_RapCalc[Es > 200.];
            return good_RapCalc;
        };

and this can be a problem, i.e., RapCalc ('don’t receive nothing`)

auto dd2 = d.Define("rap", RapCalc, {"E", "pz"});
auto histoy1 = dd2.Histo1D({"histoy1", "Energy distribution", 100, -20, 20}, "rap", "E");

Cheers, Andre

Hi Andre,

but if you apply a cut, isn’t it normal that the values in all_RapCalc will be at most the ones before the cut?

Cheers,
D

Hi Andre,

just thought again about this. Isn’t the right thing to do also to filter E with E>200 to then make your plot? For example:

auto dd2 = d.Define("rap", RapCalc, {"E", "pz"}).Define("good_E", "E[E>200]");
auto histoy1 = dd2.Histo1D({"histoy1", "Energy distribution", 100, -20, 20}, "rap", "good_E");

This will ensure that the size is the same and you would actually use the right weight for your plot.

Cheers,
D

1 Like

Hi, Danilo,

Thanks, It worked, very well. I will continue to study TDF, this
interface for analyses of data is very nice.

Best, Andre

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