CMS 3D CMS Logo

List of all members | Public Member Functions
RootTreeHandler Class Reference

#include <RootTreeHandler.h>

Public Member Functions

void readTree (const int maxEvents, const TString &fileName, MuonPairVector *savedPair, const int muonType, std::vector< std::pair< unsigned int, unsigned long long > > *evtRun, MuonPairVector *genPair=nullptr)
 
void readTree (const int maxEvents, const TString &fileName, MuonPairExtendedVector *savedPair, const int muonType, std::vector< std::pair< unsigned int, unsigned long long > > *evtRun, MuonPairExtendedVector *genPair=nullptr)
 
void readTree (const int maxEvents, const TString &fileName, std::vector< MuonPair > *savedPair, const int muonType, std::vector< GenMuonPair > *genPair=nullptr)
 Used to read the external trees. More...
 
void writeTree (const TString &fileName, const std::vector< MuonPair > *savedPair, const int muonType=0, const std::vector< GenMuonPair > *genPair=nullptr, const bool saveAll=false)
 

Detailed Description

This class can be used to save all the muon pairs (and gen muon pairs if any) to a root tree.
The writeTree method gets the name of the file to store the tree and the savedPair (and possibly genPair) vector of muon pairs.
Likewise, the readTree method takes the same arguments. It reads back from the file with the given name the pairs and stores them in the given savedPair (and genPair) vector.

Definition at line 24 of file RootTreeHandler.h.

Member Function Documentation

◆ readTree() [1/3]

void RootTreeHandler::readTree ( const int  maxEvents,
const TString &  fileName,
MuonPairVector savedPair,
const int  muonType,
std::vector< std::pair< unsigned int, unsigned long long > > *  evtRun,
MuonPairVector genPair = nullptr 
)
inline

Definition at line 86 of file RootTreeHandler.h.

References gather_cfg::cout, MuScleFitEvent::event(), MuonPair::event, beamvalidation::exit(), geometryDiff::file, MillePedeFileConverter_cfg::fileName, mps_fire::i, Data_TkAlMinBias_Run2018C_PromptReco_v3_cff::maxEvents, MuonPair::mu1, GenMuonPair::mu1, MuonPair::mu2, GenMuonPair::mu2, MuScleFitMuon::p4(), and MuScleFitEvent::run().

Referenced by ResolutionAnalyzer::analyze(), TreeSplitter::endJob(), ErrorsAnalyzer::fillHistograms(), ErrorsPropagationAnalyzer::fillHistograms(), main(), and MuScleFit::selectMuons().

91  {
92  TFile* file = TFile::Open(fileName, "READ");
93  if (file->IsOpen()) {
94  TTree* tree = (TTree*)file->Get("T");
95  MuonPair* muonPair = nullptr;
96  GenMuonPair* genMuonPair = nullptr;
97  // MuonPair * genMuonPair = 0;
98  tree->SetBranchAddress("event", &muonPair);
99  if (genPair != nullptr) {
100  tree->SetBranchAddress("genEvent", &genMuonPair);
101  }
102 
103  Long64_t nentries = tree->GetEntries();
104  if ((maxEvents != -1) && (nentries > maxEvents))
105  nentries = maxEvents;
106  for (Long64_t i = 0; i < nentries; ++i) {
107  tree->GetEntry(i);
108  //std::cout << "Reco muon1, pt = " << muonPair->mu1 << "; Reco muon2, pt = " << muonPair->mu2 << std::endl;
109  savedPair->push_back(std::make_pair(muonPair->mu1.p4(), muonPair->mu2.p4()));
110  evtRun->push_back(std::make_pair(muonPair->event.event(), muonPair->event.run()));
111  // savedPair->push_back(muonPair->getPair(muonType));
112  if (genPair != nullptr) {
113  genPair->push_back(std::make_pair(genMuonPair->mu1.p4(), genMuonPair->mu2.p4()));
114  //std::cout << "Gen muon1, pt = " << genMuonPair->mu1 << "; Gen muon2, pt = " << genMuonPair->mu2 << std::endl;
115  // genPair->push_back(genMuonPair->getPair(muonId));
116  }
117  }
118  } else {
119  std::cout << "ERROR: no file " << fileName
120  << " found. Please, correct the file name or specify an empty field in the InputRootTreeFileName "
121  "parameter to read events from the edm source."
122  << std::endl;
123  exit(1);
124  }
125  file->Close();
126  }
lorentzVector p4() const
Definition: Muon.h:40
MuScleFitMuon mu1
Definition: GenMuonPair.h:51
MuScleFitEvent event
Definition: MuonPair.h:33
MuScleFitMuon mu1
Definition: MuonPair.h:31
MuScleFitMuon mu2
Definition: MuonPair.h:32
UInt_t run() const
Definition: Event.h:24
ULong64_t event() const
Definition: Event.h:25
Definition: tree.py:1
MuScleFitMuon mu2
Definition: GenMuonPair.h:52
def exit(msg="")

◆ readTree() [2/3]

void RootTreeHandler::readTree ( const int  maxEvents,
const TString &  fileName,
MuonPairExtendedVector savedPair,
const int  muonType,
std::vector< std::pair< unsigned int, unsigned long long > > *  evtRun,
MuonPairExtendedVector genPair = nullptr 
)
inline

Definition at line 128 of file RootTreeHandler.h.

References gather_cfg::cout, MuScleFitEvent::event(), MuonPair::event, beamvalidation::exit(), geometryDiff::file, MillePedeFileConverter_cfg::fileName, mps_fire::i, Data_TkAlMinBias_Run2018C_PromptReco_v3_cff::maxEvents, MuonPair::mu1, GenMuonPair::mu1, MuonPair::mu2, GenMuonPair::mu2, and MuScleFitEvent::run().

133  {
134  TFile* file = TFile::Open(fileName, "READ");
135  if (file->IsOpen()) {
136  TTree* tree = (TTree*)file->Get("T");
137  MuonPair* muonPair = nullptr;
138  GenMuonPair* genMuonPair = nullptr;
139  tree->SetBranchAddress("event", &muonPair);
140  if (genPair != nullptr) {
141  tree->SetBranchAddress("genEvent", &genMuonPair);
142  }
143  Long64_t nentries = tree->GetEntries();
144  if ((maxEvents != -1) && (nentries > maxEvents))
145  nentries = maxEvents;
146  for (Long64_t i = 0; i < nentries; ++i) {
147  tree->GetEntry(i);
148  //std::cout << "Reco muon1, pt = " << muonPair->mu1 << "; Reco muon2, pt = " << muonPair->mu2 << std::endl;
149  savedPair->push_back(std::make_pair(muonPair->mu1, muonPair->mu2));
150  evtRun->push_back(std::make_pair(muonPair->event.event(), muonPair->event.run()));
151  // savedPair->push_back(muonPair->getPair(muonType));
152  if (genPair != nullptr) {
153  genPair->push_back(std::make_pair(genMuonPair->mu1, genMuonPair->mu2));
154  //std::cout << "Gen muon1, pt = " << genMuonPair->mu1 << "; Gen muon2, pt = " << genMuonPair->mu2 << std::endl;
155  // genPair->push_back(genMuonPair->getPair(muonId));
156  }
157  }
158  } else {
159  std::cout << "ERROR: no file " << fileName
160  << " found. Please, correct the file name or specify an empty field in the InputRootTreeFileName "
161  "parameter to read events from the edm source."
162  << std::endl;
163  exit(1);
164  }
165  file->Close();
166  }
MuScleFitMuon mu1
Definition: GenMuonPair.h:51
MuScleFitEvent event
Definition: MuonPair.h:33
MuScleFitMuon mu1
Definition: MuonPair.h:31
MuScleFitMuon mu2
Definition: MuonPair.h:32
UInt_t run() const
Definition: Event.h:24
ULong64_t event() const
Definition: Event.h:25
Definition: tree.py:1
MuScleFitMuon mu2
Definition: GenMuonPair.h:52
def exit(msg="")

◆ readTree() [3/3]

void RootTreeHandler::readTree ( const int  maxEvents,
const TString &  fileName,
std::vector< MuonPair > *  savedPair,
const int  muonType,
std::vector< GenMuonPair > *  genPair = nullptr 
)
inline

Used to read the external trees.

Definition at line 169 of file RootTreeHandler.h.

References gather_cfg::cout, beamvalidation::exit(), geometryDiff::file, MillePedeFileConverter_cfg::fileName, mps_fire::i, and Data_TkAlMinBias_Run2018C_PromptReco_v3_cff::maxEvents.

173  {
174  TFile* file = TFile::Open(fileName, "READ");
175  if (file->IsOpen()) {
176  TTree* tree = (TTree*)file->Get("T");
177  MuonPair* muonPair = nullptr;
178  GenMuonPair* genMuonPair = nullptr;
179  tree->SetBranchAddress("event", &muonPair);
180  if (genPair != nullptr) {
181  tree->SetBranchAddress("genEvent", &genMuonPair);
182  }
183 
184  Long64_t nentries = tree->GetEntries();
185  if ((maxEvents != -1) && (nentries > maxEvents))
186  nentries = maxEvents;
187  for (Long64_t i = 0; i < nentries; ++i) {
188  tree->GetEntry(i);
189  savedPair->push_back(*muonPair);
190  if (genPair != nullptr) {
191  genPair->push_back(*genMuonPair);
192  }
193  }
194  } else {
195  std::cout << "ERROR: no file " << fileName
196  << " found. Please, correct the file name or specify an empty field in the InputRootTreeFileName "
197  "parameter to read events from the edm source."
198  << std::endl;
199  exit(1);
200  }
201  file->Close();
202  }
Definition: tree.py:1
def exit(msg="")

◆ writeTree()

void RootTreeHandler::writeTree ( const TString &  fileName,
const std::vector< MuonPair > *  savedPair,
const int  muonType = 0,
const std::vector< GenMuonPair > *  genPair = nullptr,
const bool  saveAll = false 
)
inline

Definition at line 28 of file RootTreeHandler.h.

References MuonPair::copy(), GenMuonPair::copy(), gather_cfg::cout, beamvalidation::exit(), DeadROC_duringRun::f1, MillePedeFileConverter_cfg::fileName, caHitNtupletGeneratorKernels::iev, and crab_script::provenance.

Referenced by TreeSplitter::endJob(), main(), and MuScleFit::~MuScleFit().

32  {
33  lorentzVector emptyLorentzVector(0, 0, 0, 0);
34  TFile* f1 = new TFile(fileName, "RECREATE");
35  TTree* tree = new TTree("T", "Muon pairs");
36  MuonPair* muonPair = new MuonPair;
37  GenMuonPair* genMuonPair = new GenMuonPair;
38  // MuonPair * genMuonPair = new MuonPair;
39  tree->Branch("event", "MuonPair", &muonPair);
40  if (genPair != nullptr) {
41  tree->Branch("genEvent", "GenMuonPair", &genMuonPair);
42  // tree->Branch("genEvent", "MuonPair", &genMuonPair);
43 
44  if (savedPair->size() != genPair->size()) {
45  std::cout << "Error: savedPair size (" << savedPair->size() << ") and genPair size (" << genPair->size()
46  << ") are different. This is severe and I will not write the tree." << std::endl;
47  exit(1);
48  }
49  }
50  std::cout << "savedPair->size() is " << savedPair->size() << std::endl;
51  std::vector<MuonPair>::const_iterator muonPairIt = savedPair->begin();
52  unsigned int iev = 0;
53  for (; muonPairIt != savedPair->end(); ++muonPairIt, ++iev) {
54  if (saveAll || ((muonPairIt->mu1.p4() != emptyLorentzVector) && (muonPairIt->mu2.p4() != emptyLorentzVector))) {
55  // muonPair->setPair(muonType, std::make_pair(muonPairIt->first, muonPairIt->second));
56  muonPair->copy(*muonPairIt);
57 
58  // if( genPair != 0 && genPair->size() != 0 ) {
59  // genMuonPair->setPair(muonId, std::make_pair((*genPair)[iev].first, (*genPair)[iev].second));
60  // genMuonPair->mu1 = ((*genPair)[iev].first);
61  // genMuonPair->mu2 = ((*genPair)[iev].second);
62  // }
63  if (genPair != nullptr) {
64  genMuonPair->copy((*genPair)[iev]);
65  }
66 
67  tree->Fill();
68  }
69  // // Tree filled. Clear the map for the next event.
70  // muonPair->muonPairs.clear();
71  }
72 
73  // Save provenance information in the TFile
74  TH1F muonTypeHisto("MuonType", "MuonType", 40, -20, 20);
75  muonTypeHisto.Fill(muonType);
76  muonTypeHisto.Write();
78  provenance.Write();
79 
80  f1->Write();
81  f1->Close();
82  }
TkSoAView< TrackerTraits > HitToTuple< TrackerTraits > const *__restrict__ int32_t int32_t int iev
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
void copy(const MuonPair &copyPair)
Used to copy the content of another MuonPair.
Definition: MuonPair.h:25
void copy(const GenMuonPair &copyPair)
Used to copy the content of another GenMuonPair.
Definition: GenMuonPair.h:44
Definition: tree.py:1
def exit(msg="")