CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 <stdlib.h>
11 #include <vector>
12 
13 typedef std::vector<std::pair<lorentzVector,lorentzVector> > MuonPairVector;
14 typedef std::vector<std::pair<MuScleFitMuon, MuScleFitMuon> > MuonPairExtendedVector;
15 
25 {
26 public:
27  // void writeTree( const TString & fileName, const MuonPairVector * savedPair, const int muonType = 0,
28  // const MuonPairVector * genPair = 0, const bool saveAll = false )
29  void writeTree( const TString & fileName, const std::vector<MuonPair> * savedPair, const int muonType = 0,
30  const std::vector<GenMuonPair> * genPair = 0, const bool saveAll = false )
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 != 0 ) {
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 != 0 ) {
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  }
85 
86  // void readTree( const int maxEvents, const TString & fileName, MuonPairVector * savedPair,
87  // const int muonType, MuonPairVector * genPair = 0 )
88  void readTree(const int maxEvents, const TString & fileName, MuonPairVector * savedPair,
89  const int muonType, std::vector<std::pair<unsigned int, unsigned long long> > * evtRun, MuonPairVector * genPair = 0)
90  {
91  TFile * file = TFile::Open(fileName, "READ");
92  if (file->IsOpen()) {
93  TTree * tree = (TTree*)file->Get("T");
94  MuonPair * muonPair = 0;
95  GenMuonPair * genMuonPair = 0;
96  // MuonPair * genMuonPair = 0;
97  tree->SetBranchAddress("event", &muonPair);
98  if (genPair != 0) {
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 != 0) {
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  }
123 
124  void readTree(const int maxEvents, const TString & fileName, MuonPairExtendedVector * savedPair,
125  const int muonType, std::vector<std::pair<unsigned int, unsigned long long> > * evtRun, MuonPairExtendedVector * genPair = 0)
126  {
127  TFile * file = TFile::Open(fileName, "READ");
128  if (file->IsOpen()) {
129  TTree * tree = (TTree*)file->Get("T");
130  MuonPair * muonPair = 0;
131  GenMuonPair * genMuonPair = 0;
132  tree->SetBranchAddress("event", &muonPair);
133  if (genPair != 0) {
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 != 0) {
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  }
157 
158 
160  void readTree(const int maxEvents, const TString & fileName, std::vector<MuonPair> * savedPair,
161  const int muonType, std::vector<GenMuonPair> * genPair = 0)
162  {
163  TFile * file = TFile::Open(fileName, "READ");
164  if (file->IsOpen()) {
165  TTree * tree = (TTree*)file->Get("T");
166  MuonPair * muonPair = 0;
167  GenMuonPair * genMuonPair = 0;
168  tree->SetBranchAddress("event", &muonPair);
169  if (genPair != 0) {
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 != 0) {
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  }
189 
190 };
int i
Definition: DBlmapReader.cc:9
MuScleFitMuon mu1
Definition: GenMuonPair.h:54
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
MuScleFitEvent event
Definition: MuonPair.h:40
MuScleFitMuon mu1
Definition: MuonPair.h:38
MuScleFitMuon mu2
Definition: MuonPair.h:39
std::vector< std::pair< MuScleFitMuon, MuScleFitMuon > > MuonPairExtendedVector
std::vector< std::pair< lorentzVector, lorentzVector > > MuonPairVector
void writeTree(const TString &fileName, const std::vector< MuonPair > *savedPair, const int muonType=0, const std::vector< GenMuonPair > *genPair=0, const bool saveAll=false)
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
void readTree(const int maxEvents, const TString &fileName, std::vector< MuonPair > *savedPair, const int muonType, std::vector< GenMuonPair > *genPair=0)
Used to read the external trees.
tuple cout
Definition: gather_cfg.py:145
lorentzVector p4() const
Definition: Muon.h:45
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=0)
ULong64_t event() const
Definition: Event.h:29
MuScleFitMuon mu2
Definition: GenMuonPair.h:55
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=0)
UInt_t run() const
Definition: Event.h:28