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

Comparing UserCode/grimes/L1Menu/src/ReducedMenuSample.cpp (file contents):
Revision 1.9 by grimes, Sat Jun 8 09:31:25 2013 UTC vs.
Revision 1.11 by grimes, Mon Jun 24 14:47:38 2013 UTC

# Line 9 | Line 9
9   #include "l1menu/MenuSample.h"
10   #include "l1menu/TriggerMenu.h"
11   #include "l1menu/ITrigger.h"
12 < #include "l1menu/tools.h"
12 > #include "l1menu/IMenuRate.h"
13 > #include "l1menu/ITriggerRate.h"
14 > #include "l1menu/tools/tools.h"
15   #include "protobuf/l1menu.pb.h"
16   #include <google/protobuf/io/zero_copy_stream_impl.h>
17   #include <google/protobuf/io/gzip_stream.h>
# Line 38 | Line 40 | namespace // unnamed namespace
40          private:
41                  int fileDescriptor_;
42          };
43 +
44 +        class MenuRateImplementation;
45 +
46 +        class TriggerRateImplementation : public l1menu::ITriggerRate
47 +        {
48 +        public:
49 +                TriggerRateImplementation( const l1menu::ITrigger& trigger, float fraction, const MenuRateImplementation& menuRate );
50 +                TriggerRateImplementation& operator=( TriggerRateImplementation&& otherTriggerRate ); // Move assignment
51 +                virtual ~TriggerRateImplementation();
52 +
53 +                // Methods required by the l1menu::ITriggerRate interface
54 +                virtual const l1menu::ITrigger& trigger() const;
55 +                virtual float fraction() const;
56 +                virtual float rate() const;
57 +        protected:
58 +                std::unique_ptr<l1menu::ITrigger> pTrigger_;
59 +                float fraction_;
60 +                const MenuRateImplementation& menuRate_;
61 +        };
62 +
63 +        class MenuRateImplementation : public l1menu::IMenuRate
64 +        {
65 +        public:
66 +                void setTotalFraction( float fraction ) { totalFraction_=fraction; }
67 +                void setScaling( float scaling ) { scaling_=scaling; }
68 +                float scaling() const { return scaling_; }
69 +                void addTriggerRate( const l1menu::ITrigger& trigger, float fraction ) { triggerRates_.push_back( std::move(TriggerRateImplementation(trigger,fraction,*this)) ); }
70 +                // Methods required by the l1menu::IMenuRate interface
71 +                virtual float totalFraction() const { return totalFraction_; }
72 +                virtual float totalRate() const { return totalFraction_*scaling_; }
73 +                virtual const std::vector<const l1menu::ITriggerRate*>& triggerRates() const
74 +                {
75 +                        // If the sizes are the same I'll assume nothing has changed and the references
76 +                        // are still valid. I don't expect this method to be called until the triggerRates_
77 +                        // vector is complete anyway.
78 +                        if( triggerRates_.size()!=baseClassReferences_.size() )
79 +                        {
80 +                                baseClassReferences_.clear();
81 +                                for( const auto& triggerRate : triggerRates_ )
82 +                                {
83 +                                        baseClassReferences_.push_back( &triggerRate );
84 +                                }
85 +                        }
86 +
87 +                        return baseClassReferences_;
88 +                }
89 +
90 +        protected:
91 +                float totalFraction_;
92 +                float scaling_;
93 +                std::vector<TriggerRateImplementation> triggerRates_;
94 +                mutable std::vector<const l1menu::ITriggerRate*> baseClassReferences_;
95 +        };
96 +
97 +        TriggerRateImplementation::TriggerRateImplementation( const l1menu::ITrigger& trigger, float fraction, const MenuRateImplementation& menuRate )
98 +                : fraction_(fraction), menuRate_(menuRate)
99 +        {
100 +                pTrigger_=std::move( l1menu::TriggerTable::instance().copyTrigger(trigger) );
101 +        }
102 +        TriggerRateImplementation& TriggerRateImplementation::operator=( TriggerRateImplementation&& otherTriggerRate )
103 +        {
104 +                pTrigger_=std::move( otherTriggerRate.pTrigger_ );
105 +                fraction_=otherTriggerRate.fraction_;
106 +                // I can't change the menuRate_ reference, but that should already be set to the right one anyway.
107 +                return *this;
108 +        }
109 +        TriggerRateImplementation::~TriggerRateImplementation() {}
110 +        const l1menu::ITrigger& TriggerRateImplementation::trigger() const { return *pTrigger_; }
111 +        float TriggerRateImplementation::fraction() const { return fraction_; }
112 +        float TriggerRateImplementation::rate() const { return fraction_*menuRate_.scaling(); }
113 +
114   }
115  
116   namespace l1menu
# Line 56 | Line 129 | namespace l1menu
129                  ReducedMenuSamplePrivateMembers( const std::string& filename );
130                  void copyMenuToProtobufSample();
131                  ::ReducedEventImplementation event;
132 <                const l1menu::TriggerMenu& triggerMenu;
132 >                const l1menu::TriggerMenu& triggerMenu; // External const access to mutableTriggerMenu_
133 >                float eventRate;
134                  l1menuprotobuf::SampleHeader protobufSampleHeader;
135                  // Protobuf doesn't implement move semantics so I'll use pointers
136                  std::vector<std::unique_ptr<l1menuprotobuf::Run> > protobufRuns;
# Line 71 | Line 145 | namespace l1menu
145   }
146  
147   l1menu::ReducedMenuSamplePrivateMembers::ReducedMenuSamplePrivateMembers( const l1menu::TriggerMenu& newTriggerMenu )
148 <        : mutableTriggerMenu_( newTriggerMenu ), triggerMenu( mutableTriggerMenu_ )
148 >        : mutableTriggerMenu_( newTriggerMenu ), triggerMenu( mutableTriggerMenu_ ), eventRate(1)
149   {
150          GOOGLE_PROTOBUF_VERIFY_VERSION;
151  
# Line 99 | Line 173 | l1menu::ReducedMenuSamplePrivateMembers:
173  
174                  // Make a note of the names of the parameters that are recorded for each event. For this
175                  // I'm just recording the parameters that refer to the thresholds.
176 <                const auto thresholdNames=l1menu::getThresholdNames(trigger);
176 >                const auto thresholdNames=l1menu::tools::getThresholdNames(trigger);
177                  for( const auto& thresholdName : thresholdNames ) pProtobufTrigger->add_varying_parameter(thresholdName);
178  
179          } // end of loop over triggers
# Line 245 | Line 319 | void l1menu::ReducedMenuSample::addSampl
319                  for( size_t triggerNumber=0; triggerNumber<pImple_->triggerMenu.numberOfTriggers(); ++triggerNumber )
320                  {
321                          std::unique_ptr<l1menu::ITrigger> pTrigger=pImple_->triggerMenu.getTriggerCopy(triggerNumber);
322 <                        std::vector<std::string> thresholdNames=getThresholdNames(*pTrigger);
322 >                        std::vector<std::string> thresholdNames=l1menu::tools::getThresholdNames(*pTrigger);
323  
324                          try
325                          {
326 <                                setTriggerThresholdsAsTightAsPossible( event, *pTrigger, 0.001 );
326 >                                l1menu::tools::setTriggerThresholdsAsTightAsPossible( event, *pTrigger, 0.001 );
327                                  // Set all of the parameters to match the thresholds in the trigger
328                                  for( const auto& thresholdName : thresholdNames )
329                                  {
# Line 362 | Line 436 | bool l1menu::ReducedMenuSample::contains
436                  // eta cuts or whatever.
437                  // I don't care if the thresholds don't match because that's what's stored in the
438                  // ReducedMenuSample.
439 <                std::vector<std::string> parameterNames=getNonThresholdParameterNames( trigger );
439 >                std::vector<std::string> parameterNames=l1menu::tools::getNonThresholdParameterNames( trigger );
440                  bool allParametersMatch=true;
441                  for( const auto& parameterName : parameterNames )
442                  {
# Line 408 | Line 482 | const std::map<std::string,size_t> l1men
482                  // ReducedMenuSample.
483                  if( triggerWasFound ) // Trigger can still fail, but no point doing this check if it already has
484                  {
485 <                        std::vector<std::string> parameterNames=getNonThresholdParameterNames( trigger );
485 >                        std::vector<std::string> parameterNames=l1menu::tools::getNonThresholdParameterNames( trigger );
486                          for( const auto& parameterName : parameterNames )
487                          {
488                                  if( trigger.parameter(parameterName)!=triggerInMenu.parameter(parameterName) ) triggerWasFound=false;
489                          }
490                  }
491  
492 <                std::vector<std::string> thresholdNames=l1menu::getThresholdNames(triggerInMenu);
492 >                std::vector<std::string> thresholdNames=l1menu::tools::getThresholdNames(triggerInMenu);
493                  if( triggerWasFound )
494                  {
495                          for( const auto& thresholdName : thresholdNames )
# Line 432 | Line 506 | const std::map<std::string,size_t> l1men
506          // (I guess - it would be a pretty pointless trigger though). To indicate the
507          // difference between that and a trigger that wasn't found I'll respectively
508          // return the empty vector or throw an exception.
509 <        if( !triggerWasFound ) throw std::runtime_error( "l1menu::ReducedMenuSample::getTriggerParameterIdentifiers() called for a trigger that was not used to create the sample" );
509 >        if( !triggerWasFound ) throw std::runtime_error( "l1menu::ReducedMenuSample::getTriggerParameterIdentifiers() called for a trigger that was not used to create the sample - "+trigger.name() );
510  
511          return returnValue;
512   }
513 +
514 + float l1menu::ReducedMenuSample::eventRate() const
515 + {
516 +        return pImple_->eventRate;
517 + }
518 +
519 + void l1menu::ReducedMenuSample::setEventRate( float rate ) const
520 + {
521 +        pImple_->eventRate=rate;
522 + }
523 +
524 + std::unique_ptr<const l1menu::IMenuRate> l1menu::ReducedMenuSample::rate( const l1menu::TriggerMenu& menu ) const
525 + {
526 +        // TODO make sure the TriggerMenu is valid for this sample
527 +
528 +        // I need a non-const menu, so make a copy
529 +        l1menu::TriggerMenu mutableMenu( menu );
530 +
531 +        // The number of events that pass each trigger
532 +        std::vector<size_t> numberOfEventsPassed( menu.numberOfTriggers() );
533 +        float numberOfEventsPassingAnyTrigger;
534 +
535 +        for( size_t triggerNumber=0; triggerNumber<menu.numberOfTriggers(); ++triggerNumber )
536 +        {
537 +                mutableMenu.getTrigger( triggerNumber ).initiateForReducedSample( *this );
538 +        }
539 +
540 +        for( size_t eventNumber=0; eventNumber<numberOfEvents(); ++eventNumber )
541 +        {
542 +                const l1menu::IReducedEvent& event=getEvent(eventNumber);
543 +                bool anyTriggerPassed=false;
544 +
545 +                for( size_t triggerNumber=0; triggerNumber<menu.numberOfTriggers(); ++triggerNumber )
546 +                {
547 +                        if( mutableMenu.getTrigger( triggerNumber ).apply( event ) )
548 +                        {
549 +                                // If the event passes the trigger, increment the counter
550 +                                ++numberOfEventsPassed[triggerNumber];
551 +                                anyTriggerPassed=true;
552 +                        }
553 +                }
554 +
555 +                if( anyTriggerPassed ) ++numberOfEventsPassingAnyTrigger;
556 +        }
557 +
558 +        ::MenuRateImplementation* pRates=new ::MenuRateImplementation;
559 +        pRates->setScaling( pImple_->eventRate );
560 +        // This is the value I want to return, but I still need access to the extended attributes of the subclass
561 +        std::unique_ptr<const l1menu::IMenuRate> pReturnValue( pRates );
562 +
563 +        pRates->setTotalFraction( static_cast<float>(numberOfEventsPassingAnyTrigger)/static_cast<float>(numberOfEvents()) );
564 +
565 +        for( size_t triggerNumber=0; triggerNumber<numberOfEventsPassed.size(); ++triggerNumber )
566 +        {
567 +                float fraction=static_cast<float>(numberOfEventsPassed[triggerNumber])/static_cast<float>(numberOfEvents());
568 +                pRates->addTriggerRate( mutableMenu.getTrigger(triggerNumber), fraction );
569 +        }
570 +
571 +        return pReturnValue;
572 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines