ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/grimes/L1Menu/src/TriggerRatePlot.cpp
Revision: 1.6
Committed: Sat Jun 29 16:14:45 2013 UTC (11 years, 10 months ago) by grimes
Branch: MAIN
Changes since 1.5: +45 -6 lines
Log Message:
Added an option to the TriggerRatePlot to scale all thresholds at once for the plot

File Contents

# Content
1 #include "l1menu/TriggerRatePlot.h"
2
3 #include "l1menu/ITrigger.h"
4 #include "l1menu/ICachedTrigger.h"
5 #include "l1menu/L1TriggerDPGEvent.h"
6 #include "l1menu/IEvent.h"
7 #include "l1menu/TriggerTable.h"
8 #include "l1menu/ReducedMenuSample.h"
9 #include "l1menu/ReducedEvent.h"
10 #include <TH1F.h>
11 #include <sstream>
12 #include <algorithm>
13
14 #include <iostream>
15
16 l1menu::TriggerRatePlot::TriggerRatePlot( const l1menu::ITrigger& trigger, std::unique_ptr<TH1> pHistogram, const std::string& versusParameter, const std::vector<std::string> scaledParameters )
17 : pHistogram_( std::move(pHistogram) ), versusParameter_(versusParameter), histogramOwnedByMe_(true)
18 {
19 // Take a copy of the trigger
20 l1menu::TriggerTable& table=l1menu::TriggerTable::instance();
21 pTrigger_=table.copyTrigger( trigger );
22
23 // Make sure the versusParameter_ supplied is valid. If it's not then this call will
24 // throw an exception. Take a pointer to the parameter so I don't need to keep performing
25 // expensive string comparisons.
26 pParameter_=&pTrigger_->parameter(versusParameter_);
27
28 // If any parameters have been requested to be scaled along with the versusParameter, figure
29 // out what the scaling should be and take a note of pointers.
30 for( const auto& parameterToScale : scaledParameters )
31 {
32 if( parameterToScale!=versusParameter_ )
33 {
34 otherParameterScalings_.push_back( std::make_pair( &pTrigger_->parameter(parameterToScale), pTrigger_->parameter(parameterToScale)/(*pParameter_) ) );
35 }
36 }
37 // I want to make a note of the other parameters set for the trigger. As far as I know TH1
38 // has no way of adding annotations so I'll tag it on the end of the title.
39 std::stringstream description;
40 description << pTrigger_->name() << " rate versus " << versusParameter;
41
42 // Loop over all of the parameters and add their values to the description
43 std::vector<std::string> parameterNames=pTrigger_->parameterNames();
44 description << " [v" << pTrigger_->version();
45 if( parameterNames.size()>1 ) description << ",";
46 for( std::vector<std::string>::const_iterator iName=parameterNames.begin(); iName!=parameterNames.end(); ++iName )
47 {
48 if( *iName==versusParameter ) continue; // Don't bother adding the parameter I'm plotting against
49
50 // First check to see if this is one of the parameters that are being scaled
51 if( std::find(scaledParameters.begin(),scaledParameters.end(),*iName)==scaledParameters.end() )
52 {
53 // This parameter isn't being scaled, so write the absoulte value in the description
54 description << *iName << "=" << pTrigger_->parameter(*iName);
55 }
56 else
57 {
58 // This parameter is being scaled, so write what the scaling is in the description
59 description << *iName << "=x*" << pTrigger_->parameter(*iName)/(*pParameter_);
60 }
61
62 if( iName+1!=parameterNames.end() ) description << ","; // Add delimeter between parameter names
63 }
64 description << "]";
65
66 pHistogram_->SetTitle( description.str().c_str() );
67
68 }
69
70 l1menu::TriggerRatePlot::TriggerRatePlot( l1menu::TriggerRatePlot&& otherTriggerRatePlot ) noexcept
71 : pTrigger_( std::move(otherTriggerRatePlot.pTrigger_) ), pHistogram_( std::move(otherTriggerRatePlot.pHistogram_) ),
72 versusParameter_( std::move(otherTriggerRatePlot.versusParameter_) ), pParameter_(&pTrigger_->parameter(versusParameter_)),
73 otherParameterScalings_( std::move(otherTriggerRatePlot.otherParameterScalings_) ), histogramOwnedByMe_(histogramOwnedByMe_)
74 {
75 // No operation besides the initaliser list
76 }
77
78 l1menu::TriggerRatePlot& l1menu::TriggerRatePlot::operator=( l1menu::TriggerRatePlot&& otherTriggerRatePlot ) noexcept
79 {
80 // Free up whatever was there before
81 pTrigger_.reset();
82 if( histogramOwnedByMe_ ) pHistogram_.reset();
83 else pHistogram_.release();
84
85 // Then copy from the other object
86 pTrigger_=std::move(otherTriggerRatePlot.pTrigger_);
87 pHistogram_=std::move(otherTriggerRatePlot.pHistogram_);
88 versusParameter_=std::move(otherTriggerRatePlot.versusParameter_);
89 pParameter_=&pTrigger_->parameter(versusParameter_);
90 otherParameterScalings_=std::move(otherTriggerRatePlot.otherParameterScalings_);
91 histogramOwnedByMe_=otherTriggerRatePlot.histogramOwnedByMe_;
92
93 return *this;
94 }
95
96 l1menu::TriggerRatePlot::~TriggerRatePlot()
97 {
98 // If the flag has been set telling me that this instance doesn't own the histogram,
99 // then I need to tell the unique_ptr not to delete it.
100 if( !histogramOwnedByMe_ )
101 {
102 pHistogram_.release();
103 }
104 }
105
106 void l1menu::TriggerRatePlot::addEvent( const l1menu::L1TriggerDPGEvent& event )
107 {
108 // loop over all of the bins in the histogram, and see if the trigger passes
109 // for the value at the center of the bin.
110 for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
111 {
112 (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
113
114 // Scale accordingly any other parameters that should be scaled.
115 for( const auto& parameterScalingPair : otherParameterScalings_ ) *(parameterScalingPair.first)=parameterScalingPair.second*(*pParameter_);
116
117 if( pTrigger_->apply( event ) )
118 {
119 pHistogram_->Fill( (*pParameter_), event.weight() );
120 }
121 // could put an "else break" in here, but I don't know if triggers
122 // will run their thresholds the other way. E.g. a trigger could require
123 // a minimum amount of energy in the event, in which case the higher
124 // bins will pass.
125 }
126 }
127
128 void l1menu::TriggerRatePlot::addSample( const l1menu::ISample& sample )
129 {
130 float weightPerEvent=sample.eventRate()/sample.sumOfWeights();
131
132 // Create a cached trigger, which depending on the concrete type of the ISample
133 // may or may not significantly increase the speed at which this next loop happens.
134 std::unique_ptr<l1menu::ICachedTrigger> pCachedTrigger=sample.createCachedTrigger( *pTrigger_ );
135
136 for( size_t eventNumber=0; eventNumber<sample.numberOfEvents(); ++eventNumber )
137 {
138 const l1menu::IEvent& event=sample.getEvent( eventNumber );
139
140 for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
141 {
142 (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
143
144 // Scale accordingly any other parameters that should be scaled.
145 for( const auto& parameterScalingPair : otherParameterScalings_ ) *(parameterScalingPair.first)=parameterScalingPair.second*(*pParameter_);
146
147 if( pCachedTrigger->apply(event) ) // If the event passes the trigger
148 {
149 pHistogram_->Fill( (*pParameter_), event.weight()*weightPerEvent );
150 }
151 // could put an "else break" in here, but I don't know if triggers will
152 // run their thresholds the other way. E.g. a trigger could require
153 // a minimum amount of energy in the event, in which case the higher
154 // bins will pass.
155 } // end of loop over histogram bins
156 } // end of loop over events
157
158 }
159
160 const l1menu::ITrigger& l1menu::TriggerRatePlot::getTrigger() const
161 {
162 return *pTrigger_;
163 }
164
165 void l1menu::TriggerRatePlot::initiateForReducedSample( const l1menu::ReducedMenuSample& sample )
166 {
167 pTrigger_->initiateForReducedSample( sample );
168 }
169
170 void l1menu::TriggerRatePlot::addEvent( const l1menu::ReducedEvent& event )
171 {
172 // Basically exactly the same as for the l1menu::L1TriggerDPGEvent version. I
173 // should probably template this.
174 for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
175 {
176 (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
177
178 // Scale accordingly any other parameters that should be scaled.
179 for( const auto& parameterScalingPair : otherParameterScalings_ ) *(parameterScalingPair.first)=parameterScalingPair.second*(*pParameter_);
180
181 if( pTrigger_->apply( event ) )
182 {
183 pHistogram_->Fill( (*pParameter_), event.weight() );
184 }
185 }
186
187 }
188
189 TH1* l1menu::TriggerRatePlot::getPlot()
190 {
191 return pHistogram_.get();
192 }
193
194 TH1* l1menu::TriggerRatePlot::relinquishOwnershipOfPlot()
195 {
196 // Rather than call release() on the unique_ptr, I'll set a flag so that I know
197 // to release() in the destructor instead. This way it's possible to relinquish
198 // ownership but still perform operations on the histograms.
199 histogramOwnedByMe_=false;
200 return pHistogram_.get();
201 }