ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/grimes/VHbbAnalysisCode/src/LightweightAnalyser.cpp
Revision: 1.1
Committed: Tue Feb 14 01:43:15 2012 UTC (13 years, 2 months ago) by grimes
Branch: MAIN
Log Message:
Only added the directory structure in the last commit, this is now the files.

File Contents

# User Rev Content
1 grimes 1.1 #include "TrkUpgradeAnalysis/VHbb/interface/LightweightAnalyser.h"
2    
3     #include <sstream>
4    
5     #include "TrkUpgradeAnalysis/VHbb/interface/VHbbCandidateCutSets.h"
6    
7     #include "VHbbAnalysis/VHbbDataFormats/interface/VHbbEventAuxInfo.h"
8     #include "VHbbAnalysis/VHbbDataFormats/src/HbbCandidateFinderAlgo.cc" // Not entirely sure why this is included and not linked to
9     #include "DataFormats/FWLite/interface/Event.h"
10     #include "DataFormats/FWLite/interface/Handle.h"
11     #include "DataFormats/PatCandidates/interface/Muon.h"
12    
13    
14     #include <TFile.h>
15     #include <TH1.h>
16     #include <TH2.h>
17    
18    
19     const bool trkupgradeanalysis::LightweightAnalyser::verbose_=false;
20     const double trkupgradeanalysis::LightweightAnalyser::jetPtThresholdZ_=20;
21     const double trkupgradeanalysis::LightweightAnalyser::jetPtThresholdW_=20;
22     const bool trkupgradeanalysis::LightweightAnalyser::useHighestPtHiggsZ_=false;
23     const bool trkupgradeanalysis::LightweightAnalyser::useHighestPtHiggsW_=true;
24    
25     namespace // Use the unnamed namespace
26     {
27     TDirectory* createDirectory( const std::string& fullPath, TDirectory* pParent )
28     {
29     if( pParent==NULL ) throw std::runtime_error( "The parent directory is a Null pointer" );
30    
31     TDirectory* pSubDirectory=pParent;
32     size_t currentPosition=0;
33     size_t nextSlash;
34     do
35     {
36     nextSlash=fullPath.find_first_of('/', currentPosition );
37     std::string directoryName=fullPath.substr(currentPosition,nextSlash-currentPosition);
38     currentPosition=nextSlash+1;
39    
40     TDirectory* pNextSubDirectory=pSubDirectory->GetDirectory( directoryName.c_str() );
41     if( pNextSubDirectory==NULL ) pNextSubDirectory=pSubDirectory->mkdir( directoryName.c_str() );
42     if( pNextSubDirectory==NULL ) throw std::runtime_error( "Couldn't create the root directory \""+directoryName+"\"" );
43     pSubDirectory=pNextSubDirectory;
44    
45     } while( nextSlash!=std::string::npos );
46    
47     return pSubDirectory;
48     }
49     }
50    
51     trkupgradeanalysis::LightweightAnalyser::LightweightAnalyser( const std::string& outputFilename )
52     : zFinder_( verbose_, jetPtThresholdZ_, useHighestPtHiggsZ_ ),
53     wFinder_( verbose_, jetPtThresholdW_, useHighestPtHiggsW_ ),
54     numberOfEvents_(0),
55     numberOfEventsWithAtLeastOneZOrWCandidate_(0),
56     pPlotsForEverything_(NULL)
57     {
58     // All of these are empty smart pointers, so I need to call reset on them
59     pOutputFile_.reset( new TFile( outputFilename.c_str(), "RECREATE" ) );
60    
61     cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::SignalSelectionZee(120)) );
62     cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::SignalSelectionZmumu(120)) );
63     cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::SignalSelectionWen(120)) );
64     cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::SignalSelectionWmun(120)) );
65     cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::SignalSelectionZmumuWithoutAdditionalJetsCut(120)) );
66     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VlightRegionHWmun) );
67     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VlightRegionHWen) );
68     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VlightRegionHZmumu) );
69     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VlightRegionHZee) );
70     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::TTbarRegionHWmun) );
71     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::TTbarRegionHWen) );
72     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::TTbarRegionHZmumu) );
73     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::TTbarRegionHZee) );
74     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VbbRegionHWmun) );
75     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VbbRegionHWen) );
76     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VbbRegionHZmumu) );
77     // cutsToApply_.push_back( boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet>(new trkupgradeanalysis::VbbRegionHZee) );
78     }
79    
80     trkupgradeanalysis::LightweightAnalyser::~LightweightAnalyser()
81     {
82     save();
83     pOutputFile_->Close();
84     }
85    
86     void trkupgradeanalysis::LightweightAnalyser::changeDirectory( const std::string& directory )
87     {
88     std::map<std::string,PlotsSplitByMCType>::iterator iPlots=plotsForEventTypes_.find(directory);
89    
90     if( iPlots==plotsForEventTypes_.end() )
91     {
92     pPlotsForEverything_=&plotsForEventTypes_[directory]; // This will create a new entry in the map
93    
94     // Only book the histograms for all MC event types now. I don't know what MC event types there
95     // until I look at the events so I need to wait until then
96     bookHistograms( pPlotsForEverything_->allEventTypes_, directory+"/allEvents" );
97     }
98     else pPlotsForEverything_=&iPlots->second; // This directory has already been created and the histograms booked, so reuse those
99    
100     currentDirectory_=directory;
101     }
102    
103     void trkupgradeanalysis::LightweightAnalyser::save()
104     {
105     pOutputFile_->Write(0,TObject::kOverwrite);
106     }
107    
108     void trkupgradeanalysis::LightweightAnalyser::bookHistograms( trkupgradeanalysis::LightweightAnalyser::AllPlots& plots, const std::string& directoryName )
109     {
110     TDirectory* pRootDirectory=pOutputFile_->GetDirectory("/");
111     TDirectory* pDirectory=createDirectory( directoryName, pRootDirectory );
112     // TDirectory* pDirectory=pRootDirectory->GetDirectory( directoryName.c_str() );
113     // if( pDirectory==NULL ) pDirectory=pRootDirectory->mkdir( directoryName.c_str() );
114    
115     TDirectory* pSubDirectory=pDirectory->mkdir( "VHbbCandidates" );
116    
117     plots.pNumberOfCandidates_=new TH2F( "numberOfCandidates","Number of candidates",5,-0.5,4.5,5,-0.5,4.5);
118     plots.pNumberOfCandidates_->SetDirectory(pSubDirectory);
119     plots.candidateHistogramsForAllEvents_.book( pSubDirectory );
120     plots.pNumberOfCandidates_->GetXaxis()->SetTitle( "Z candidates" );
121     plots.pNumberOfCandidates_->GetYaxis()->SetTitle( "W candidates" );
122    
123     pSubDirectory=pDirectory->mkdir( "MuonInfos" );
124     plots.allMuons_.book( pSubDirectory );
125    
126     pSubDirectory=pDirectory->mkdir( "ElectronInfos" );
127     plots.allElectrons_.book( pSubDirectory );
128    
129     pSubDirectory=pDirectory->mkdir( "MonteCarloTruth" );
130     plots.monteCarloInfo_.book( pSubDirectory );
131    
132     pSubDirectory=pDirectory->mkdir( "Cuts" );
133    
134     for( std::vector< boost::shared_ptr<trkupgradeanalysis::VHbbCandidateCutSet> >::const_iterator iCutSet=cutsToApply_.begin();
135     iCutSet!=cutsToApply_.end(); ++iCutSet )
136     {
137     TDirectory* pSubSubDirectory=pSubDirectory->mkdir( (*iCutSet)->name().c_str() );
138    
139     CutSetPlotSet newPlotSet( *iCutSet );
140     newPlotSet.book( pSubSubDirectory );
141     plots.cutCollectionPlotSets_.push_back( newPlotSet );
142     }
143     }
144    
145     void trkupgradeanalysis::LightweightAnalyser::fillAllPlotsStructure( AllPlots* pPlotsToFill, const VHbbEvent& vhbbEvent, const std::vector<VHbbCandidate>& zCandidates, const std::vector<VHbbCandidate>& wCandidates )
146     {
147     pPlotsToFill->allMuons_.fill( vhbbEvent.muInfo );
148     pPlotsToFill->allElectrons_.fill( vhbbEvent.eleInfo );
149    
150     for( std::vector<VHbbCandidate>::const_iterator iCandidate=zCandidates.begin(); iCandidate!=zCandidates.end(); ++iCandidate )
151     {
152     pPlotsToFill->candidateHistogramsForAllEvents_.fill( *iCandidate );
153    
154     // Loop over all of the cuts. If this candidate passes the cuts then fill the plots
155     for( std::vector<CutSetPlotSet>::iterator iCutPlotSet=pPlotsToFill->cutCollectionPlotSets_.begin();
156     iCutPlotSet!=pPlotsToFill->cutCollectionPlotSets_.end(); ++iCutPlotSet )
157     {
158     iCutPlotSet->fill( *iCandidate );
159     }
160     }
161    
162     pPlotsToFill->pNumberOfCandidates_->Fill( zCandidates.size(), wCandidates.size() );
163     }
164    
165     void trkupgradeanalysis::LightweightAnalyser::processEvent( const fwlite::Event& event )
166     {
167     std::vector<VHbbCandidate> zCandidates;
168     std::vector<VHbbCandidate> wCandidates;
169    
170     fwlite::Handle<VHbbEvent> vhbbHandle;
171     vhbbHandle.getByLabel( event, "HbbAnalyzerNew" );
172     const VHbbEvent* pVHbbEvent=vhbbHandle.product();
173     if( pVHbbEvent == 0 )
174     {
175     std::cerr << "Couldn't get the VHbbEvent" << std::endl;
176     return;
177     }
178    
179     AllPlots* pCurrentPlotsForThisEventType=NULL;
180     fwlite::Handle<VHbbEventAuxInfo> vhbbAuxInfoHandle;
181     vhbbAuxInfoHandle.getByLabel( event, "HbbAnalyzerNew" );
182     const VHbbEventAuxInfo* pVHbbEventAuxInfo=vhbbAuxInfoHandle.product();
183     if( pVHbbEventAuxInfo == 0 )
184     {
185     std::cerr << "Couldn't get the VHbbEventAuxInfo" << std::endl;
186     return;
187     }
188     else
189     {
190     pPlotsForEverything_->allEventTypes_.monteCarloInfo_.fill( *pVHbbEventAuxInfo );
191     std::string eventType=eventTypeFromMC( *pVHbbEventAuxInfo );
192    
193     std::map<std::string,AllPlots>::iterator iPlotsForThisEventType=pPlotsForEverything_->mcEventTypePlots_.find(eventType);
194    
195     if( iPlotsForThisEventType==pPlotsForEverything_->mcEventTypePlots_.end() )
196     {
197     // This event type hasn't been encountered before so I need to book the histograms
198     AllPlots& plotsForThisEventType=pPlotsForEverything_->mcEventTypePlots_[eventType];
199     bookHistograms( plotsForThisEventType, currentDirectory_+"/"+eventType );
200     pCurrentPlotsForThisEventType=&plotsForThisEventType;
201     }
202     else pCurrentPlotsForThisEventType=&(iPlotsForThisEventType->second);
203     }
204    
205     ++numberOfEvents_;
206    
207     zFinder_.run( pVHbbEvent, zCandidates );
208     wFinder_.run( pVHbbEvent, wCandidates );
209    
210     // fwlite::Handle<edm::View<pat::Muon> > muonHandle;
211     // muonHandle.getByLabel( event, "muons" );
212     // const edm::View<pat::Muon>& muons=*muonHandle;
213     // std::cout << "muons.size()=" << muons.size() << std::endl;
214    
215     fillAllPlotsStructure( &pPlotsForEverything_->allEventTypes_, *pVHbbEvent, zCandidates, wCandidates );
216     if( pCurrentPlotsForThisEventType!=NULL ) fillAllPlotsStructure( pCurrentPlotsForThisEventType, *pVHbbEvent, zCandidates, wCandidates );
217    
218     // // This loops over all of the muons in the event and plots some things about them
219     // pCurrentPlots_->allMuons_.fill( pVHbbEvent->muInfo );
220     // pCurrentPlots_->allElectrons_.fill( pVHbbEvent->eleInfo );
221     //
222     // for( std::vector<VHbbCandidate>::const_iterator iCandidate=zCandidates.begin(); iCandidate!=zCandidates.end(); ++iCandidate )
223     // {
224     // pCurrentPlots_->candidateHistogramsForAllEvents_.fill( *iCandidate );
225     //
226     // // Loop over all of the cuts. If this candidate passes the cuts then fill the plots
227     // for( std::vector<CutSetPlotSet>::iterator iCutPlotSet=pCurrentPlots_->cutCollectionPlotSets_.begin();
228     // iCutPlotSet!=pCurrentPlots_->cutCollectionPlotSets_.end(); ++iCutPlotSet )
229     // {
230     // iCutPlotSet->fill( *iCandidate );
231     // }
232     // }
233    
234     if( !zCandidates.empty() || !wCandidates.empty() ) ++numberOfEventsWithAtLeastOneZOrWCandidate_;
235    
236     // pCurrentPlots_->pNumberOfCandidates_->Fill( zCandidates.size(), wCandidates.size() );
237     }
238    
239     void trkupgradeanalysis::LightweightAnalyser::processFile( TFile* pInputFile )
240     {
241     if( pPlotsForEverything_==NULL ) changeDirectory("allEvents");
242    
243     fwlite::Event event( pInputFile );
244     for( event.toBegin(); !event.atEnd(); ++event )
245     {
246     processEvent( event );
247     }
248    
249     std::cout << "There were " << numberOfEventsWithAtLeastOneZOrWCandidate_ << " events with a Z or W candidate out of " << numberOfEvents_ << " events." << std::endl;
250     }
251    
252     std::string trkupgradeanalysis::LightweightAnalyser::eventTypeFromMC( const VHbbEventAuxInfo& eventAuxInfo )
253     {
254     std::stringstream eventStringStream;
255    
256     std::vector<VHbbEventAuxInfo::ParticleMCInfo> zMCInfo=eventAuxInfo.mcZ;
257     std::vector<VHbbEventAuxInfo::ParticleMCInfo> higgsMCInfo=eventAuxInfo.mcH;
258    
259     for( std::vector<VHbbEventAuxInfo::ParticleMCInfo>::const_iterator higgsInfo=higgsMCInfo.begin(); higgsInfo!=higgsMCInfo.end(); ++higgsInfo )
260     {
261     if( higgsInfo->dauid.size()==1 && std::abs(higgsInfo->dauid[0])==25 ) continue;
262    
263     eventStringStream << "H->(";
264     for( std::vector<int>::const_iterator daughterID=higgsInfo->dauid.begin(); daughterID!=higgsInfo->dauid.end(); ++daughterID )
265     {
266     if( daughterID!=higgsInfo->dauid.begin() ) eventStringStream << ",";
267     eventStringStream << *daughterID;
268     }
269     eventStringStream << ") ";
270     }
271    
272     for( std::vector<VHbbEventAuxInfo::ParticleMCInfo>::const_iterator zInfo=zMCInfo.begin(); zInfo!=zMCInfo.end(); ++zInfo )
273     {
274     if( zInfo->dauid.empty() ) continue;
275     if( zInfo->dauid.size()==1 && std::abs(zInfo->dauid[0])==23 ) continue;
276     if( zInfo->dauid.size()==2 && std::abs(zInfo->dauid[0])==23 && std::abs(zInfo->dauid[1])==25 ) continue;
277    
278     eventStringStream << "Z->(";
279     for( std::vector<int>::const_iterator daughterID=zInfo->dauid.begin(); daughterID!=zInfo->dauid.end(); ++daughterID )
280     {
281     if( *daughterID==23 ) continue;
282     if( zInfo->dauid.size()>2 && *daughterID==22 ) continue;
283     if( daughterID!=zInfo->dauid.begin() ) eventStringStream << ",";
284     eventStringStream << *daughterID;
285     }
286     eventStringStream << ") ";
287     }
288    
289     return eventStringStream.str();
290     }
291