ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/grimes/L1Menu/src/TriggerRatePlot.cpp
Revision: 1.3
Committed: Sun Jun 2 22:40:57 2013 UTC (11 years, 11 months ago) by grimes
Branch: MAIN
Changes since 1.2: +5 -0 lines
Log Message:
A few changes, and added skeleton files for all of the triggers.

File Contents

# User Rev Content
1 grimes 1.1 #include "l1menu/TriggerRatePlot.h"
2    
3     #include "l1menu/ITrigger.h"
4     #include "l1menu/IEvent.h"
5     #include "l1menu/TriggerTable.h"
6 grimes 1.2 #include "l1menu/ReducedMenuSample.h"
7     #include "l1menu/IReducedEvent.h"
8 grimes 1.1 #include <TH1F.h>
9     #include <iostream>
10     #include <sstream>
11    
12     l1menu::TriggerRatePlot::TriggerRatePlot( const l1menu::ITrigger& trigger, std::unique_ptr<TH1> pHistogram, const std::string versusParameter )
13     : pHistogram_( std::move(pHistogram) ), versusParameter_(versusParameter), histogramOwnedByMe_(true)
14     {
15     // Take a copy of the trigger
16     l1menu::TriggerTable& table=l1menu::TriggerTable::instance();
17     pTrigger_=table.copyTrigger( trigger );
18    
19     // Make sure the versusParameter_ supplied is valid. If it's not then this call will
20     // throw an exception.
21 grimes 1.2 pParameter_=&pTrigger_->parameter(versusParameter_);
22 grimes 1.1
23     // I want to make a note of the other parameters set for the trigger. As far as I know TH1
24     // has no way of adding annotations so I'll tag it on the end of the title.
25     std::stringstream description;
26     description << pTrigger_->name() << " rate versus " << versusParameter;
27    
28     // Loop over all of the parameters and add their values to the description
29     std::vector<std::string> parameterNames=pTrigger_->parameterNames();
30     description << " [v" << pTrigger_->version();
31     if( parameterNames.size()>1 ) description << ",";
32     for( std::vector<std::string>::const_iterator iName=parameterNames.begin(); iName!=parameterNames.end(); ++iName )
33     {
34     if( *iName==versusParameter ) continue; // Don't bother adding the parameter I'm plotting against
35     description << *iName << "=" << pTrigger_->parameter(*iName);
36     if( iName+1!=parameterNames.end() ) description << ","; // Add delimeter between parameter names
37     }
38     description << "]";
39    
40     pHistogram_->SetTitle( description.str().c_str() );
41    
42     }
43    
44     l1menu::TriggerRatePlot::TriggerRatePlot( l1menu::TriggerRatePlot&& otherTriggerRatePlot ) noexcept
45     : pTrigger_( std::move(otherTriggerRatePlot.pTrigger_) ), pHistogram_( std::move(otherTriggerRatePlot.pHistogram_) ),
46 grimes 1.2 versusParameter_( std::move(otherTriggerRatePlot.versusParameter_) ), pParameter_(otherTriggerRatePlot.pParameter_),
47     histogramOwnedByMe_(histogramOwnedByMe_)
48 grimes 1.1 {
49     // No operation besides the initaliser list
50     }
51    
52     l1menu::TriggerRatePlot& l1menu::TriggerRatePlot::operator=( l1menu::TriggerRatePlot&& otherTriggerRatePlot ) noexcept
53     {
54     // Free up whatever was there before
55     pTrigger_.reset();
56     if( histogramOwnedByMe_ ) pHistogram_.reset();
57     else pHistogram_.release();
58    
59     // Then copy from the other object
60     pTrigger_=std::move(otherTriggerRatePlot.pTrigger_);
61     pHistogram_=std::move(otherTriggerRatePlot.pHistogram_);
62     versusParameter_=std::move(otherTriggerRatePlot.versusParameter_);
63 grimes 1.2 pParameter_=otherTriggerRatePlot.pParameter_;
64     histogramOwnedByMe_=otherTriggerRatePlot.histogramOwnedByMe_;
65 grimes 1.1
66     return *this;
67     }
68    
69     l1menu::TriggerRatePlot::~TriggerRatePlot()
70     {
71     // If the flag has been set telling me that this instance doesn't own the histogram,
72     // then I need to tell the unique_ptr not to delete it.
73     if( !histogramOwnedByMe_ )
74     {
75     pHistogram_.release();
76     }
77     }
78    
79 grimes 1.2 void l1menu::TriggerRatePlot::addEvent( const l1menu::IEvent& event )
80 grimes 1.1 {
81     // loop over all of the bins in the histogram, and see if the trigger passes
82     // for the value at the center of the bin.
83     for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
84     {
85 grimes 1.2 (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
86 grimes 1.1 if( pTrigger_->apply( event ) )
87     {
88 grimes 1.2 pHistogram_->Fill( (*pParameter_), event.weight() );
89 grimes 1.1 }
90     // could put an "else break" in here, but I don't know if triggers
91     // will run their thresholds the other way. E.g. a trigger could require
92     // a minimum amount of energy in the event, in which case the higher
93     // bins will pass.
94     }
95     }
96    
97 grimes 1.3 const l1menu::ITrigger& l1menu::TriggerRatePlot::getTrigger() const
98     {
99     return *pTrigger_;
100     }
101    
102 grimes 1.2 void l1menu::TriggerRatePlot::initiateForReducedSample( const l1menu::ReducedMenuSample& sample )
103     {
104     pTrigger_->initiateForReducedSample( sample );
105     }
106    
107     void l1menu::TriggerRatePlot::addEvent( const l1menu::IReducedEvent& event )
108     {
109     // Basically exactly the same as for the l1menu::IEvent version. I
110     // should probably template this.
111     for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
112     {
113     (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
114     if( pTrigger_->apply( event ) )
115     {
116     pHistogram_->Fill( (*pParameter_), event.weight() );
117     }
118     }
119    
120     }
121    
122 grimes 1.1 TH1* l1menu::TriggerRatePlot::getPlot()
123     {
124     return pHistogram_.get();
125     }
126    
127     TH1* l1menu::TriggerRatePlot::relinquishOwnershipOfPlot()
128     {
129     // Rather than call release() on the unique_ptr, I'll set a flag so that I know
130     // to release() in the destructor instead. This way it's possible to relinquish
131     // ownership but still perform operations on the histograms.
132     histogramOwnedByMe_=false;
133     return pHistogram_.get();
134     }