CMS 3D CMS Logo

RootTreeHandler.h
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include <TFile.h>
4 #include <TTree.h>
5 
9 #include <TH1F.h>
10 #include <cstdlib>
11 #include <vector>
12 
13 typedef std::vector<std::pair<lorentzVector, lorentzVector> > MuonPairVector;
14 typedef std::vector<std::pair<MuScleFitMuon, MuScleFitMuon> > MuonPairExtendedVector;
15 
25 public:
26  // void writeTree( const TString & fileName, const MuonPairVector * savedPair, const int muonType = 0,
27  // const MuonPairVector * genPair = 0, const bool saveAll = false )
28  void writeTree(const TString& fileName,
29  const std::vector<MuonPair>* savedPair,
30  const int muonType = 0,
31  const std::vector<GenMuonPair>* genPair = nullptr,
32  const bool saveAll = false) {
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  }
83 
84  // void readTree( const int maxEvents, const TString & fileName, MuonPairVector * savedPair,
85  // const int muonType, MuonPairVector * genPair = 0 )
86  void readTree(const int maxEvents,
87  const TString& fileName,
88  MuonPairVector* savedPair,
89  const int muonType,
90  std::vector<std::pair<unsigned int, unsigned long long> >* evtRun,
91  MuonPairVector* genPair = nullptr) {
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  }
127 
128  void readTree(const int maxEvents,
129  const TString& fileName,
130  MuonPairExtendedVector* savedPair,
131  const int muonType,
132  std::vector<std::pair<unsigned int, unsigned long long> >* evtRun,
133  MuonPairExtendedVector* genPair = nullptr) {
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  }
167 
169  void readTree(const int maxEvents,
170  const TString& fileName,
171  std::vector<MuonPair>* savedPair,
172  const int muonType,
173  std::vector<GenMuonPair>* genPair = nullptr) {
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  }
203 };
lorentzVector p4() const
Definition: Muon.h:40
TkSoAView< TrackerTraits > HitToTuple< TrackerTraits > const *__restrict__ int32_t int32_t int iev
MuScleFitMuon mu1
Definition: GenMuonPair.h:51
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)
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
void writeTree(const TString &fileName, const std::vector< MuonPair > *savedPair, const int muonType=0, const std::vector< GenMuonPair > *genPair=nullptr, const bool saveAll=false)
MuScleFitEvent event
Definition: MuonPair.h:33
MuScleFitMuon mu1
Definition: MuonPair.h:31
MuScleFitMuon mu2
Definition: MuonPair.h:32
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)
std::vector< std::pair< MuScleFitMuon, MuScleFitMuon > > MuonPairExtendedVector
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
UInt_t run() const
Definition: Event.h:24
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.
std::vector< std::pair< lorentzVector, lorentzVector > > MuonPairVector
ULong64_t event() const
Definition: Event.h:25
Definition: tree.py:1
MuScleFitMuon mu2
Definition: GenMuonPair.h:52
def exit(msg="")