Rivet is hosted by Hepforge, IPPP Durham

Rivet analyses reference

EXAMPLE

A demo to show aspects of writing a Rivet analysis
Experiment: ()
Status: EXAMPLE
Authors:
  • Andy Buckley
No references listed
Beams: * *
Beam energies: ANY
Run details:
  • All event types will be accepted.

This analysis is a demonstration of the Rivet analysis structure and functionality: booking histograms; the initialisation, analysis and finalisation phases; and a simple loop over event particles. It has no physical meaning, but can be used as a simple pedagogical template for writing real analyses.

Source code: EXAMPLE_CUTS.cc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// -*- C++ -*-
#include "Rivet/Analysis.hh"
#include "Rivet/Projections/FinalState.hh"

namespace Rivet {


  /// @brief Just measures a few random things as an example.
  class EXAMPLE_CUTS : public Analysis {
  public:

    /// Constructor
    DEFAULT_RIVET_ANALYSIS_CTOR(EXAMPLE_CUTS);


    /// @name Analysis methods
    //@{

    /// Set up projections and book histograms
    void init() {

      // Projections
      const FinalState cnfs(Cuts::abseta < 4);
      declare(cnfs, "FS");

      // Histograms
      _histPt         = bookHisto1D("pT", 30, 0, 30);
      _histMass       = bookHisto1D("Mass", 20, 0, 1);

    }


    /// Do the analysis
    void analyze(const Event& event) {
      // Make sure to always include the event weight in histogram fills!
      const double weight = event.weight();

      const Particles ps = apply<FinalState>(event, "FS").particlesByPt();

      Cut ptcut = Cuts::range(Cuts::pT, 5, 20);
      Cut masscut = Cuts::range(Cuts::mass, 0, 0.2);
      Cut combine = ptcut && masscut; //< Possible to combine cuts

      for (const Particle& p : ps) {
        if ( ptcut->accept(p) )
          _histPt->fill(p.pT(), weight);
        if ( combine->accept(p) )
          _histMass->fill(p.mass(), weight);
      }
    }


    /// Finalize
    void finalize() {
      normalize({_histPt, _histMass});
    }

    //@}


    //@{
    /// Histograms
    Histo1DPtr _histPt, _histMass;
    //@}

  };



  // The hook for the plugin system
  DECLARE_RIVET_PLUGIN(EXAMPLE_CUTS);

}
EXAMPLE_SMEAR.cc
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// -*- C++ -*-
#include "Rivet/Analysis.hh"
#include "Rivet/Projections/FastJets.hh"
#include "Rivet/Projections/IdentifiedFinalState.hh"
#include "Rivet/Projections/DressedLeptons.hh"
#include "Rivet/Projections/TauFinder.hh"
#include "Rivet/Projections/SmearedJets.hh"
#include "Rivet/Projections/SmearedParticles.hh"
#include "Rivet/Projections/SmearedMET.hh"

namespace Rivet {


  class EXAMPLE_SMEAR : public Analysis {
  public:

    /// Constructor
    DEFAULT_RIVET_ANALYSIS_CTOR(EXAMPLE_SMEAR);


    /// @name Analysis methods
    //@{

    /// Book histograms and initialise projections before the run
    void init() {

      MissingMomentum mm(Cuts::abseta < 5);
      declare(mm, "MET0");

      SmearedMET smm1(mm, MET_SMEAR_ATLAS_RUN2);
      declare(smm1, "MET1");

      SmearedMET smm2(mm, [](const Vector3& met, double){ return P3_SMEAR_LEN_GAUSS(met, 0.1*met.mod()); });
      declare(smm2, "MET2");


      FastJets fj(FinalState(Cuts::abseta < 5), FastJets::ANTIKT, 0.4);
      declare(fj, "Jets0");

      SmearedJets sj1(fj, JET_SMEAR_IDENTITY);
      declare(sj1, "Jets1");

      SmearedJets sj2(fj, JET_SMEAR_ATLAS_RUN2,
                      [](const Jet& j){ return j.bTagged() ? 0.7*(1 - exp(-j.pT()/(10*GeV))) : 0.01; } );
      declare(sj2, "Jets2");

      SmearedJets sj3(fj,
                      JET_SMEAR_CMS_RUN2,
                      JET_BTAG_EFFS(0.7, 0.1, 0.01),
                      JET_CTAG_PERFECT,
                      JET_EFF_CONST(0.8));
      declare(sj3, "Jets3");


      IdentifiedFinalState photons(Cuts::abseta < 5, PID::PHOTON);


      IdentifiedFinalState truthelectrons(Cuts::abseta < 5 && Cuts::pT > 10*GeV, {{PID::ELECTRON, PID::POSITRON}});
      declare(truthelectrons, "Electrons0");
      DressedLeptons dressedelectrons(photons, truthelectrons, 0.2);
      declare(dressedelectrons, "Electrons1");
      SmearedParticles recoelectrons(dressedelectrons, ELECTRON_EFF_ATLAS_RUN2, ELECTRON_SMEAR_ATLAS_RUN2);
      declare(recoelectrons, "Electrons2");

      IdentifiedFinalState truthmuons(Cuts::abseta < 5 && Cuts::pT > 10*GeV, {{PID::MUON, PID::ANTIMUON}});
      declare(truthmuons, "Muons0");
      DressedLeptons dressedmuons(photons, truthmuons, 0.2);
      declare(dressedmuons, "Muons1");
      SmearedParticles recomuons(dressedmuons, MUON_EFF_ATLAS_RUN2, MUON_SMEAR_ATLAS_RUN2);
      declare(recomuons, "Muons2");

      TauFinder truthtaus(TauFinder::ANY, Cuts::abseta < 5 && Cuts::pT > 10*GeV);
      declare(truthtaus, "Taus0");
      DressedLeptons dressedtaus(photons, truthtaus, 0.2);
      declare(dressedtaus, "Taus1");
      SmearedParticles recotaus(dressedtaus, TAU_EFF_ATLAS_RUN2, TAU_SMEAR_ATLAS_RUN2);
      declare(recotaus, "Taus2");


      _h_met_true = bookHisto1D("met_true", 30, 0.0, 120);
      _h_met_reco = bookHisto1D("met_reco", 30, 0.0, 120);

      _h_nj_true = bookHisto1D("jet_N_true", 10, -0.5, 9.5);
      _h_nj_reco = bookHisto1D("jet_N_reco", 10, -0.5, 9.5);
      _h_j1pt_true = bookHisto1D("jet_pt1_true", 30, 0.0, 120);
      _h_j1pt_reco = bookHisto1D("jet_pt1_reco", 30, 0.0, 120);
      _h_j1eta_true = bookHisto1D("jet_eta1_true", 20, -5.0, 5.0);
      _h_j1eta_reco = bookHisto1D("jet_eta1_reco", 20, -5.0, 5.0);

      _h_ne_true = bookHisto1D("elec_N_true", 5, -0.5, 4.5);
      _h_ne_reco = bookHisto1D("elec_N_reco", 5, -0.5, 4.5);
      _h_e1pt_true = bookHisto1D("elec_pt1_true", 30, 0, 120);
      _h_e1pt_reco = bookHisto1D("elec_pt1_reco", 30, 0, 120);
      _h_e1eta_true = bookHisto1D("elec_eta1_true", 20, -5.0, 5.0);
      _h_e1eta_reco = bookHisto1D("elec_eta1_reco", 20, -5.0, 5.0);

      _h_nm_true = bookHisto1D("muon_N_true", 5, -0.5, 4.5);
      _h_nm_reco = bookHisto1D("muon_N_reco", 5, -0.5, 4.5);
      _h_m1pt_true = bookHisto1D("muon_pt1_true", 30, 0, 120);
      _h_m1pt_reco = bookHisto1D("muon_pt1_reco", 30, 0, 120);
      _h_m1eta_true = bookHisto1D("muon_eta1_true", 20, -5.0, 5.0);
      _h_m1eta_reco = bookHisto1D("muon_eta1_reco", 20, -5.0, 5.0);

      _h_nt_true = bookHisto1D("tau_N_true", 5, -0.5, 4.5);
      _h_nt_reco = bookHisto1D("tau_N_reco", 5, -0.5, 4.5);
      _h_t1pt_true = bookHisto1D("tau_pt1_true", 30, 0, 120);
      _h_t1pt_reco = bookHisto1D("tau_pt1_reco", 30, 0, 120);
      _h_t1eta_true = bookHisto1D("tau_eta1_true", 20, -5.0, 5.0);
      _h_t1eta_reco = bookHisto1D("tau_eta1_reco", 20, -5.0, 5.0);
    }


    /// Perform the per-event analysis
    void analyze(const Event& event) {
      const double weight = event.weight();

      const Vector3 met0 = apply<MissingMomentum>(event, "MET0").vectorEt();
      const Vector3 met1 = apply<SmearedMET>(event, "MET1").vectorEt();
      const Vector3 met2 = apply<SmearedMET>(event, "MET2").vectorEt();
      MSG_DEBUG("MET = " << met0.mod()/GeV << ", " << met1.mod()/GeV << ", " << met2.mod()/GeV << " GeV");
      _h_met_true->fill(met0.mod()/GeV, weight);
      _h_met_reco->fill(met1.mod()/GeV, weight);
      if (met0.perp() > 0 && met1.perp() > 0 && deltaPhi(met0, met1) > 0.1) {
        MSG_WARNING("Large MET phi change: " << met0.phi()  << " -> " << met1.phi() <<
                    "; dphi = " << deltaPhi(met0, met1));
      }

      const Jets jets0 = apply<JetAlg>(event, "Jets0").jetsByPt(Cuts::pT > 10*GeV);
      const Jets jets1 = apply<JetAlg>(event, "Jets1").jetsByPt(Cuts::pT > 10*GeV);
      const Jets jets2 = apply<JetAlg>(event, "Jets2").jetsByPt(Cuts::pT > 10*GeV);
      const Jets jets3 = apply<JetAlg>(event, "Jets3").jetsByPt(Cuts::pT > 10*GeV);
      MSG_DEBUG("Numbers of jets = " << jets0.size() << " true; "
               << jets1.size() << ", " << jets2.size() << ", " << jets3.size());
      if (!jets0.empty() && !jets2.empty() && deltaPhi(jets0[0], jets2[0]) > 0.1) {
        MSG_DEBUG("Large jet1 phi change (could be a different truth-jet): " <<
                  jets0[0].phi() << " -> " << jets2[0].phi() <<
                  "; dphi = " << deltaPhi(jets0[0], jets2[0]) <<
                  "; pT = " << jets0[0].pT()/GeV << " -> " << jets2[0].pT()/GeV);
      }
      _h_nj_true->fill(jets0.size(), weight);
      _h_nj_reco->fill(jets2.size(), weight);
      if (!jets0.empty()) {
        _h_j1pt_true->fill(jets0.front().pT()/GeV, weight);
        _h_j1eta_true->fill(jets0.front().eta(), weight);
      }
      if (!jets2.empty()) {
        _h_j1pt_reco->fill(jets2.front().pT()/GeV, weight);
        _h_j1eta_reco->fill(jets2.front().eta(), weight);
      }

      const Particles& elecs1 = apply<ParticleFinder>(event, "Electrons1").particlesByPt();
      const Particles& elecs2 = apply<ParticleFinder>(event, "Electrons2").particlesByPt();
      MSG_DEBUG("Numbers of electrons = " << elecs1.size() << " true; " << elecs2.size() << " reco");
      _h_ne_true->fill(elecs1.size(), weight);
      _h_ne_reco->fill(elecs2.size(), weight);
      if (!elecs1.empty()) {
        _h_e1pt_true->fill(elecs1.front().pT()/GeV, weight);
        _h_e1eta_true->fill(elecs1.front().eta(), weight);
      }
      if (!elecs2.empty()) {
        _h_e1pt_reco->fill(elecs2.front().pT()/GeV, weight);
        _h_e1eta_reco->fill(elecs2.front().eta(), weight);
      }

      const Particles& muons1 = apply<ParticleFinder>(event, "Muons1").particlesByPt();
      const Particles& muons2 = apply<ParticleFinder>(event, "Muons2").particlesByPt();
      MSG_DEBUG("Numbers of muons = " << muons1.size() << " true; " << muons2.size() << " reco");
      _h_nm_true->fill(muons1.size(), weight);
      _h_nm_reco->fill(muons2.size(), weight);
      if (!muons1.empty()) {
        _h_m1pt_true->fill(muons1.front().pT()/GeV, weight);
        _h_m1eta_true->fill(muons1.front().eta(), weight);
      }
      if (!muons2.empty()) {
        _h_m1pt_reco->fill(muons2.front().pT()/GeV, weight);
        _h_m1eta_reco->fill(muons2.front().eta(), weight);
      }

      const Particles& taus1 = apply<ParticleFinder>(event, "Taus1").particlesByPt();
      const Particles& taus2 = apply<ParticleFinder>(event, "Taus2").particlesByPt();
      MSG_DEBUG("Numbers of taus = " << taus1.size() << " true; " << taus2.size() << " reco");
      _h_nt_true->fill(taus1.size(), weight);
      _h_nt_reco->fill(taus2.size(), weight);
      if (!taus1.empty()) {
        _h_t1pt_true->fill(taus1.front().pT()/GeV, weight);
        _h_t1eta_true->fill(taus1.front().eta(), weight);
      }
      if (!taus2.empty()) {
        _h_t1pt_reco->fill(taus2.front().pT()/GeV, weight);
        _h_t1eta_reco->fill(taus2.front().eta(), weight);
      }

    }


    /// Normalise histograms etc., after the run
    void finalize() {
      normalize(_h_met_true);
      normalize(_h_met_reco);

      normalize(_h_nj_true);
      normalize(_h_nj_reco);
      normalize(_h_j1pt_true, 1-_h_nj_true->bin(0).area());
      normalize(_h_j1pt_reco, 1-_h_nj_reco->bin(0).area());
      normalize(_h_j1eta_true, 1-_h_nj_true->bin(0).area());
      normalize(_h_j1eta_reco, 1-_h_nj_reco->bin(0).area());

      normalize(_h_ne_true);
      normalize(_h_ne_reco);
      normalize(_h_e1pt_true, 1-_h_ne_true->bin(0).area());
      normalize(_h_e1pt_reco, 1-_h_ne_reco->bin(0).area());
      normalize(_h_e1eta_true, 1-_h_ne_true->bin(0).area());
      normalize(_h_e1eta_reco, 1-_h_ne_reco->bin(0).area());

      normalize(_h_nm_true);
      normalize(_h_nm_reco);
      normalize(_h_m1pt_true, 1-_h_nm_true->bin(0).area());
      normalize(_h_m1pt_reco, 1-_h_nm_reco->bin(0).area());
      normalize(_h_m1eta_true, 1-_h_nm_true->bin(0).area());
      normalize(_h_m1eta_reco, 1-_h_nm_reco->bin(0).area());

      normalize(_h_nt_true);
      normalize(_h_nt_reco);
      normalize(_h_t1pt_true, 1-_h_nt_true->bin(0).area());
      normalize(_h_t1pt_reco, 1-_h_nt_reco->bin(0).area());
      normalize(_h_t1eta_true, 1-_h_nt_true->bin(0).area());
      normalize(_h_t1eta_reco, 1-_h_nt_reco->bin(0).area());
    }

    //@}


  private:

    /// @name Histograms
    //@{
    Histo1DPtr _h_met_true, _h_met_reco;
    Histo1DPtr _h_nj_true, _h_nj_reco, _h_ne_true, _h_ne_reco,  _h_nm_true, _h_nm_reco,  _h_nt_true, _h_nt_reco;
    Histo1DPtr _h_j1pt_true, _h_j1pt_reco, _h_e1pt_true, _h_e1pt_reco,  _h_m1pt_true, _h_m1pt_reco,  _h_t1pt_true, _h_t1pt_reco;
    Histo1DPtr _h_j1eta_true, _h_j1eta_reco, _h_e1eta_true, _h_e1eta_reco,  _h_m1eta_true, _h_m1eta_reco,  _h_t1eta_true, _h_t1eta_reco;
    //@}

  };



  // The hook for the plugin system
  DECLARE_RIVET_PLUGIN(EXAMPLE_SMEAR);


}
EXAMPLE.cc
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
// -*- C++ -*-
#include "Rivet/Analysis.hh"
#include "Rivet/Projections/FinalState.hh"
#include "Rivet/Projections/ChargedFinalState.hh"
#include "Rivet/Projections/FastJets.hh"
#include "Rivet/Projections/Thrust.hh"
#include "Rivet/Projections/Sphericity.hh"

namespace Rivet {


  /// @brief Just measures a few observables as a demo
  class EXAMPLE : public Analysis {
  public:

    /// Constructor
    DEFAULT_RIVET_ANALYSIS_CTOR(EXAMPLE);


    /// @name Analysis methods
    //@{

    /// Set up projections and book histograms
    void init() {
      // Projections
      const FinalState cnfs(Cuts::abseta < 2.5 && Cuts::pT > 500*MeV);
      const ChargedFinalState cfs(cnfs);
      declare(cnfs, "FS");
      declare(cfs, "CFS");
      declare(FastJets(cnfs, FastJets::ANTIKT, 0.4), "Jets");
      declare(Thrust(cfs), "Thrust");
      declare(Sphericity(cfs), "Sphericity");

      // Histograms
      _histTot         = bookHisto1D("TotalMult", 100, -0.5, 99.5);
      _histChTot       = bookHisto1D("TotalChMult", 50, -1.0, 99.0);
      _histHadrTot     = bookHisto1D("HadrTotalMult", 100, -0.5, 99.5);
      _histHadrChTot   = bookHisto1D("HadrTotalChMult", 50, -1.0, 99.0);
      _histMajor       = bookHisto1D("Major", 10, 0.0, 0.6);
      _histSphericity  = bookHisto1D("Sphericity", 10, 0.0, 0.8);
      _histAplanarity  = bookHisto1D("Aplanarity", 10, 0.0, 0.3);
      _histThrust      = bookHisto1D("Thrust", { 0.5, 0.6, 0.7, 0.80, 0.85, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0 });
    }


    /// Do the analysis
    void analyze(const Event& event) {
      // Make sure to always include the event weight in histogram fills!
      const double weight = event.weight();

      const Particles& cnparticles = apply<FinalState>(event, "FS").particles();
      MSG_DEBUG("Total multiplicity = " << cnparticles.size());
      _histTot->fill(cnparticles.size(), weight);
      int cnhadronmult = 0;
      for (const Particle& p : cnparticles)
        if (isHadron(p)) cnhadronmult += 1;
      MSG_DEBUG("Hadron multiplicity = " << cnhadronmult);
      _histHadrTot->fill(cnhadronmult, weight);

      const Particles& cparticles = apply<FinalState>(event, "CFS").particles();
      MSG_DEBUG("Total charged multiplicity = " << cparticles.size());
      _histChTot->fill(cparticles.size(), weight);
      int chadronmult = 0;
      for (const Particle& p : cparticles)
        if (isHadron(p)) chadronmult += 1;
      MSG_DEBUG("Hadron charged multiplicity = " << chadronmult);
      _histHadrChTot->fill(chadronmult, weight);

      const Thrust& t = apply<Thrust>(event, "Thrust");
      MSG_DEBUG("Thrust = " << t.thrust());
      _histThrust->fill(t.thrust(), weight);
      _histMajor->fill(t.thrustMajor(), weight);

      const Sphericity& s = apply<Sphericity>(event, "Sphericity");
      MSG_DEBUG("Sphericity = " << s.sphericity());
      _histSphericity->fill(s.sphericity(), weight);
      MSG_DEBUG("Aplanarity = " << s.aplanarity());
      _histAplanarity->fill(s.aplanarity(), weight);

      const Jets jets = apply<FastJets>(event, "Jets").jets(Cuts::pT > 5*GeV);
      const size_t num_b_jets = count_if(jets.begin(), jets.end(), hasBTag(Cuts::pT > 500*MeV));
      MSG_DEBUG("Num B-jets with pT > 5 GeV = " << num_b_jets);
    }


    /// Finalize
    void finalize() {
      normalize({_histTot, _histChTot, _histHadrTot, _histHadrChTot, _histThrust, _histMajor, _histSphericity, _histAplanarity});
    }

    //@}


    //@{
    /// Histograms
    Histo1DPtr _histTot, _histChTot, _histHadrTot, _histHadrChTot, _histThrust, _histMajor, _histSphericity, _histAplanarity;
    //@}

  };


  // The hook for the plugin system
  DECLARE_RIVET_PLUGIN(EXAMPLE);

}