Re: Problem with TH1::Draw

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Jul 13 1999 - 18:35:35 MEST


Hi Damon,
a small macro drawing an histogram in BAR mode when the histogram has
errors.

Rene Brun

{
   gROOT->Reset();
   TH1F *h = new TH1F("h","demo with bars",20,-5,15);
   h->Sumw2();
   for (Int_t i=0;i<1000;i++) {
      Float_t x = gRandom->Landau(0,1);
      h->Fill(x);
   }
   TCanvas *c1 = new TCanvas("c1","demo",600,800);
   c1->Divide(1,2);
   c1->cd(1);
   h->SetFillColor(kBlue);
   h->SetMarkerStyle(21);
   h->SetBarWidth(0.5);
   h->SetBarOffset(0.25);
   h->Draw("bhist");
   c1->cd(2);
   h->Draw("e1");
}         

Damon Spayde wrote:
> 
> I have written a script that reads in an ASCII file and generates three
> one-dimensional histograms of its contents.  I would really like to look
> at these histograms in bar graph form; i.e. with a bar for each bin.
> However, when I specify the option "B" in TH1::Draw() the histogram gets
> drawn with little tiny points and error bars for each bin.  Any help in
> understanding why this is happening would be greatly appreciated.  My ROOT
> version is 2.21/08, run on a DEC Alpha with Digital Unix 4.0d.  ROOT was
> compiled using gcc version 2.8.1.  Below I have included a copy of the
> script.
> 
>                                         Thanks,
>                                         Damon Spayde
> 
> --------------------------------
> Damon Spayde
> Department of Physics
> University of Maryland
> PO Box 175
> College Park, MD  20742
> Phone:  (301) 405-6113
> Fax:  (301) 405-8558
> e-mail:  dspayde@physics.umd.edu
> --------------------------------
> 
> //////////////////////////////////////////////////////////////////
> //                                                              //
> // dotout_hist -- Create a histogram of a *.out file's contents //
> //                                                              //
> // Author:  Damon Spayde                                        //
> //                                                              //
> // Usage:                                                       //
> //                                                              //
> // Caveats:                                                     //
> //   1)  Assumes that badruns file and ADC files are sorted.    //
> //                                                              //
> //////////////////////////////////////////////////////////////////
> 
> #include <fstream.h>
> 
> struct Badrun
> {
>   Int_t low;
>   Int_t high;
> };
> 
> struct Adc
> {
>   Int_t runnumber;
>   Int_t helicity;
>   Int_t events;
>   Double_t yield;
>   Double_t yield_error;
>   Double_t yield_width;
>   Int_t pulsepairs;
>   Double_t asym;
>   Double_t asym_error;
>   Double_t asym_width;
> };
> 
> 
> const Int_t IN = -1;
> const Int_t OUT = 1;
> const Double_t PPM = 1.0e6;
> 
> Int_t dotout_hist(Char_t *adc_file,Int_t first_run,Int_t last_run, Char_t *badruns_file = "badruns.dat")
> {
> 
>   // Initialization
>   Adc adc;
>   Badrun badrun;
>   ifstream BADRUNSfile(badruns_file);   // Open badruns.dat for
>                                         // reading
>   if(BADRUNSfile.bad()) {
>     cerr << "Error:  Could not open badruns_file.\n";
>     exit(7);
>   }
> 
>   Char_t adc_filename[40];
>   sprintf(adc_filename,"%s.out",adc_file);
>   ifstream ADCfile(adc_filename);   // Open appropriate .out file for
>                                // reading
>   if(ADCfile.bad()) {
>     cerr << "Error:  Could not open adc_filename.\n";
>     exit(8);
>   }
> 
>   Char_t hname[20];
>   Char_t htitle[80];
>   sprintf(hname,"h_%s",adc_file);
>   sprintf(htitle,"Weighted Asymmetry Histogram in %s over Runs
> %d to %d, Both Wave Plate States",adc_file,first_run,last_run);
>   TH1F *adc_hist = new TH1F(hname,htitle,200,-100,100); //Create a new
>                                                         //histogram
> 
>   sprintf(hname,"h_in_%s",adc_file);
>   sprintf(htitle,"Weighted Asymmetry Histogram in %s over Runs %d to
> %d, Wave Plate In",adc_file,first_run,last_run);
>   TH1F *adc_in_hist = new TH1F(hname,htitle,200,-100,100); //Create a new
>                                                         //histogram
> 
>   sprintf(hname,"h_out_%s",adc_file);
>   sprintf(htitle,"Weighted Asymmetry Histogram in %s over Runs %d to
> %d, Wave Plate Out",adc_file,first_run,last_run);
>   TH1F *adc_out_hist = new TH1F(hname,htitle,200,-100,100); //Create a new
>                                                         //histogram
> 
>   adc_hist->Sumw2();  // Record sum of weights for each bin of
>                       // histogram
>   adc_in_hist->Sumw2();  // Record sum of weights for each bin of
>                       // histogram
>   adc_out_hist->Sumw2();  // Record sum of weights for each bin of
>                       // histogram
> 
>   // Loop over the adc file until line corresponding to first run or
>   // greater is loaded into the first struct
>   do {
>     ADCfile >> adc.runnumber >> adc.helicity;
>     ADCfile >> adc.events >> adc.yield >> adc.yield_error;
>     ADCfile >> adc.pulsepairs >> adc.asym >> adc.asym_error;
>     //    cout << adc.runnumber << ":  " << adc.asym << "\n";
>   } while (adc.runnumber < (first_run - 1));
> 
>   do {
>     BADRUNSfile >> badrun.low >> badrun.high;
>   } while (badrun.high < first_run);
> 
>   // Loop over all file lines
>   Int_t i = ;
>   Int_t j = ;
> 
>   while(adc.runnumber >= (first_run - 1) && adc.runnumber <= last_run) {
> 
>     ADCfile >> adc.runnumber >> adc.helicity;
>     ADCfile >> adc.events >> adc.yield >> adc.yield_error;
>     ADCfile >> adc.pulsepairs >> adc.asym >> adc.asym_error;
>     adc.asym *= PPM;
>     adc.asym_error *= PPM;
>     adc.yield_width = adc.yield * sqrt(adc.events);
>     adc.asym_width = adc.asym * sqrt(adc.pulsepairs);
> 
>     cout << adc.runnumber << ":  " << adc.asym << "\n";
> 
>     do {
>       if (badrun.high < adc.runnumber) { // Is it necessary to read
>                                          // another line of
>         // badruns.dat?
>         BADRUNSfile >> badrun.low >> badrun.high;
>       }
> 
>       if (adc.runnumber < badrun.low) {
>         // Put hist fill here
>         adc_hist->Fill((Axis_t) adc.helicity*adc.asym,(Stat_t)
>                        1.0/(adc.asym_error * adc.asym_error));
>         switch (adc.helicity) {
>         case IN:
>           adc_in_hist->Fill((Axis_t) adc.asym,(Stat_t) 1.0/(adc.asym_error * adc.asym_error));
>           break;
>         case OUT:
>           adc_out_hist->Fill((Axis_t) adc.asym,(Stat_t) 1.0/(adc.asym_error * adc.asym_error));
>           break;
>         default:
>           printf("Error: helicity not equal to +/-1 for run
> %d.\n",adc.runnumber);
>           break;
>         }
>       }
> 
>     } while (badrun.high < adc.runnumber);
> 
>   }
> 
>   ADCfile.close();
>   BADRUNSfile.close();
> 
>   Char_t ctitle[40];
>   sprintf(ctitle,"%s Histograms",adc_file);
>   TCanvas *c2 = new TCanvas("c2",ctitle,600,777);
>   c2->SetBorderMode(0);
>   c2->SetFillColor(0);
>   gStyle->SetPadBorderMode(0);
>   gStyle->SetFillColor(0);
>   c2->Divide(1,3);
>   c2->cd(1);
>   adc_in_hist->Draw("B");
>   adc_in_hist->Print();
>   gPad->SetLogy(1);
>   gPad->Modified();
>   c2->cd(2);
>   adc_out_hist->Draw("B");
>   adc_out_hist->Print();
>   gPad->SetLogy(1);
>   gPad->Modified();
>   c2->cd(3);
>   adc_hist->Draw("B");
>   adc_hist->Print();
>   gPad->SetLogy(1);
>   gPad->Modified();
>   c2->Modified();
> 
> 
> 
> }
> 
> 
> 
> 
> 
> 
> 
> 
>



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:35 MET