ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/grimes/L1Menu/src/MenuRatePlots.cpp
Revision: 1.13
Committed: Wed Jul 24 11:48:55 2013 UTC (11 years, 9 months ago) by grimes
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +38 -0 lines
Log Message:
Big commit of lots of changes before migrating to Git.

File Contents

# Content
1 #include "l1menu/MenuRatePlots.h"
2
3 #include <sstream>
4 #include "l1menu/ITrigger.h"
5 #include "l1menu/TriggerMenu.h"
6 #include "l1menu/TriggerRatePlot.h"
7 #include "l1menu/tools/tools.h"
8 #include <TH1F.h>
9 #include <TDirectory.h>
10 #include <TKey.h>
11 #include <iostream>
12
13 l1menu::MenuRatePlots::MenuRatePlots( const l1menu::TriggerMenu& triggerMenu, TDirectory* pDirectory )
14 {
15 // Before making any histograms make sure errors are done properly
16 TH1::SetDefaultSumw2();
17
18 // This is always useful
19 const l1menu::TriggerTable& triggerTable=l1menu::TriggerTable::instance();
20
21 // Loop over each of the triggers in the menu, book a histogram for it and then create
22 // a l1menu::TriggerRate plot for it.
23 for( size_t triggerNumber=0; triggerNumber<triggerMenu.numberOfTriggers(); ++triggerNumber )
24 {
25 std::unique_ptr<l1menu::ITrigger> pTrigger=triggerMenu.getTriggerCopy(triggerNumber);
26 // Figure out the parameter names of all the possible thresholds.
27 const std::vector<std::string> thresholdNames=l1menu::tools::getThresholdNames(*pTrigger);
28
29 //
30 // If there is more than one threshold add a plot where all of the thresholds are scaled together.
31 //
32 if( thresholdNames.size()>1 )
33 {
34 const std::string& mainThreshold=thresholdNames.front();
35 unsigned int numberOfBins=100;
36 float lowerEdge=0;
37 float upperEdge=100;
38 try
39 {
40 numberOfBins=triggerTable.getSuggestedNumberOfBins( pTrigger->name(), mainThreshold );
41 lowerEdge=triggerTable.getSuggestedLowerEdge( pTrigger->name(), mainThreshold );
42 upperEdge=triggerTable.getSuggestedUpperEdge( pTrigger->name(), mainThreshold );
43 }
44 catch( std::exception& error) { /* Do nothing. If no binning suggestions have been set for this trigger use the defaults I set above. */ }
45
46 std::unique_ptr<TH1> pHistogram( new TH1F( (pTrigger->name()+"_v_allThresholdsScaled").c_str(), "This title gets changed by TriggerRatePlot anyway", numberOfBins, lowerEdge, upperEdge ) );
47 pHistogram->SetDirectory( pDirectory );
48 // Passing thresholdNames tells the TriggerRatePlot to scale all parameters named in that
49 // vector along with mainThreshold.
50 triggerPlots_.push_back( std::move(l1menu::TriggerRatePlot(*pTrigger,std::move(pHistogram),mainThreshold,thresholdNames)) );
51
52 }
53
54 // When a threshold is tested, I want all the other thresholds to be zero. I'll run through
55 // and zero all of them now.
56 for( std::vector<std::string>::const_iterator iName=thresholdNames.begin(); iName!=thresholdNames.end(); ++iName )
57 {
58 pTrigger->parameter(*iName)=0;
59 }
60
61
62 // I want a plot for each of the thresholds, so I'll loop over the threshold names
63 for( std::vector<std::string>::const_iterator iThresholdName=thresholdNames.begin(); iThresholdName!=thresholdNames.end(); ++iThresholdName )
64 {
65 unsigned int numberOfBins=100;
66 float lowerEdge=0;
67 float upperEdge=100;
68 try
69 {
70 numberOfBins=triggerTable.getSuggestedNumberOfBins( pTrigger->name(), *iThresholdName );
71 lowerEdge=triggerTable.getSuggestedLowerEdge( pTrigger->name(), *iThresholdName );
72 upperEdge=triggerTable.getSuggestedUpperEdge( pTrigger->name(), *iThresholdName );
73 }
74 catch( std::exception& error) { /* Do nothing. If no binning suggestions have been set for this trigger use the defaults I set above. */ }
75
76 std::unique_ptr<TH1> pHistogram( new TH1F( (pTrigger->name()+"_v_"+(*iThresholdName)).c_str(), "This title gets changed by TriggerRatePlot anyway", numberOfBins, lowerEdge, upperEdge ) );
77 pHistogram->SetDirectory( pDirectory );
78 triggerPlots_.push_back( std::move(l1menu::TriggerRatePlot(*pTrigger,std::move(pHistogram),*iThresholdName)) );
79 }
80
81 } // end of loop over the triggers in the menu
82 }
83
84 l1menu::MenuRatePlots::MenuRatePlots( const TDirectory* pPreExistingPlotDirectory )
85 {
86 // Before making any histograms make sure errors are done properly
87 TH1::SetDefaultSumw2();
88
89 // Loop over all of the histograms in the directory.
90 TList* pListOfKeys=pPreExistingPlotDirectory->GetListOfKeys();
91 std::string oldKeyName;
92
93 for( int index=0; index<pListOfKeys->GetEntries(); ++index )
94 {
95 TKey* pKey=dynamic_cast<TKey*>( pListOfKeys->At(index) );
96 // Only use the highest cycle number for each key
97 if( oldKeyName==pKey->GetName() ) continue;
98 oldKeyName=pKey->GetName();
99
100 TH1* pHistogram=dynamic_cast<TH1*>( pKey->ReadObj() );
101
102 if( pHistogram!=NULL )
103 {
104 try
105 {
106 l1menu::TriggerRatePlot ratePlotFromHistogram( pHistogram );
107 triggerPlots_.push_back( std::move(ratePlotFromHistogram) );
108 }
109 catch( std::exception& error )
110 {
111 std::cerr << "Couldn't create TriggerRatePlot for " << pHistogram->GetName() << " because: " << error.what() << std::endl;
112 }
113
114 } // end of "if( dynamic_cast to TH1* successful )"
115
116 } // end of loop over the keys in the file
117 }
118
119 void l1menu::MenuRatePlots::addEvent( const l1menu::IEvent& event )
120 {
121 // Loop over each of the TriggerRatePlots and add the event to each of them.
122 for( auto& ratePlot : triggerPlots_ )
123 {
124 ratePlot.addEvent( event );
125 }
126 }
127
128 void l1menu::MenuRatePlots::addSample( const l1menu::ISample& sample )
129 {
130 // Loop over each of the TriggerRatePlots and add the sample to each of them.
131 for( auto& ratePlot : triggerPlots_ )
132 {
133 ratePlot.addSample( sample );
134 }
135 }
136
137 void l1menu::MenuRatePlots::setDirectory( TDirectory* pDirectory )
138 {
139 // Loop over each of the TriggerRatePlots and individually set the directory.
140 for( auto& ratePlot : triggerPlots_ )
141 {
142 ratePlot.getPlot()->SetDirectory( pDirectory );
143 }
144 }
145
146 std::vector<TH1*> l1menu::MenuRatePlots::getPlots()
147 {
148 std::vector<TH1*> returnValue;
149 for( auto& ratePlot : triggerPlots_ )
150 {
151 returnValue.push_back( ratePlot.getPlot() );
152 }
153 return returnValue;
154 }
155
156 void l1menu::MenuRatePlots::relinquishOwnershipOfPlots()
157 {
158 // Loop over each of the TriggerRatePlots and individually release them.
159 for( std::vector<l1menu::TriggerRatePlot>::iterator iRatePlot=triggerPlots_.begin(); iRatePlot!=triggerPlots_.end(); ++iRatePlot )
160 {
161 iRatePlot->relinquishOwnershipOfPlot();
162 }
163 }