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 
24 {
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, const std::vector<MuonPair> * savedPair, const int muonType = 0,
29  const std::vector<GenMuonPair> * genPair = 0, const bool saveAll = false )
30  {
31  lorentzVector emptyLorentzVector(0,0,0,0);
32  TFile * f1 = new TFile(fileName, "RECREATE");
33  TTree * tree = new TTree("T", "Muon pairs");
34  MuonPair * muonPair = new MuonPair;
35  GenMuonPair * genMuonPair = new GenMuonPair;
36  // MuonPair * genMuonPair = new MuonPair;
37  tree->Branch("event", "MuonPair", &muonPair);
38  if( genPair != 0 ) {
39  tree->Branch("genEvent", "GenMuonPair", &genMuonPair);
40  // tree->Branch("genEvent", "MuonPair", &genMuonPair);
41 
42  if( savedPair->size() != genPair->size() ) {
43  std::cout << "Error: savedPair size ("
44  << savedPair->size() <<") and genPair size ("
45  << genPair->size() <<") are different. This is severe and I will not write the tree." << std::endl;
46  exit(1);
47  }
48  }
49  std::cout << "savedPair->size() is "<<savedPair->size()<< std::endl;
50  std::vector<MuonPair>::const_iterator muonPairIt = savedPair->begin();
51  unsigned int iev = 0;
52  for( ; muonPairIt != savedPair->end(); ++muonPairIt, ++iev ) {
53 
54  if( saveAll || ( (muonPairIt->mu1.p4() != emptyLorentzVector) && (muonPairIt->mu2.p4() != emptyLorentzVector) ) ) {
55 
56  // muonPair->setPair(muonType, std::make_pair(muonPairIt->first, muonPairIt->second));
57  muonPair->copy(*muonPairIt);
58 
59  // if( genPair != 0 && genPair->size() != 0 ) {
60  // genMuonPair->setPair(muonId, std::make_pair((*genPair)[iev].first, (*genPair)[iev].second));
61  // genMuonPair->mu1 = ((*genPair)[iev].first);
62  // genMuonPair->mu2 = ((*genPair)[iev].second);
63  // }
64  if( genPair != 0 ) {
65  genMuonPair->copy((*genPair)[iev]);
66  }
67 
68  tree->Fill();
69  }
70  // // Tree filled. Clear the map for the next event.
71  // muonPair->muonPairs.clear();
72  }
73 
74  // Save provenance information in the TFile
75  TH1F muonTypeHisto("MuonType", "MuonType", 40, -20, 20);
76  muonTypeHisto.Fill(muonType);
77  muonTypeHisto.Write();
78  MuScleFitProvenance provenance(muonType);
79  provenance.Write();
80 
81  f1->Write();
82  f1->Close();
83  }
84 
85  // void readTree( const int maxEvents, const TString & fileName, MuonPairVector * savedPair,
86  // const int muonType, MuonPairVector * genPair = 0 )
87  void readTree( const int maxEvents, const TString & fileName, MuonPairVector * savedPair,
88  const int muonType, std::vector<std::pair<int, int> > * evtRun, MuonPairVector * genPair = 0 )
89  {
90  TFile * file = TFile::Open(fileName, "READ");
91  if( file->IsOpen() ) {
92  TTree * tree = (TTree*)file->Get("T");
93  MuonPair * muonPair = 0;
94  GenMuonPair * genMuonPair = 0;
95  // MuonPair * genMuonPair = 0;
96  tree->SetBranchAddress("event",&muonPair);
97  if( genPair != 0 ) {
98  tree->SetBranchAddress("genEvent",&genMuonPair);
99  }
100 
101  Long64_t nentries = tree->GetEntries();
102  if( (maxEvents != -1) && (nentries > maxEvents) ) nentries = maxEvents;
103  for( Long64_t i=0; i<nentries; ++i ) {
104  tree->GetEntry(i);
105  //std::cout << "Reco muon1, pt = " << muonPair->mu1 << "; Reco muon2, pt = " << muonPair->mu2 << std::endl;
106  savedPair->push_back(std::make_pair(muonPair->mu1.p4(), muonPair->mu2.p4()));
107  evtRun->push_back(std::make_pair(muonPair->event.event(), muonPair->event.run()));
108  // savedPair->push_back(muonPair->getPair(muonType));
109  if( genPair != 0 ) {
110  genPair->push_back(std::make_pair(genMuonPair->mu1.p4(), genMuonPair->mu2.p4()));
111  //std::cout << "Gen muon1, pt = " << genMuonPair->mu1 << "; Gen muon2, pt = " << genMuonPair->mu2 << std::endl;
112  // genPair->push_back(genMuonPair->getPair(muonId));
113  }
114  }
115  }
116  else {
117  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;
118  exit(1);
119  }
120  file->Close();
121  }
122 
124  void readTree( const int maxEvents, const TString & fileName, std::vector<MuonPair> * savedPair,
125  const int muonType, std::vector<GenMuonPair> * 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 
137  Long64_t nentries = tree->GetEntries();
138  if( (maxEvents != -1) && (nentries > maxEvents) ) nentries = maxEvents;
139  for( Long64_t i=0; i<nentries; ++i ) {
140  tree->GetEntry(i);
141  savedPair->push_back(*muonPair);
142  if( genPair != 0 ) {
143  genPair->push_back(*genMuonPair);
144  }
145  }
146  }
147  else {
148  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;
149  exit(1);
150  }
151  file->Close();
152  }
153 
154 };
void readTree(const int maxEvents, const TString &fileName, MuonPairVector *savedPair, const int muonType, std::vector< std::pair< int, int > > *evtRun, MuonPairVector *genPair=0)
int i
Definition: DBlmapReader.cc:9
UInt_t event() const
Definition: Event.h:27
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< 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:121
lorentzVector p4() const
Definition: Muon.h:45
MuScleFitMuon mu2
Definition: GenMuonPair.h:55
UInt_t run() const
Definition: Event.h:26