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

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 88 of file RootTreeHandler.h.

References gather_cfg::cout, MuScleFitEvent::event(), MuonPair::event, cmsRelvalreport::exit, FrontierConditions_GlobalTag_cff::file, 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().

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

References gather_cfg::cout, MuScleFitEvent::event(), MuonPair::event, cmsRelvalreport::exit, FrontierConditions_GlobalTag_cff::file, mps_fire::i, Data_TkAlMinBias_Run2018C_PromptReco_v3_cff::maxEvents, MuonPair::mu1, GenMuonPair::mu1, MuonPair::mu2, GenMuonPair::mu2, and MuScleFitEvent::run().

126  {
127  TFile * file = TFile::Open(fileName, "READ");
128  if (file->IsOpen()) {
129  TTree * tree = (TTree*)file->Get("T");
130  MuonPair * muonPair = nullptr;
131  GenMuonPair * genMuonPair = nullptr;
132  tree->SetBranchAddress("event", &muonPair);
133  if (genPair != nullptr) {
134  tree->SetBranchAddress("genEvent", &genMuonPair);
135  }
136  Long64_t nentries = tree->GetEntries();
137  if ((maxEvents != -1) && (nentries > maxEvents)) nentries = maxEvents;
138  for (Long64_t i=0; i<nentries; ++i) {
139  tree->GetEntry(i);
140  //std::cout << "Reco muon1, pt = " << muonPair->mu1 << "; Reco muon2, pt = " << muonPair->mu2 << std::endl;
141  savedPair->push_back(std::make_pair(muonPair->mu1, muonPair->mu2));
142  evtRun->push_back(std::make_pair(muonPair->event.event(), muonPair->event.run()));
143  // savedPair->push_back(muonPair->getPair(muonType));
144  if (genPair != nullptr) {
145  genPair->push_back(std::make_pair(genMuonPair->mu1, genMuonPair->mu2));
146  //std::cout << "Gen muon1, pt = " << genMuonPair->mu1 << "; Gen muon2, pt = " << genMuonPair->mu2 << std::endl;
147  // genPair->push_back(genMuonPair->getPair(muonId));
148  }
149  }
150  }
151  else {
152  std::cout << "ERROR: no file " << fileName << " found. Please, correct the file name or specify an empty field in the InputRootTreeFileName parameter to read events from the edm source." << std::endl;
153  exit(1);
154  }
155  file->Close();
156  }
MuScleFitMuon mu1
Definition: GenMuonPair.h:54
MuScleFitEvent event
Definition: MuonPair.h:40
MuScleFitMuon mu1
Definition: MuonPair.h:38
MuScleFitMuon mu2
Definition: MuonPair.h:39
Definition: tree.py:1
ULong64_t event() const
Definition: Event.h:29
MuScleFitMuon mu2
Definition: GenMuonPair.h:55
UInt_t run() const
Definition: Event.h:28
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 160 of file RootTreeHandler.h.

References gather_cfg::cout, cmsRelvalreport::exit, FrontierConditions_GlobalTag_cff::file, mps_fire::i, and Data_TkAlMinBias_Run2018C_PromptReco_v3_cff::maxEvents.

162  {
163  TFile * file = TFile::Open(fileName, "READ");
164  if (file->IsOpen()) {
165  TTree * tree = (TTree*)file->Get("T");
166  MuonPair * muonPair = nullptr;
167  GenMuonPair * genMuonPair = nullptr;
168  tree->SetBranchAddress("event", &muonPair);
169  if (genPair != nullptr) {
170  tree->SetBranchAddress("genEvent", &genMuonPair);
171  }
172 
173  Long64_t nentries = tree->GetEntries();
174  if ((maxEvents != -1) && (nentries > maxEvents)) nentries = maxEvents;
175  for (Long64_t i=0; i<nentries; ++i) {
176  tree->GetEntry(i);
177  savedPair->push_back(*muonPair);
178  if (genPair != nullptr) {
179  genPair->push_back(*genMuonPair);
180  }
181  }
182  }
183  else {
184  std::cout << "ERROR: no file " << fileName << " found. Please, correct the file name or specify an empty field in the InputRootTreeFileName parameter to read events from the edm source." << std::endl;
185  exit(1);
186  }
187  file->Close();
188  }
Definition: tree.py:1
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 29 of file RootTreeHandler.h.

References MuonPair::copy(), GenMuonPair::copy(), gather_cfg::cout, cmsRelvalreport::exit, and connectstrParser::f1.

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

31  {
32  lorentzVector emptyLorentzVector(0,0,0,0);
33  TFile * f1 = new TFile(fileName, "RECREATE");
34  TTree * tree = new TTree("T", "Muon pairs");
35  MuonPair * muonPair = new MuonPair;
36  GenMuonPair * genMuonPair = new GenMuonPair;
37  // MuonPair * genMuonPair = new MuonPair;
38  tree->Branch("event", "MuonPair", &muonPair);
39  if( genPair != nullptr ) {
40  tree->Branch("genEvent", "GenMuonPair", &genMuonPair);
41  // tree->Branch("genEvent", "MuonPair", &genMuonPair);
42 
43  if( savedPair->size() != genPair->size() ) {
44  std::cout << "Error: savedPair size ("
45  << savedPair->size() <<") and genPair size ("
46  << genPair->size() <<") 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 
55  if( saveAll || ( (muonPairIt->mu1.p4() != emptyLorentzVector) && (muonPairIt->mu2.p4() != emptyLorentzVector) ) ) {
56 
57  // muonPair->setPair(muonType, std::make_pair(muonPairIt->first, muonPairIt->second));
58  muonPair->copy(*muonPairIt);
59 
60  // if( genPair != 0 && genPair->size() != 0 ) {
61  // genMuonPair->setPair(muonId, std::make_pair((*genPair)[iev].first, (*genPair)[iev].second));
62  // genMuonPair->mu1 = ((*genPair)[iev].first);
63  // genMuonPair->mu2 = ((*genPair)[iev].second);
64  // }
65  if( genPair != nullptr ) {
66  genMuonPair->copy((*genPair)[iev]);
67  }
68 
69  tree->Fill();
70  }
71  // // Tree filled. Clear the map for the next event.
72  // muonPair->muonPairs.clear();
73  }
74 
75  // Save provenance information in the TFile
76  TH1F muonTypeHisto("MuonType", "MuonType", 40, -20, 20);
77  muonTypeHisto.Fill(muonType);
78  muonTypeHisto.Write();
79  MuScleFitProvenance provenance(muonType);
80  provenance.Write();
81 
82  f1->Write();
83  f1->Close();
84  }
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
void copy(const MuonPair &copyPair)
Used to copy the content of another MuonPair.
Definition: MuonPair.h:31
void copy(const GenMuonPair &copyPair)
Used to copy the content of another GenMuonPair.
Definition: GenMuonPair.h:46
Definition: tree.py:1