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, MuonPair::event, cmsRelvalreport::exit, mergeVDriftHistosByStation::file, i, LaserTracksInput_cfi::maxEvents, MuonPair::mu1, GenMuonPair::mu1, MuonPair::mu2, GenMuonPair::mu2, MuonPair::run, and diffTreeTool::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  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  }
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
lorentzVector mu1
Definition: GenMuonPair.h:48
lorentzVector mu2
Definition: GenMuonPair.h:49
tuple cout
Definition: gather_cfg.py:121
lorentzVector mu1
Definition: MuonPair.h:41
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 122 of file RootTreeHandler.h.

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

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