The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SOOT::Examples::Hist - SOOT Examples for Hist

DESCRIPTION

This is a listing of all SOOT examples for Hist.

EXAMPLES

draw2dopt.pl

  use strict;
  use warnings;
  use SOOT ':all';
  
  # display the various 2-d drawing options
  $gROOT->Reset();
  $gStyle->SetOptStat(0);
  $gStyle->SetPalette(1);
  $gStyle->SetCanvasColor(33);
  $gStyle->SetFrameFillColor(18);
  
  my $pl = TPaveLabel->new();
  
  my $f2 = TF2->new("f2","xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
  my @params = (130,-1.4,1.8,1.5,1, 150,2,0.5,-2,0.5, 3600,-2,0.7,-3,0.3);
  for (my $i = 0; $i < scalar @params; $i++) {
   $f2->SetParameter($i, $params[$i]);
  }
  my $h2 = TH2F->new("h2","xygaus + xygaus(5) + xylandau(10)",20,-4,4,20,-4,4);
  $h2->SetFillColor(46);
  $h2->FillRandom("f2",40000);
  
  # basic 2-d options
  my $x1 = 0.67; 
  my $y1 = 0.875; 
  my $x2 = 0.85; 
  my $y2 = 0.95;
  my $cancolor = 17;
  
  my $c2h = TCanvas->new("c2h","2-d options",10,10,800,600);
  $c2h->Divide(2,2);
  $c2h->SetFillColor($cancolor);
  $c2h->cd(1);
  $h2->Draw();       
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"SCAT","brNDC");
  $c2h->cd(2);
  $h2->Draw("box");  
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"BOX","brNDC");
  $c2h->cd(3);
  $h2->Draw("arr");  
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"ARR","brNDC");
  $c2h->cd(4);
  $h2->Draw("colz"); 
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"COLZ","brNDC");
  $c2h->Update();
  
  # text option
  my $ctext = TCanvas->new("ctext","text option",50,50,800,600);
  $gPad->SetGrid();
  $ctext->SetFillColor($cancolor);
  $ctext->SetGrid();
  $h2->Draw("text"); 
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"TEXT","brNDC");
  $ctext->Update();
  
  # contour options
  my $cont = TCanvas->new("contours","contours",100,100,800,600);
  $cont->Divide(2,2);
  $gPad->SetGrid();
  $cont->SetFillColor($cancolor);
  $cont->cd(1);
  $h2->Draw("contz"); 
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"CONTZ","brNDC");
  $cont->cd(2);
  $gPad->SetGrid();
  $h2->Draw("cont1"); 
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"CONT1","brNDC");
  $cont->cd(3);
  $gPad->SetGrid();
  $h2->Draw("cont2"); 
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"CONT2","brNDC");
  $cont->cd(4);
  $gPad->SetGrid();
  $h2->Draw("cont3"); 
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"CONT3","brNDC");
  $cont->Update();
  
  #lego options
  my $lego = TCanvas->new("lego","lego options",150,150,800,600);
  $lego->Divide(2,2);
  $lego->SetFillColor($cancolor);
  $lego->cd(1);
  $h2->Draw("lego");     
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"LEGO","brNDC");
  $lego->cd(2);
  $h2->Draw("lego1");    
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"LEGO1","brNDC");
  $lego->cd(3);
  $gPad->SetTheta(61); 
  $gPad->SetPhi(-82);
  $h2->Draw("surf1pol"); 
  $pl->DrawPaveLabel($x1,$y1,$x2+0.05,$y2,"SURF1POL","brNDC");
  $lego->cd(4);
  $gPad->SetTheta(21); 
  $gPad->SetPhi(-90);
  $h2->Draw("surf1cyl"); 
  $pl->DrawPaveLabel($x1,$y1,$x2+0.05,$y2,"SURF1CYL","brNDC");
  $lego->Update();
  
  # surface options
  my $surf = TCanvas->new("surfaces","surface options",200,200,800,600);
  $surf->Divide(2,2);
  $surf->SetFillColor($cancolor);
  $surf->cd(1);
  $h2->Draw("surf1");   
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"SURF1","brNDC");
  $surf->cd(2);
  $h2->Draw("surf2z");  
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"SURF2Z","brNDC");
  $surf->cd(3);
  $h2->Draw("surf3");   
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"SURF3","brNDC");
  $surf->cd(4);
  $h2->Draw("surf4");   
  $pl->DrawPaveLabel($x1,$y1,$x2,$y2,"SURF4","brNDC");
  $surf->Update();
  
  $gApplication->Run;

earth.pl

  use strict;
  use warnings;
  use SOOT ':all';
  use File::Spec;
  
  $gROOT->Reset;
  $gStyle->SetPalette(1);
  $gStyle->SetOptTitle(1);
  $gStyle->SetOptStat(0);
  
  my $c1 = TCanvas->new("c1","earth_projections",1000,800);
  $c1->Divide(2,2);
  
  my $h1 = TH2F->new("h1","Aitoff",    180, -180, 180, 179, -89.5, 89.5);
  my $h2 = TH2F->new("h2","Mercator",  180, -180, 180, 161, -80.5, 80.5);
  my $h3 = TH2F->new("h3","Sinusoidal",180, -180, 180, 181, -90.5, 90.5);
  my $h4 = TH2F->new("h4","Parabolic", 180, -180, 180, 181, -90.5, 90.5);
  
  my $inFile = File::Spec->catfile($ENV{ROOTSYS}, qw(share doc root tutorials graphics earth.dat));
  open my $fh, "<", $inFile or die "Cannot open $inFile: $!";
  while (<$fh>) {
    chomp;
    my ($x, $y) = split /\s+/, $_;
    $x *= 1.;
    $y *= 1.;
    $h1->Fill($x, $y, 1);
    $h2->Fill($x, $y, 1);
    $h3->Fill($x, $y, 1);
    $h4->Fill($x, $y, 1);
  }
  close $fh;
  
  $c1->cd(1);
  $h1->Draw("z aitoff");
  
  $c1->cd(2);
  $h2->Draw("z mercator");
  
  $c1->cd(3);
  $h3->Draw("z sinusoidal");
  
  $c1->cd(4);
  $h4->Draw("z parabolic");
  
  $c1->Update();
  
  $gApplication->Run;
  

hksimple.pl

  use strict;
  use warnings;
  use SOOT ':all';
  use constant kUPDATE => 10;
  
  # *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  # *-*
  # *-*  This script illustrates the advantages of a TH1K histogram
  # *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  
  # Create a new canvas.
  my $c1 = TCanvas->new("c1","Dynamic Filling Example",200,10,600,900);
  $c1->SetFillColor(42);
  
  # Create a normal histogram and two TH1K histograms
  my @hpx;
  $hpx[0] = TH1F->new("hp0","Normal histogram",1000,-4,4);
  $hpx[1] = TH1K->new("hk1","Nearest Neighboor of order 3",1000,-4,4);
  $hpx[2] = TH1K->new("hk2","Nearest Neighboor of order 16",1000,-4,4,16);
  $c1->Divide(1,3);
  for my $j (0..2) {
     $c1->cd($j+1); 
     $gPad->SetFrameFillColor(33);
     $hpx[$j]->SetFillColor(48);
     $hpx[$j]->Draw();
  }
  
  # Fill histograms randomly
  $gRandom->SetSeed();
  foreach (0..299) {
    my $px = $gRandom->Gaus(0.0,1.0);
    $hpx[$_]->Fill($px) for 0..2;
    padRefresh($c1) if $_ and $_ % kUPDATE == 0;
  }
  
  $hpx[$_]->Fit("gaus","","") for 0..2;
  
  padRefresh($c1);
  
  sub padRefresh {
    my $pad = shift;
    my $flag = shift || 0;
  
    return if not defined $pad;
    $pad->Modified();
    $pad->Update();
    my $tl = $pad->GetListOfPrimitives();
    return if not defined $tl;
    for (my $i = 0; $i < $tl->GetSize(); $i++) {
      my $obj = $tl->At($i);
      padRefresh($obj, 1) if $obj->isa("TPad");
    }
    return if ($flag);
    $gSystem->ProcessEvents();
  }
  
  
  $gApplication->Run;
  

hstack.pl

  use strict;
  use warnings;
  use SOOT ':all';
  
  $gROOT->Reset();
  $gBenchmark->Start('hstack');
     
  my $hs = THStack->new("hs","test stacked histograms");
  
  # create three 1-d histograms
  my $h1 = TH1F->new("h1","test hstack",100,-4,4);
  $h1->FillRandom("gaus",20000);
  $h1->SetFillColor(kRed);
  $h1->SetMarkerStyle(21);
  $h1->SetMarkerColor(kRed);
  $hs->Add($h1);
  
  my $h2 = TH1F->new("h2","test hstack",100,-4,4);
  $h2->FillRandom("gaus",15000);
  $h2->SetFillColor(kBlue);
  $h2->SetMarkerStyle(21);
  $h2->SetMarkerColor(kBlue);
  $hs->Add($h2);
  
  my $h3 = TH1F->new("h3","test hstack",100,-4,4);
  $h3->FillRandom("gaus",10000);
  $h3->SetFillColor(kGreen);
  $h3->SetMarkerStyle(21);
  $h3->SetMarkerColor(kGreen);
  $hs->Add($h3);
  
  my $c1 = TCanvas->new("c1","stacked hists",10,10,1000,800);
  $c1->SetFillColor(41);
  $c1->Divide(2,2);
  
  # in top left pad, draw the stack with defaults
  $c1->cd(1);
  $hs->Draw();
  
  # in top right pad, draw the stack in non-stack mode and errors option
  $c1->cd(2);
  $gPad->SetGrid();
  $hs->Draw("nostack,e1p");
  
  # in bottom left, draw in stack mode with "lego1" option
  $c1->cd(3);
  $gPad->SetFrameFillColor(17);
  $gPad->SetTheta(3.77);
  $gPad->SetPhi(2.9);
  $hs->Draw("lego1");
  
  $c1->cd(4);
  
  #create two 2-D histograms and draw them in stack mode
  $gPad->SetFrameFillColor(17);
  
  my $a = THStack->new("a","test legos");
  my $f1 = TF2->new("f1","xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
  $f1->SetParameters([130.,-1.4,1.8,1.5,1, 150,2,0.5,-2,0.5, 3600,-2,0.7,-3,0.3]);
  
  my $h2a = TH2F->new("h2a","h2a",20,-4,4,20,-4,4);
  $h2a->SetFillColor(38);
  $h2a->FillRandom("f1",4000);
  
  my $f2 = TF2->new("f2","xygaus + xygaus(5)",-4,4,-4,4);
  $f2->SetParameters([100.,-1.4,1.9,1.1,2, 80,2,0.7,-2,0.5]);
  
  my $h2b = TH2F->new("h2b","h2b",20,-4,4,20,-4,4);
  $h2b->SetFillColor(46);
  $h2b->FillRandom("f2",3000);
  
  $a->Add($h2a);
  $a->Add($h2b);
  $a->Draw();
  
  $gBenchmark->Show('hstack');
  
  $gApplication->Run;
  

multicolor.pl

  use strict;
  use warnings;
  use SOOT ':all';
  use constant NBINS => 20;
  
  my $stack = shift;
  my $c1 = TCanvas->new;
  
  my $hs = THStack->new("hs","three plots")->keep;
  my @colors = (kBlue, kRed, kYellow);
  my @names  = qw(h1 h2 h3);
  my @h = map {
    my $h = TH2F->new(($names[$_]) x 2, NBINS,-4,4, NBINS,-4,4);
    $h->keep;
    $h->SetFillColor($colors[$_]);
    $hs->Add($h);
    $h
  } 0..$#names;
  
  my $r = TRandom->new;
  
  $h[0]->Fill($r->Gaus(), $r->Gaus()) for 1..20000; 
  
  foreach (1..200) {
    my $ix = int($r->Uniform(0, NBINS));
    my $iy = int($r->Uniform(0, NBINS));
    my $bin = $h[0]->GetBin($ix, $iy);
    my $val = $h[0]->GetBinContent($bin);
    next if $val <= 0;
    $h[0]->SetBinContent($bin,0) if not $stack;
    if ($r->Rndm() > 0.5) {
      $h[1]->SetBinContent($bin, 0) if not $stack;
      $h[2]->SetBinContent($bin, $val);
    } 
    else {
      $h[2]->SetBinContent($bin, 0) if not $stack;
      $h[1]->SetBinContent($bin, $val);
    }
  }
  $hs->Draw("lego1");
  
  $gApplication->Run;      
  

quantiles.pl

  use strict;
  use warnings;
  use SOOT ':all';
  
  use constant NQ => 100;
  use constant NSHOTS => 10;
  
  # demo for quantiles
  # Author; Rene Brun
  my $xq = [map {$_/NQ} 1..NQ]; # position where to compute the quantiles in [0,1]
  my $yq = [(0.) x NQ]; # array to contain the quantiles
  
  my $gr70 = TGraph->new(NSHOTS);
  my $gr90 = TGraph->new(NSHOTS);
  my $gr98 = TGraph->new(NSHOTS);
  my $h = TH1F->new("h", "demo quantiles", 50, -3, 3);
  
  for my $shot (0..NSHOTS-1) {
    $h->FillRandom("gaus", 50);
    $h->GetQuantiles(NQ, $yq, $xq);
    $gr70->SetPoint($shot, $shot+1, $yq->[70]*1.0);
    $gr90->SetPoint($shot, $shot+1, $yq->[90]*1.0);
    $gr98->SetPoint($shot, $shot+1, $yq->[98]*1.0);
  }
  
  # show the original histogram in the top pad
  my $c1 = TCanvas->new("c1", "demo quantiles", 10, 10, 600, 900);
  $c1->SetFillColor(41);
  $c1->Divide(1, 3);
  $c1->cd(1);
  $h->SetFillColor(38);
  $h->Draw();
  
  # show the final quantiles in the middle pad
  $c1->cd(2);
  $gPad->SetFrameFillColor(33);
  $gPad->SetGrid();
  my $gr = TGraph->new(NQ, $xq, $yq);
  $gr->SetTitle("final quantiles");
  $gr->SetMarkerStyle(21);
  $gr->SetMarkerColor(kRed);
  $gr->SetMarkerSize(0.3);
  $gr->Draw("ap");
  
  # show the evolution of some  quantiles in the bottom pad
  $c1->cd(3);
  $gPad->SetFrameFillColor(17);
  $gPad->DrawFrame(0, 0, NSHOTS+1, 3.2);
  $gPad->SetGrid();
  $gr98->SetMarkerStyle(22);
  $gr98->SetMarkerColor(kRed);
  $gr98->Draw("lp");
  $gr90->SetMarkerStyle(21);
  $gr90->SetMarkerColor(kBlue);
  $gr90->Draw("lp");
  $gr70->SetMarkerStyle(20);
  $gr70->SetMarkerColor(kMagenta);
  $gr70->Draw("lp");
  
  # add a legend
  my $legend = TLegend->new(0.85, 0.74, 0.95, 0.95);
  $legend->SetTextFont(72);
  $legend->SetTextSize(0.05);
  $legend->AddEntry($gr98," q98","lp");
  $legend->AddEntry($gr90," q90","lp");
  $legend->AddEntry($gr70," q70","lp");
  $legend->Draw();
  
  $gApplication->Run;

seism.pl

  use strict;
  use warnings;
  use SOOT ':all';
  use threads;
  use Time::HiRes 'usleep';
  
  my $sw = TStopwatch->new(); 
  $sw->Start();
  
  # set time offset
  #my $dtime = TDatime->new(); # FIXME TDatime not wrapped (not a TObject), but utterly superseded by Perl-tools
  $gStyle->SetTimeOffset(time()); # We could be more elaborate. Check out DateTime.pm
  
  my $c1 = TCanvas->new("c1","Time on axis",10,10,1000,500);
  $c1->SetFillColor(42);
  $c1->SetFrameFillColor(33);
  $c1->SetGrid();
     
  my $bintime = 1; # one bin = 1 second. change it to set the time scale
  my $ht = TH1F->new("ht","The ROOT seism",10,0,10*$bintime);
  my $signal = 1000.0;
  
  $ht->SetMaximum($signal);
  $ht->SetMinimum(-$signal);
  $ht->SetStats(0);
  $ht->SetLineColor(2);
  $ht->GetXaxis()->SetTimeDisplay(1);
  $ht->GetYaxis()->SetNdivisions(520);
  $ht->Draw();
     
  my $thr = threads->new(sub {$gApplication->Run()}); #canvas can be edited during the loop
  usleep(5000); # FIXME find better way to fix this
  $gApplication->SetReturnFromRun(1);
  
  for my $i (1..2299) {
    #======= Build a signal : noisy damped sine ======
    my $noise = $gRandom->Gaus(0,120);
    $noise += $signal*sin(($i-700.)*6.28/30)*exp((700.-$i)/300.) if $i > 700;
    $ht->SetBinContent($i,$noise);
    $c1->Modified();
    $c1->Update();
  }
  print sprintf("Real Time = %8.3fs, Cpu Time = %8.3fs\n",$sw->RealTime(),$sw->CpuTime());
  
  $gApplication->Terminate();
  $thr->join();
  

transpad.pl

  use strict;
  use warnings;
  use SOOT ':all';
  
  # Example of a canvas showing two histograms with different scales.
  # The second histogram is drawn in a transparent pad
  my $c1 = TCanvas->new("c1","transparent pad",200,10,700,500);
  my $pad1 = TPad->new("pad1","",0,0,1,1);
  my $pad2 = TPad->new("pad2","",0,0,1,1);
  $pad2->SetFillStyle(4000); # will be transparent
  $pad1->Draw();
  $pad1->cd();
  
  my $h1 = TH1F->new("h1","h1",100,-3,3);
  my $h2 = TH1F->new("h2","h2",100,-3,3);
  my $r = TRandom->new;
  
  my $nloop = 100000;
  for my $i (0..$nloop-1) {
    if ($i < 1000) {
      my $x1 = $r->Gaus(-1,0.5);
      $h1->Fill($x1);
    }
    my $x2 = $r->Gaus(1,1.5);
    $h2->Fill($x2);
  }
  
  $h1->Draw();
  $pad1->Update(); #this will force the generation of the "stats" box
  my $ps1 = $h1->GetListOfFunctions()->FindObject("stats")->as('TPaveStats');
  $ps1->SetX1NDC(0.4); 
  $ps1->SetX2NDC(0.6);
  $pad1->Modified();
  $c1->cd();
   
  #compute the pad range with suitable margins
  my $ymin = 0;
  my $ymax = 2000;
  my $dy = ($ymax-$ymin)/0.8; # 10 per cent margins top and bottom
  my $xmin = -3;
  my $xmax = 3;
  my $dx = ($xmax-$xmin)/0.8; # 10 per cent margins left and right
  $pad2->Range($xmin-0.1*$dx,$ymin-0.1*$dy,$xmax+0.1*$dx,$ymax+0.1*$dy);
  $pad2->Draw();
  $pad2->cd();
  $h2->SetLineColor(kRed);
  $h2->Draw("sames");
  $pad2->Update();
  
  my $ps2 = $h2->GetListOfFunctions()->FindObject("stats")->as('TPaveStats');
  $ps2->SetX1NDC(0.65); 
  $ps2->SetX2NDC(0.85);
  $ps2->SetTextColor(kRed);
  
  # draw axis on the right side of the pad
  my $axis = TGaxis->new($xmax,$ymin,$xmax,$ymax,$ymin,$ymax,50510,"+L");
  $axis->SetLabelColor(kRed);
  $axis->Draw();
  
  $gApplication->Run;

zones.pl

  use strict;
  use warnings;
  use SOOT ':all';
  
  $gROOT->Reset();
  my $c1 = TCanvas->new('c1','The Ntuple canvas',200,10,700,780);
  $gStyle->SetPadBorderMode(0);
  $gStyle->SetOptStat(0);
  $c1->Divide(2,2,0,0);
  
  my $pad1 = TPad->new('pad1','This is pad1',0.02,0.52,0.48,0.98,21);
  my $pad2 = TPad->new('pad2','This is pad2',0.52,0.52,0.98,0.98,21);
  my $pad3 = TPad->new('pad3','This is pad3',0.02,0.02,0.48,0.48,21);
  my $pad4 = TPad->new('pad4','This is pad4',0.52,0.02,0.98,0.48,1);
  
  $pad1->Draw();
  $pad2->Draw();
  $pad3->Draw();
  $pad4->Draw();
  
  my $h1 = TH2F->new("h1","test1",10,0,1,20,0,20);
  my $h2 = TH2F->new("h2","test2",10,0,1,20,0,100);
  my $h3 = TH2F->new("h3","test3",10,0,1,20,-1,1);
  my $h4 = TH2F->new("h4","test4",10,0,1,20,0,1000);
  $h1->FillRandom("gaus", 100000);
  $h2->FillRandom("gaus", 100000);
  $h3->FillRandom("gaus", 100000);
  $h4->FillRandom("gaus", 100000);
  
  $pad1->cd();
  $pad1->SetBottomMargin(0);
  $pad1->SetRightMargin(0);
  $pad1->SetTickx(2);
  $h1->Draw();
  
  $pad2->cd();
  $pad2->SetLeftMargin(0);
  $pad2->SetBottomMargin(0);
  $pad2->SetTickx(2);
  $pad2->SetTicky(2);
  $h2->GetYaxis()->SetLabelOffset(0.01);
  $h2->Draw();
  
  $pad3->cd();
  $pad3->SetTopMargin(0);
  $pad3->SetRightMargin(0);
  $h3->Draw();
  
  $pad4->cd();
  $pad4->SetLeftMargin(0);
  $pad4->SetTopMargin(0);
  $pad4->SetTicky(2);
  $h4->Draw();
  
  $c1->Update();
  
  $gApplication->Run;
  

SEE ALSO

SOOT

AUTHOR

Steffen Mueller, <smueller@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2010 by Steffen Mueller

SOOT, the Perl-ROOT wrapper, is free software; you can redistribute it and/or modify it under the same terms as ROOT itself, that is, the GNU Lesser General Public License. A copy of the full license text is available from the distribution as the LICENSE file.