ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbbAnalysis/VHbbDataFormats/interface/TriggerReader.h
Revision: 1.6
Committed: Mon Sep 12 22:16:56 2011 UTC (13 years, 7 months ago) by arizzi
Content type: text/plain
Branch: MAIN
Changes since 1.5: +13 -7 lines
Log Message:
fixes

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 arizzi 1.6 void setEvent( fwlite::Event * e) { ev=e;
19     if(!passAll) {
20     hTriggerResults.getByLabel(*ev,"TriggerResults","","HLT");
21     run = ev->eventAuxiliary().id().run();
22     }
23     }
24 arizzi 1.1
25     bool accept(const std::string & triggername)
26     {
27 arizzi 1.2 if(passAll) return true;
28 arizzi 1.1
29    
30     regex_t regex;
31     int reti;
32    
33    
34    
35    
36     std::map<std::string,size_t>::iterator nit;
37 arizzi 1.6 if(run != cacheRun[triggername] || nameMap.find(triggername) == nameMap.end())
38 arizzi 1.1 {
39 arizzi 1.5
40     reti = regcomp(&regex, triggername.c_str(), 0);
41     // reti = regcomp(&regex, "HLT_Ele32_CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT_v.*", 0);
42     if( reti ){ std::cerr << "Could not compile regex" << std::endl;}
43    
44 arizzi 1.6 std::cout << "new run" << run << std::endl;
45     // cacheRun[triggername]=ev->getRun().run();
46     cacheRun[triggername]=run;
47 arizzi 1.1 edm::TriggerNames const& triggerNames = ev->triggerNames(*hTriggerResults);
48     std::string oldiname="whatever";
49     nit=nameMap.find(triggername);
50     if(nit!=nameMap.end() && nit->second < triggerNames.size())
51     {
52     oldiname=triggerNames.triggerName(nit->second);
53     }
54     // std::cout << "whatever3" << std::endl;
55     // std::cout << "reg1 " << regexec(&regex, oldiname.c_str(), 0, NULL, 0) << std::endl;
56     if(nit==nameMap.end() || ( regexec(&regex, oldiname.c_str(), 0, NULL, 0) != 0 )) //; ! oldiname.Contains( TRegexp(triggername)) ) // changed, search it
57     {
58     std::cout << "searching " << triggername << std::endl;
59     for (unsigned i = 0; i < triggerNames.size(); ++i) {
60     std::string iname(triggerNames.triggerName(i));
61    
62     if( regexec(&regex, iname.c_str(), 0, NULL, 0) == 0 ) //iname.Contains(TRegexp(triggername)) )
63     {
64     nameMap[triggername]= i;
65     std::cout << "FOUND: " << triggerNames.triggerName(i) << " is bit " << i << std::endl;
66     }
67     }
68    
69     }
70 arizzi 1.5 regfree(&regex);
71 arizzi 1.1 }
72     nit=nameMap.find(triggername);
73     if(nit==nameMap.end())
74     {
75     std::cout << "ERROR: trigger name not found" << std::endl;
76 arizzi 1.6 // edm::TriggerNames const& triggerNames = ev->triggerNames(*hTriggerResults);
77 arizzi 1.3 // for (unsigned i = 0; i < triggerNames.size(); ++i) std::cout << triggerNames.triggerName(i) << " is bit " << i << "looking for: "<< triggername << std::endl;
78     nameMap[triggername]=100000000; // meaning not found in this run
79 arizzi 1.1
80     return false;
81     }
82 arizzi 1.3 if(nit->second==100000000) return false;
83 arizzi 1.1 return hTriggerResults->accept(nit->second);
84     }
85    
86     private:
87 arizzi 1.6 fwlite::Handle<edm::TriggerResults> hTriggerResults;
88 arizzi 1.1 std::map<std::string,size_t> nameMap;
89 arizzi 1.3 std::map<std::string,size_t> cacheRun;
90     // unsigned int cacheRun;
91 arizzi 1.1 fwlite::Event * ev;
92 arizzi 1.2 bool passAll;
93 arizzi 1.6 unsigned int run;
94 arizzi 1.1
95     };
96    
97     #endif