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, 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 22 of file RootTreeHandler.h.

Member Function Documentation

void RootTreeHandler::readTree ( const int  maxEvents,
const TString &  fileName,
MuonPairVector savedPair,
const int  muonType,
MuonPairVector genPair = 0 
)
inline

Definition at line 83 of file RootTreeHandler.h.

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

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

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  }
int i
Definition: DBlmapReader.cc:9
lorentzVector mu2
Definition: MuonPair.h:42
lorentzVector mu1
Definition: GenMuonPair.h:48
lorentzVector mu2
Definition: GenMuonPair.h:49
tuple cout
Definition: gather_cfg.py:41
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 117 of file RootTreeHandler.h.

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

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  }
int i
Definition: DBlmapReader.cc:9
tuple cout
Definition: gather_cfg.py:41
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 27 of file RootTreeHandler.h.

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

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

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  }
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:41