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