Go to the documentation of this file.00001 #include <iostream>
00002
00003 #include <TFile.h>
00004 #include <TTree.h>
00005
00006 #include <MuonAnalysis/MomentumScaleCalibration/interface/MuonPair.h>
00007 #include <MuonAnalysis/MomentumScaleCalibration/interface/GenMuonPair.h>
00008 #include <MuonAnalysis/MomentumScaleCalibration/interface/MuScleFitProvenance.h>
00009 #include <TH1F.h>
00010 #include <stdlib.h>
00011 #include <vector>
00012
00013 typedef std::vector<std::pair<lorentzVector,lorentzVector> > MuonPairVector;
00014
00023 class RootTreeHandler
00024 {
00025 public:
00026
00027
00028 void writeTree( const TString & fileName, const std::vector<MuonPair> * savedPair, const int muonType = 0,
00029 const std::vector<GenMuonPair> * genPair = 0, const bool saveAll = false )
00030 {
00031 lorentzVector emptyLorentzVector(0,0,0,0);
00032 TFile * f1 = new TFile(fileName, "RECREATE");
00033 TTree * tree = new TTree("T", "Muon pairs");
00034 MuonPair * muonPair = new MuonPair;
00035 GenMuonPair * genMuonPair = new GenMuonPair;
00036
00037 tree->Branch("event", "MuonPair", &muonPair);
00038 if( genPair != 0 ) {
00039 tree->Branch("genEvent", "GenMuonPair", &genMuonPair);
00040
00041
00042 if( savedPair->size() != genPair->size() ) {
00043 std::cout << "Error: savedPair size ("
00044 << savedPair->size() <<") and genPair size ("
00045 << genPair->size() <<") are different. This is severe and I will not write the tree." << std::endl;
00046 exit(1);
00047 }
00048 }
00049 std::cout << "savedPair->size() is "<<savedPair->size()<< std::endl;
00050 std::vector<MuonPair>::const_iterator muonPairIt = savedPair->begin();
00051 unsigned int iev = 0;
00052 for( ; muonPairIt != savedPair->end(); ++muonPairIt, ++iev ) {
00053
00054 if( saveAll || ( (muonPairIt->mu1 != emptyLorentzVector) && (muonPairIt->mu2 != emptyLorentzVector) ) ) {
00055
00056
00057 muonPair->copy(*muonPairIt);
00058
00059
00060
00061
00062
00063
00064 if( genPair != 0 ) {
00065 genMuonPair->copy((*genPair)[iev]);
00066 }
00067
00068 tree->Fill();
00069 }
00070
00071
00072 }
00073
00074
00075 TH1F muonTypeHisto("MuonType", "MuonType", 40, -20, 20);
00076 muonTypeHisto.Fill(muonType);
00077 muonTypeHisto.Write();
00078 MuScleFitProvenance provenance(muonType);
00079 provenance.Write();
00080
00081 f1->Write();
00082 f1->Close();
00083 }
00084
00085
00086
00087 void readTree( const int maxEvents, const TString & fileName, MuonPairVector * savedPair,
00088 const int muonType, std::vector<std::pair<int, int> > * evtRun, MuonPairVector * genPair = 0 )
00089 {
00090 TFile * file = TFile::Open(fileName, "READ");
00091 if( file->IsOpen() ) {
00092 TTree * tree = (TTree*)file->Get("T");
00093 MuonPair * muonPair = 0;
00094 GenMuonPair * genMuonPair = 0;
00095
00096 tree->SetBranchAddress("event",&muonPair);
00097 if( genPair != 0 ) {
00098 tree->SetBranchAddress("genEvent",&genMuonPair);
00099 }
00100
00101 Long64_t nentries = tree->GetEntries();
00102 if( (maxEvents != -1) && (nentries > maxEvents) ) nentries = maxEvents;
00103 for( Long64_t i=0; i<nentries; ++i ) {
00104 tree->GetEntry(i);
00105 savedPair->push_back(std::make_pair(muonPair->mu1, muonPair->mu2));
00106 evtRun->push_back(std::make_pair(muonPair->event, muonPair->run));
00107
00108 if( genPair != 0 ) {
00109 genPair->push_back(std::make_pair(genMuonPair->mu1, genMuonPair->mu2));
00110
00111 }
00112 }
00113 }
00114 else {
00115 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;
00116 exit(1);
00117 }
00118 file->Close();
00119 }
00120
00122 void readTree( const int maxEvents, const TString & fileName, std::vector<MuonPair> * savedPair,
00123 const int muonType, std::vector<GenMuonPair> * genPair = 0 )
00124 {
00125 TFile * file = TFile::Open(fileName, "READ");
00126 if( file->IsOpen() ) {
00127 TTree * tree = (TTree*)file->Get("T");
00128 MuonPair * muonPair = 0;
00129 GenMuonPair * genMuonPair = 0;
00130 tree->SetBranchAddress("event",&muonPair);
00131 if( genPair != 0 ) {
00132 tree->SetBranchAddress("genEvent",&genMuonPair);
00133 }
00134
00135 Long64_t nentries = tree->GetEntries();
00136 if( (maxEvents != -1) && (nentries > maxEvents) ) nentries = maxEvents;
00137 for( Long64_t i=0; i<nentries; ++i ) {
00138 tree->GetEntry(i);
00139 savedPair->push_back(*muonPair);
00140 if( genPair != 0 ) {
00141 genPair->push_back(*genMuonPair);
00142 }
00143 }
00144 }
00145 else {
00146 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;
00147 exit(1);
00148 }
00149 file->Close();
00150 }
00151
00152 };