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 != emptyLorentzVector) && (muonPairIt->mu2 != 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  savedPair->push_back(std::make_pair(muonPair->mu1, muonPair->mu2));
106  evtRun->push_back(std::make_pair(muonPair->event, muonPair->run));
107  // savedPair->push_back(muonPair->getPair(muonType));
108  if( genPair != 0 ) {
109  genPair->push_back(std::make_pair(genMuonPair->mu1, genMuonPair->mu2));
110  // genPair->push_back(genMuonPair->getPair(muonId));
111  }
112  }
113  }
114  else {
115  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;
116  exit(1);
117  }
118  file->Close();
119  }
120 
122  void readTree( const int maxEvents, const TString & fileName, std::vector<MuonPair> * savedPair,
123  const int muonType, std::vector<GenMuonPair> * genPair = 0 )
124  {
125  TFile * file = TFile::Open(fileName, "READ");
126  if( file->IsOpen() ) {
127  TTree * tree = (TTree*)file->Get("T");
128  MuonPair * muonPair = 0;
129  GenMuonPair * genMuonPair = 0;
130  tree->SetBranchAddress("event",&muonPair);
131  if( genPair != 0 ) {
132  tree->SetBranchAddress("genEvent",&genMuonPair);
133  }
134 
135  Long64_t nentries = tree->GetEntries();
136  if( (maxEvents != -1) && (nentries > maxEvents) ) nentries = maxEvents;
137  for( Long64_t i=0; i<nentries; ++i ) {
138  tree->GetEntry(i);
139  savedPair->push_back(*muonPair);
140  if( genPair != 0 ) {
141  genPair->push_back(*genMuonPair);
142  }
143  }
144  }
145  else {
146  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;
147  exit(1);
148  }
149  file->Close();
150  }
151 
152 };
void readTree(const int maxEvents, const TString &fileName, MuonPairVector *savedPair, const int muonType, std::vector< std::pair< int, int > > *evtRun, MuonPairVector *genPair=0)
UInt_t event
Definition: MuonPair.h:44
int i
Definition: DBlmapReader.cc:9
lorentzVector mu2
Definition: MuonPair.h:42
UInt_t run
Definition: MuonPair.h:43
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:8
lorentzVector mu1
Definition: GenMuonPair.h:48
lorentzVector mu2
Definition: GenMuonPair.h:49
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:33
void copy(const GenMuonPair &copyPair)
Used to copy the content of another GenMuonPair.
Definition: GenMuonPair.h:41
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 mu1
Definition: MuonPair.h:41