ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/interface/Tuple_Triggers.h
Revision: 1.1
Committed: Wed Nov 7 21:53:11 2012 UTC (12 years, 5 months ago) by bbetchar
Content type: text/plain
Branch: MAIN
Log Message:
Grab ntuplizing codes from UserCode/SusyCAF

File Contents

# User Rev Content
1 bbetchar 1.1 #ifndef TUPLE_TRIGGERS
2     #define TUPLE_TRIGGERS
3    
4     #include <algorithm>
5    
6     #include "FWCore/Framework/interface/EDProducer.h"
7     #include "FWCore/Framework/interface/Frameworkfwd.h"
8     #include "FWCore/Framework/interface/Event.h"
9     #include "DataFormats/HLTReco/interface/TriggerEvent.h"
10     #include "DataFormats/Common/interface/TriggerResults.h"
11     #include "FWCore/Common/interface/TriggerNames.h"
12     #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
13     #include "FWCore/Utilities/interface/InputTag.h"
14    
15     class Tuple_Triggers : public edm::EDProducer
16     {
17     public:
18     explicit Tuple_Triggers(const edm::ParameterSet& conf)
19     : inputTag (conf.getParameter<edm::InputTag>("InputTag"))
20     , sourceName(conf.getParameter<std::string> ("SourceName"))
21     , sourceType(NOT_APPLICABLE)
22     , tag_( conf.getParameter<edm::InputTag>("TriggerEventInputTag"))
23     , run_(-1)
24     {
25     // Source is either a stream or a dataset (mutually exclusive)
26     if (sourceName.length() > 0) {
27     if (sourceName.length() >= 2 && sourceName[0]=='S' && sourceName[1]==':') {
28     sourceType = STREAM;
29     sourceName = sourceName.substr(2);
30     }
31     else if (sourceName.length() >= 3 && sourceName[0]=='D' && sourceName[1]=='S' && sourceName[2]==':') {
32     sourceType = DATASET;
33     sourceName = sourceName.substr(3);
34     }
35     else throw edm::Exception(edm::errors::Configuration)
36     << "Invalid SourceName = '" << sourceName
37     << "' -- must start with either 'S:' for streams or 'DS:' for datasets."
38     << std::endl;
39     }
40    
41     produces <bool> ( "hltHandleValid");
42     produces <std::string> ( "hltKey");
43     produces <std::map<std::string,bool> >("triggered");
44     produces <std::map<std::string,bool> >("parasiticTrigger");
45     produces <std::map<std::string,int> > ("prescaled");
46     produces <std::map<std::string,std::string> >("hltL1Seeds");
47     }
48    
49     private:
50     enum DataSource { NOT_APPLICABLE, STREAM, DATASET };
51     edm::InputTag inputTag;
52     std::string sourceName;
53     DataSource sourceType;
54     HLTConfigProvider hltConfig;
55     edm::InputTag tag_;
56     int run_;
57    
58     // Stored per run/lumisection to save time
59     std::vector<std::string> dataSource;
60    
61     void printNames(const std::vector<std::string>& names) {
62     for (unsigned int i = 0; i < names.size(); ++i)
63     edm::LogProblem( "Tuple_Triggers" ) << " " << names[i] << std::endl;
64     }
65    
66     void getDataSource() {
67     dataSource.clear();
68     if (sourceType == NOT_APPLICABLE) return;
69    
70     if (sourceType == STREAM) {
71     unsigned int index = hltConfig.streamIndex(sourceName);
72     if (index >= hltConfig.streamNames().size()) {
73     edm::LogError( "Tuple_Triggers" ) << "Streams in '" << inputTag.process() << "' HLT menu:";
74     printNames(hltConfig.streamNames());
75     throw edm::Exception(edm::errors::Configuration) << "Stream with name '" << sourceName << "' does not exist." << std::endl;
76     }
77     dataSource = hltConfig.streamContent(sourceName);
78     }
79     else {
80     unsigned int index = hltConfig.datasetIndex(sourceName);
81     if (index >= hltConfig.datasetNames().size()) {
82     edm::LogError( "Tuple_Triggers" ) << "Datasets in '" << inputTag.process() << "' HLT menu:";
83     printNames(hltConfig.datasetNames());
84     throw edm::Exception(edm::errors::Configuration) << "Dataset with name '" << sourceName << "' does not exist." << std::endl;
85     }
86     dataSource = hltConfig.datasetContent(sourceName);
87     }
88     }
89    
90     void produce( edm::Event& event, const edm::EventSetup& setup) {
91    
92     if ( int(event.getRun().run()) != run_ ) {
93     // Set process name using method here: https://hypernews.cern.ch/HyperNews/CMS/get/physTools/1791/1/1/1/1/1/2.html
94     if ( inputTag.process().empty() ) {
95     edm::Handle<trigger::TriggerEvent> temp;
96     event.getByLabel( tag_, temp );
97     if( temp.isValid() ) { inputTag = edm::InputTag( inputTag.label(), inputTag.instance(), temp.provenance()->processName() ); }
98     else { edm::LogError( "Tuple_Triggers" ) << "[SusyCAF::produce] Cannot retrieve TriggerEvent product for " << tag_; }
99     }
100     // Initialise HLTConfigProvider
101     bool hltChanged = false;
102     if (!hltConfig.init(event.getRun(), setup, inputTag.process(), hltChanged) ) {
103     edm::LogError( "Tuple_Triggers" ) << "HLT config initialization error with process name \"" << inputTag.process() << "\".";
104     } else if ( hltConfig.size() < 1 ) {
105     edm::LogError( "Tuple_Triggers" ) << "HLT config has zero size.";
106     }
107     getDataSource();
108     }
109    
110     // Retrieve TriggerResults with appropriate InputTag
111     edm::Handle<edm::TriggerResults> results;
112     event.getByLabel( inputTag, results );
113     //std::cout << "Retrieving TriggerResults with: " << inputTag << std::endl;
114    
115     std::auto_ptr<std::map<std::string,bool> > triggered(new std::map<std::string,bool>());
116     std::auto_ptr<std::map<std::string,bool> > parasiticTrigger(new std::map<std::string,bool>());
117     std::auto_ptr<std::map<std::string,int> > prescaled(new std::map<std::string,int>());
118     std::auto_ptr<std::map<std::string,std::string> > hltL1Seeds(new std::map<std::string,std::string>());
119    
120     if(results.isValid()) {
121     const edm::TriggerNames& names = event.triggerNames(*results);
122     for(unsigned i=0; i < results->size(); i++) {
123     (*prescaled)[names.triggerName(i)] = hltConfig.prescaleValue(event,setup,names.triggerName(i));
124    
125     if (dataSource.empty() || std::find(dataSource.begin(), dataSource.end(), names.triggerName(i)) != dataSource.end()) {
126     (*triggered)[names.triggerName(i)] = results->accept(i) ;
127     const std::vector<std::pair<bool,std::string> >& l1Seeds = hltConfig.hltL1GTSeeds(i);
128     if (l1Seeds.size() == 1) {
129     (*hltL1Seeds)[names.triggerName(i)] = l1Seeds.front().second;
130     }
131     else if (l1Seeds.size() > 1) {
132     std::string combined;
133     for (unsigned iSeed = 0; iSeed < l1Seeds.size(); ++iSeed) {
134     if (combined.length()) combined += " OR ";
135     combined += "("+l1Seeds[iSeed].second+")";
136     } // end loop over L1 seeds
137     (*hltL1Seeds)[names.triggerName(i)] = combined;
138     }
139     } else
140     (*parasiticTrigger)[names.triggerName(i)] = results->accept(i) ;
141     }
142     } else { edm::LogError( "Tuple_Triggers" ) << "[SusyCAF::produce] Cannot retrieve TriggerResults product for " << inputTag; }
143    
144     std::auto_ptr<std::vector<std::string> > source(new std::vector<std::string>(dataSource));
145    
146     event.put( std::auto_ptr<bool>(new bool(results.isValid())), "hltHandleValid");
147     event.put( std::auto_ptr<std::string>(new std::string(hltConfig.tableName())), "hltKey");
148     event.put( triggered,"triggered");
149     event.put( parasiticTrigger,"parasiticTrigger");
150     event.put( prescaled,"prescaled");
151     event.put( hltL1Seeds,"hltL1Seeds");
152     }
153    
154     };
155    
156     #endif