ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/grimes/L1Menu/src/TriggerRatePlot.cpp
(Generate patch)

Comparing UserCode/grimes/L1Menu/src/TriggerRatePlot.cpp (file contents):
Revision 1.1 by grimes, Tue May 28 23:14:03 2013 UTC vs.
Revision 1.8 by grimes, Thu Jul 4 13:02:22 2013 UTC

# Line 1 | Line 1
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/ISample.h"
8   #include "l1menu/TriggerTable.h"
9   #include <TH1F.h>
7 #include <iostream>
10   #include <sstream>
11 + #include <algorithm>
12 +
13 + #include <iostream>
14  
15 < l1menu::TriggerRatePlot::TriggerRatePlot( const l1menu::ITrigger& trigger, std::unique_ptr<TH1> pHistogram, const std::string versusParameter )
15 > l1menu::TriggerRatePlot::TriggerRatePlot( const l1menu::ITrigger& trigger, std::unique_ptr<TH1> pHistogram, const std::string& versusParameter, const std::vector<std::string> scaledParameters )
16          : pHistogram_( std::move(pHistogram) ), versusParameter_(versusParameter), histogramOwnedByMe_(true)
17   {
18          // Take a copy of the trigger
# Line 15 | Line 20 | l1menu::TriggerRatePlot::TriggerRatePlot
20          pTrigger_=table.copyTrigger( trigger );
21  
22          // Make sure the versusParameter_ supplied is valid. If it's not then this call will
23 <        // throw an exception.
24 <        pTrigger_->parameter(versusParameter_);
25 <
23 >        // throw an exception. Take a pointer to the parameter so I don't need to keep performing
24 >        // expensive string comparisons.
25 >        pParameter_=&pTrigger_->parameter(versusParameter_);
26 >
27 >        // If any parameters have been requested to be scaled along with the versusParameter, figure
28 >        // out what the scaling should be and take a note of pointers.
29 >        for( const auto& parameterToScale : scaledParameters )
30 >        {
31 >                if( parameterToScale!=versusParameter_ )
32 >                {
33 >                        otherParameterScalings_.push_back( std::make_pair( &pTrigger_->parameter(parameterToScale), pTrigger_->parameter(parameterToScale)/(*pParameter_) ) );
34 >                }
35 >        }
36          // I want to make a note of the other parameters set for the trigger. As far as I know TH1
37          // has no way of adding annotations so I'll tag it on the end of the title.
38          std::stringstream description;
# Line 30 | Line 45 | l1menu::TriggerRatePlot::TriggerRatePlot
45          for( std::vector<std::string>::const_iterator iName=parameterNames.begin(); iName!=parameterNames.end(); ++iName )
46          {
47                  if( *iName==versusParameter ) continue; // Don't bother adding the parameter I'm plotting against
48 <                description << *iName << "=" << pTrigger_->parameter(*iName);
48 >
49 >                // First check to see if this is one of the parameters that are being scaled
50 >                if( std::find(scaledParameters.begin(),scaledParameters.end(),*iName)==scaledParameters.end() )
51 >                {
52 >                        // This parameter isn't being scaled, so write the absoulte value in the description
53 >                        description << *iName << "=" << pTrigger_->parameter(*iName);
54 >                }
55 >                else
56 >                {
57 >                        // This parameter is being scaled, so write what the scaling is in the description
58 >                        description << *iName << "=x*" << pTrigger_->parameter(*iName)/(*pParameter_);
59 >                }
60 >
61                  if( iName+1!=parameterNames.end() ) description << ","; // Add delimeter between parameter names
62          }
63          description << "]";
# Line 41 | Line 68 | l1menu::TriggerRatePlot::TriggerRatePlot
68  
69   l1menu::TriggerRatePlot::TriggerRatePlot( l1menu::TriggerRatePlot&& otherTriggerRatePlot ) noexcept
70          : pTrigger_( std::move(otherTriggerRatePlot.pTrigger_) ), pHistogram_( std::move(otherTriggerRatePlot.pHistogram_) ),
71 <          versusParameter_( std::move(otherTriggerRatePlot.versusParameter_) ), histogramOwnedByMe_(histogramOwnedByMe_)
71 >          versusParameter_( std::move(otherTriggerRatePlot.versusParameter_) ), pParameter_(&pTrigger_->parameter(versusParameter_)),
72 >          otherParameterScalings_( std::move(otherTriggerRatePlot.otherParameterScalings_) ), histogramOwnedByMe_(histogramOwnedByMe_)
73   {
74          // No operation besides the initaliser list
75   }
# Line 57 | Line 85 | l1menu::TriggerRatePlot& l1menu::Trigger
85          pTrigger_=std::move(otherTriggerRatePlot.pTrigger_);
86          pHistogram_=std::move(otherTriggerRatePlot.pHistogram_);
87          versusParameter_=std::move(otherTriggerRatePlot.versusParameter_);
88 <        histogramOwnedByMe_=histogramOwnedByMe_;
88 >        pParameter_=&pTrigger_->parameter(versusParameter_);
89 >        otherParameterScalings_=std::move(otherTriggerRatePlot.otherParameterScalings_);
90 >        histogramOwnedByMe_=otherTriggerRatePlot.histogramOwnedByMe_;
91  
92          return *this;
93   }
# Line 72 | Line 102 | l1menu::TriggerRatePlot::~TriggerRatePlo
102          }
103   }
104  
105 < void l1menu::TriggerRatePlot::addEvent( const l1menu::IEvent& event ) const
105 > void l1menu::TriggerRatePlot::addEvent( const l1menu::IEvent& event )
106   {
107 <        float& threshold1=pTrigger_->parameter(versusParameter_);
107 >        const l1menu::ISample& sample=event.sample();
108 >        float weightPerEvent=sample.eventRate()/sample.sumOfWeights();
109 >
110 >        // For some implementations of ISample, it is significantly faster to
111 >        // create ICachedTriggers and then loop over those.
112 >        std::unique_ptr<l1menu::ICachedTrigger> pCachedTrigger=sample.createCachedTrigger( *pTrigger_ );
113  
79        // loop over all of the bins in the histogram, and see if the trigger passes
80        // for the value at the center of the bin.
114          for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
115          {
116 <                threshold1=pHistogram_->GetBinCenter(binNumber);
117 <                if( pTrigger_->apply( event ) )
116 >                (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
117 >
118 >                // Scale accordingly any other parameters that should be scaled.
119 >                for( const auto& parameterScalingPair : otherParameterScalings_ ) *(parameterScalingPair.first)=parameterScalingPair.second*(*pParameter_);
120 >
121 >                if( pCachedTrigger->apply(event) )
122                  {
123 <                        pHistogram_->Fill( threshold1, event.weight() );
123 >                        pHistogram_->Fill( (*pParameter_), event.weight()*weightPerEvent );
124                  }
125 <                // could put an "else break" in here, but I don't know if triggers
125 >                else break;
126 >                // Not sure if this "else break" is a good idea. I don't know if triggers
127                  // will run their thresholds the other way. E.g. a trigger could require
128                  // a minimum amount of energy in the event, in which case the higher
129                  // bins will pass.
130          }
131   }
132  
133 + void l1menu::TriggerRatePlot::addSample( const l1menu::ISample& sample )
134 + {
135 +        float weightPerEvent=sample.eventRate()/sample.sumOfWeights();
136 +
137 +        // Create a cached trigger, which depending on the concrete type of the ISample
138 +        // may or may not significantly increase the speed at which this next loop happens.
139 +        std::unique_ptr<l1menu::ICachedTrigger> pCachedTrigger=sample.createCachedTrigger( *pTrigger_ );
140 +
141 +        for( size_t eventNumber=0; eventNumber<sample.numberOfEvents(); ++eventNumber )
142 +        {
143 +                const l1menu::IEvent& event=sample.getEvent( eventNumber );
144 +
145 +                for( int binNumber=1; binNumber<pHistogram_->GetNbinsX(); ++binNumber )
146 +                {
147 +                        (*pParameter_)=pHistogram_->GetBinCenter(binNumber);
148 +
149 +                        // Scale accordingly any other parameters that should be scaled.
150 +                        for( const auto& parameterScalingPair : otherParameterScalings_ ) *(parameterScalingPair.first)=parameterScalingPair.second*(*pParameter_);
151 +
152 +                        if( pCachedTrigger->apply(event) ) // If the event passes the trigger
153 +                        {
154 +                                pHistogram_->Fill( (*pParameter_), event.weight()*weightPerEvent );
155 +                        }
156 +                        else break;
157 +                        // Not sure if this "else break" is a good idea. I don't know if triggers
158 +                        // will run their thresholds the other way. E.g. a trigger could require
159 +                        // a minimum amount of energy in the event, in which case the higher
160 +                        // bins will pass.
161 +                } // end of loop over histogram bins
162 +        } // end of loop over events
163 +
164 + }
165 +
166 + const l1menu::ITrigger& l1menu::TriggerRatePlot::getTrigger() const
167 + {
168 +        return *pTrigger_;
169 + }
170 +
171   TH1* l1menu::TriggerRatePlot::getPlot()
172   {
173          return pHistogram_.get();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines