ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbbAnalysis/VHbbDataFormats/interface/TriggerReader.h
Revision: 1.5
Committed: Sat Sep 3 08:00:55 2011 UTC (13 years, 8 months ago) by arizzi
Content type: text/plain
Branch: MAIN
CVS Tags: AR_Sep8_LightNtuple
Changes since 1.4: +6 -4 lines
Log Message:
 create regexp only if needed

File Contents

# User Rev Content
1 arizzi 1.1 #ifndef TRIGGERREADER_H
2     #define TRIGGERREADER_H
3     #include "DataFormats/FWLite/interface/Event.h"
4     #include "DataFormats/FWLite/interface/Handle.h"
5     #include "DataFormats/Common/interface/TriggerResults.h"
6     #include "FWCore/Common/interface/TriggerNames.h"
7     #include <string>
8     #include <map>
9     #include <TString.h>
10     #include <TRegexp.h>
11     #include <regex.h>
12     #include <stdio.h>
13    
14     class TriggerReader {
15     public:
16 arizzi 1.3 TriggerReader(bool passAllEvents=false) : passAll(passAllEvents) {}
17 arizzi 1.1
18     void setEvent( fwlite::Event * e) { ev=e;}
19    
20     bool accept(const std::string & triggername)
21     {
22 arizzi 1.2 if(passAll) return true;
23 arizzi 1.1 fwlite::Handle<edm::TriggerResults> hTriggerResults;
24     hTriggerResults.getByLabel(*ev,"TriggerResults","","HLT");
25    
26    
27     regex_t regex;
28     int reti;
29    
30    
31    
32    
33     std::map<std::string,size_t>::iterator nit;
34 arizzi 1.3 if(ev->getRun().run() != cacheRun[triggername] || nameMap.find(triggername) == nameMap.end())
35 arizzi 1.1 {
36 arizzi 1.5
37     reti = regcomp(&regex, triggername.c_str(), 0);
38     // reti = regcomp(&regex, "HLT_Ele32_CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT_v.*", 0);
39     if( reti ){ std::cerr << "Could not compile regex" << std::endl;}
40    
41 arizzi 1.3 std::cout << "new run" << ev->getRun().run() << std::endl;
42     cacheRun[triggername]=ev->getRun().run();
43 arizzi 1.1 edm::TriggerNames const& triggerNames = ev->triggerNames(*hTriggerResults);
44     std::string oldiname="whatever";
45     nit=nameMap.find(triggername);
46     if(nit!=nameMap.end() && nit->second < triggerNames.size())
47     {
48     oldiname=triggerNames.triggerName(nit->second);
49     }
50     // std::cout << "whatever3" << std::endl;
51     // std::cout << "reg1 " << regexec(&regex, oldiname.c_str(), 0, NULL, 0) << std::endl;
52     if(nit==nameMap.end() || ( regexec(&regex, oldiname.c_str(), 0, NULL, 0) != 0 )) //; ! oldiname.Contains( TRegexp(triggername)) ) // changed, search it
53     {
54     std::cout << "searching " << triggername << std::endl;
55     for (unsigned i = 0; i < triggerNames.size(); ++i) {
56     std::string iname(triggerNames.triggerName(i));
57    
58     if( regexec(&regex, iname.c_str(), 0, NULL, 0) == 0 ) //iname.Contains(TRegexp(triggername)) )
59     {
60     nameMap[triggername]= i;
61     std::cout << "FOUND: " << triggerNames.triggerName(i) << " is bit " << i << std::endl;
62     }
63     }
64    
65     }
66 arizzi 1.5 regfree(&regex);
67 arizzi 1.1 }
68     nit=nameMap.find(triggername);
69     if(nit==nameMap.end())
70     {
71     std::cout << "ERROR: trigger name not found" << std::endl;
72     edm::TriggerNames const& triggerNames = ev->triggerNames(*hTriggerResults);
73 arizzi 1.3 // for (unsigned i = 0; i < triggerNames.size(); ++i) std::cout << triggerNames.triggerName(i) << " is bit " << i << "looking for: "<< triggername << std::endl;
74     nameMap[triggername]=100000000; // meaning not found in this run
75 arizzi 1.1
76     return false;
77     }
78 arizzi 1.3 if(nit->second==100000000) return false;
79 arizzi 1.1 return hTriggerResults->accept(nit->second);
80     }
81    
82     private:
83     std::map<std::string,size_t> nameMap;
84 arizzi 1.3 std::map<std::string,size_t> cacheRun;
85     // unsigned int cacheRun;
86 arizzi 1.1 fwlite::Event * ev;
87 arizzi 1.2 bool passAll;
88 arizzi 1.1
89     };
90    
91     #endif