CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h

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