Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist000_TH1_first.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// Hello World example for TH1
4///
5/// Shows how to create, fill and write a histogram to a ROOT file.
6///
7/// \macro_code
8///
9/// \date November 2024
10/// \author Giacomo Parolini (CERN)
11
12void hist000_TH1_first()
13{
14 // Open the file to write the histogram to
15 auto outFile = std::unique_ptr<TFile>(TFile::Open("outfile.root", "RECREATE"));
16
17 // Create the histogram object
18 // There are several constructors you can use (\see TH1). In this example we use the
19 // simplest one, accepting a number of bins and a range.
20 int nBins = 30;
21 double rangeMin = 0.0;
22 double rangeMax = 10.0;
23 TH1D histogram("histogram", "My first ROOT histogram", nBins, rangeMin, rangeMax);
24
25 // Fill the histogram. In this simple example we use a fake set of data.
26 // The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
27 // In general you should prefer TH1D over TH1F unless you have a very specific reason
28 // to do otherwise.
29 const std::array values{1, 2, 3, 3, 3, 4, 3, 2, 1, 0};
30 for (double val : values) {
31 histogram.Fill(val);
32 }
33
34 // Write the histogram to `outFile`.
35 outFile->WriteObject(&histogram, histogram.GetName());
36
37 // When the TFile goes out of scope it will close itself and write its contents to disk.
38}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4131
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:925