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/// \macro_output
9///
10/// \date November 2024
11/// \author Giacomo Parolini (CERN)
12
14{
15 // Open the file to write the histogram to
16 auto outFile = std::unique_ptr<TFile>(TFile::Open("outfile.root", "RECREATE"));
17
18 // Create the histogram object
19 // There are several constructors you can use (\see TH1). In this example we use the
20 // simplest one, accepting a number of bins and a range.
21 int nBins = 30;
22 double rangeMin = 0.0;
23 double rangeMax = 10.0;
24 TH1D histogram("histogram", "My first ROOT histogram", nBins, rangeMin, rangeMax);
25
26 // Fill the histogram. In this simple example we use a fake set of data.
27 // The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
28 // In general you should prefer TH1D over TH1F unless you have a very specific reason
29 // to do otherwise.
30 const std::array values{1, 2, 3, 3, 3, 4, 3, 2, 1, 0};
31 for (double val : values) {
32 histogram.Fill(val);
33 }
34
35 // Write the histogram to `outFile`.
36 outFile->WriteObject(&histogram, histogram.GetName());
37
38 // When the TFile goes out of scope it will close itself and write its contents to disk.
39}
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:4088
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693