CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions
RootTreeHandler Class Reference

#include <RootTreeHandler.h>

Public Member Functions

void readTree (const int maxEvents, const TString &fileName, MuonPairVector *savedPair, const int muonType, std::vector< std::pair< int, int > > *evtRun, MuonPairVector *genPair=0)
 
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. More...
 
void writeTree (const TString &fileName, const std::vector< MuonPair > *savedPair, const int muonType=0, const std::vector< GenMuonPair > *genPair=0, const bool saveAll=false)
 

Detailed Description

This class can be used to save all the muon pairs (and gen muon pairs if any) to a root tree.
The writeTree method gets the name of the file to store the tree and the savedPair (and possibly genPair) vector of muon pairs.
Likewise, the readTree method takes the same arguments. It reads back from the file with the given name the pairs and stores them in the given savedPair (and genPair) vector.

Definition at line 23 of file RootTreeHandler.h.

Member Function Documentation

void RootTreeHandler::readTree ( const int  maxEvents,
const TString &  fileName,
MuonPairVector savedPair,
const int  muonType,
std::vector< std::pair< int, int > > *  evtRun,
MuonPairVector genPair = 0 
)
inline

Definition at line 87 of file RootTreeHandler.h.

References gather_cfg::cout, MuScleFitEvent::event(), MuonPair::event, cmsRelvalreport::exit, mergeVDriftHistosByStation::file, i, LaserTracksInput_cfi::maxEvents, MuonPair::mu1, GenMuonPair::mu1, MuonPair::mu2, GenMuonPair::mu2, MuScleFitMuon::p4(), MuScleFitEvent::run(), and MainPageGenerator::tree.

Referenced by TreeSplitter::endJob(), ErrorsAnalyzer::fillHistograms(), ErrorsPropagationAnalyzer::fillHistograms(), main(), and MuScleFit::selectMuons().

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  }
int i
Definition: DBlmapReader.cc:9
UInt_t event() const
Definition: Event.h:27
MuScleFitMuon mu1
Definition: GenMuonPair.h:54
MuScleFitEvent event
Definition: MuonPair.h:40
MuScleFitMuon mu1
Definition: MuonPair.h:38
MuScleFitMuon mu2
Definition: MuonPair.h:39
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
void RootTreeHandler::readTree ( const int  maxEvents,
const TString &  fileName,
std::vector< MuonPair > *  savedPair,
const int  muonType,
std::vector< GenMuonPair > *  genPair = 0 
)
inline

Used to read the external trees.

Definition at line 124 of file RootTreeHandler.h.

References gather_cfg::cout, cmsRelvalreport::exit, mergeVDriftHistosByStation::file, i, LaserTracksInput_cfi::maxEvents, and MainPageGenerator::tree.

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  }
int i
Definition: DBlmapReader.cc:9
tuple cout
Definition: gather_cfg.py:121
void RootTreeHandler::writeTree ( const TString &  fileName,
const std::vector< MuonPair > *  savedPair,
const int  muonType = 0,
const std::vector< GenMuonPair > *  genPair = 0,
const bool  saveAll = false 
)
inline

Definition at line 28 of file RootTreeHandler.h.

References MuonPair::copy(), GenMuonPair::copy(), gather_cfg::cout, cmsRelvalreport::exit, python.connectstrParser::f1, and MainPageGenerator::tree.

Referenced by TreeSplitter::endJob(), main(), and MuScleFit::~MuScleFit().

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  }
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
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
tuple cout
Definition: gather_cfg.py:121