ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/L1RpcTriggerAnalysis/src/GoldenPattern.cc
Revision: 1.2
Committed: Mon May 20 23:27:11 2013 UTC (11 years, 11 months ago) by konec
Content type: text/plain
Branch: MAIN
Changes since 1.1: +11 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 konec 1.1 #include "UserCode/L1RpcTriggerAnalysis/interface/GoldenPattern.h"
2    
3     #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
4     #include "DataFormats/MuonDetId/interface/RPCDetId.h"
5     #include "DataFormats/MuonDetId/interface/CSCDetId.h"
6     #include "DataFormats/MuonDetId/interface/DTChamberId.h"
7    
8     #include "UserCode/L1RpcTriggerAnalysis/interface/RPCDetIdUtil.h"
9     #include "UserCode/L1RpcTriggerAnalysis/interface/DTphDigiSpec.h"
10     #include "UserCode/L1RpcTriggerAnalysis/interface/CSCDigiSpec.h"
11     #include "UserCode/L1RpcTriggerAnalysis/interface/RPCDigiSpec.h"
12    
13    
14     void GoldenPattern::Result::runNoCheck() const
15     {
16     double fract = 1;
17     for (auto i=posRpcResult.begin(); i!=posRpcResult.end();i++) fract *= norm(POSRPC,i->second);
18     for (auto i=posCscResult.begin(); i!=posCscResult.end();i++) fract *= norm(POSCSC,i->second);
19     for (auto i=posDtResult.begin(); i!=posDtResult.end();i++) fract *= norm(POSDT, i->second);
20     for (auto i=benCscResult.begin(); i!=benCscResult.end();i++) fract *= norm(BENCSC,i->second);
21     for (auto i=benDtResult.begin(); i!=benDtResult.end();i++) fract *= norm(BENDT, i->second);
22     unsigned int nTot = nMatchedPosRpc+nMatchedPosCsc+nMatchedPosDt+nMatchedBenCsc+nMatchedBenDt;
23     theValue = ( nTot > 2) ? pow(fract, 1./((double) nTot)) : 0.;
24     if (posRpcResult.size() != nMatchedPosRpc) theValue = 0.;
25     if (posCscResult.size() != nMatchedPosCsc) theValue = 0.;
26     if (posDtResult.size() != nMatchedPosDt) theValue = 0.;
27     }
28    
29     double GoldenPattern::Result::norm(GoldenPattern::PosBenCase where, double whereInDist) const {
30     double normValue = 2.*(0.5-fabs(whereInDist-0.5));
31     static const double epsilon = 1.e-9;
32     if (normValue > epsilon) {
33     switch (where) {
34     case POSRPC : nMatchedPosRpc++; break;
35     case POSCSC : nMatchedPosCsc++; break;
36     case BENCSC : nMatchedBenCsc++; break;
37     case POSDT : nMatchedPosDt++; break;
38     case BENDT : nMatchedBenDt++; break;
39     };
40     } else {
41     normValue =1.;
42     }
43     return normValue;
44     }
45    
46     bool GoldenPattern::Result::operator < (const GoldenPattern::Result &o) const {
47 konec 1.2 if ( *this && o) {
48     if (nMatchedTot() < o.nMatchedTot()) return true;
49     else if (nMatchedTot() == o.nMatchedTot() && value() < o.value()) return true;
50     else return false;
51     }
52     else if (o) {return true; }
53     else if (*this) { return false; }
54     else return false;
55    
56     // return (value() < o.value() );
57     // return false;
58 konec 1.1 }
59    
60     GoldenPattern::Result::operator bool() const {
61     return (value() > 0.1);
62     }
63    
64     double GoldenPattern::Result::value() const {
65     run();
66     return theValue;
67     }
68    
69     unsigned int GoldenPattern::Result::nMatchedTot() const {
70     run();
71     return nMatchedPosRpc+nMatchedPosCsc+nMatchedBenCsc+nMatchedPosDt+nMatchedBenDt;
72     }
73    
74     std::ostream & operator << (std::ostream &out, const GoldenPattern::Result& o)
75     {
76     o.run();
77     out <<"Result: "
78     << " value: "<<o.theValue
79     <<" nPos+Ben: ("<<o.nMatchedPosCsc<<"/"<<o.posCscResult.size()<<"+"<<o.nMatchedBenCsc<<"/"<<o.benCscResult.size()
80     <<", "<<o.nMatchedPosDt <<"/"<<o.posDtResult.size()<<"+"<<o.nMatchedBenDt<<"/"<<o.benDtResult.size()
81     <<", "<<o.nMatchedPosRpc<<"/"<<o.posRpcResult.size()<<", tot:"<<o.nMatchedTot()<<")";
82     return out;
83     }
84    
85    
86    
87     void GoldenPattern::add( GoldenPattern::PosBenCase aCase, uint32_t rawId, int posOrBen, unsigned int freq)
88     {
89     switch ( aCase ) {
90     case POSRPC : posRpc[rawId][posOrBen] += freq; break;
91     case POSCSC : posCsc[rawId][posOrBen] += freq; break;
92     case BENCSC : bendingCsc[rawId][posOrBen] += freq; break;
93     case POSDT : posDt[rawId][posOrBen] += freq; break;
94     case BENDT : bendingDt[rawId][posOrBen] += freq; break;
95     };
96     }
97    
98    
99     void GoldenPattern::add(const Pattern & p)
100     {
101     const Pattern::DataType & detdigi = p ;
102     for (Pattern::DataType::const_iterator is = detdigi.begin(); is != detdigi.end(); ++is) {
103     uint32_t rawId = is->first;
104     DetId detId(rawId);
105     if (detId.det() != DetId::Muon) std::cout << "PROBLEM ;;;"<<std::endl;
106     switch (detId.subdetId()) {
107     case MuonSubdetId::RPC: {
108     RPCDigiSpec digi(rawId, is->second);
109     posRpc[rawId][digi.halfStrip()]++;
110     break;
111     }
112     case MuonSubdetId::DT: {
113     DTphDigiSpec digi(rawId, is->second);
114     if (digi.bxNum() != 0 || digi.bxCnt() != 0 || digi.ts2() != 0) break;
115     posDt[rawId][digi.phi()]++;
116     bendingDt[rawId][digi.phiB()]++;
117     break;
118     }
119     case MuonSubdetId::CSC: {
120     CSCDigiSpec digi(rawId, is->second);
121     posCsc[rawId][digi.strip()]++;
122     bendingCsc[rawId][digi.pattern()]++;
123     break;
124     }
125     default: {std::cout <<" Unexpeced sebdet case, id ="<<is->first <<std::endl; return; }
126     };
127     }
128     }
129    
130     GoldenPattern::Result GoldenPattern::compare(const Pattern &p) const
131     {
132     Result result;
133     const Pattern::DataType & detdigi = p;
134     for (Pattern::DataType::const_iterator is = detdigi.begin(); is != detdigi.end(); ++is) {
135     uint32_t rawId = is->first;
136     DetId detId(rawId);
137     if (detId.det() != DetId::Muon) std::cout << "PROBLEM ;;;"<<std::endl;
138     if (detId.subdetId() == MuonSubdetId::RPC) {
139     RPCDigiSpec digi(rawId, is->second);
140     DetFreq::const_iterator idm = posRpc.find(rawId);
141     if (idm != posRpc.end() ) {
142     double f = whereInDistribution(digi.halfStrip(), idm->second);
143     result.posRpcResult.push_back( std::make_pair(rawId, f) );
144     }
145     } else if (detId.subdetId() == MuonSubdetId::DT) {
146     DTphDigiSpec digi(rawId, is->second);
147     DetFreq::const_iterator idm = posDt.find(rawId);
148     if (idm != posDt.end() ) {
149     double f = whereInDistribution(digi.phi(), idm->second);
150     result.posDtResult.push_back( std::make_pair(rawId, f) );
151     }
152     idm = bendingDt.find(rawId);
153     if (idm != bendingDt.end() ) {
154     double f = whereInDistribution(digi.phiB(), idm->second);
155     result.benDtResult.push_back( std::make_pair(rawId, f) );
156     }
157     } else if (detId.subdetId() == MuonSubdetId::CSC) {
158     CSCDigiSpec digi(rawId, is->second);
159     DetFreq::const_iterator idm = posCsc.find(rawId);
160     if (idm != posCsc.end() ) {
161     double f = whereInDistribution(digi.strip(), idm->second);
162     result.posCscResult.push_back( std::make_pair(rawId, f) );
163     }
164     idm = bendingCsc.find(rawId);
165     if (idm != bendingCsc.end() ) {
166     double f = whereInDistribution(digi.pattern(), idm->second);
167     result.benCscResult.push_back( std::make_pair(rawId, f) );
168     }
169     }
170     }
171     return result;
172     }
173    
174     double GoldenPattern::whereInDistribution( int obj, const GoldenPattern::MFreq & m) const
175     {
176     double sum_before = 0;
177     double sum_after = 0;
178     double sum_obj = 0;
179     for (MFreq::const_iterator im = m.begin(); im != m.end(); im++) {
180     if (im->first < obj) sum_before+= im->second;
181     if (im->first == obj) sum_obj = im->second;
182     if (im->first > obj) sum_after += im->second;
183     }
184     double sum = std::max(1.,sum_before+sum_after+sum_obj );
185     return (sum_before+sum_obj/2.)/sum;
186     }
187    
188     void GoldenPattern::purge()
189     {
190     for (DetFreq::iterator idf=posRpc.begin(); idf != posRpc.end(); idf++) {
191     MFreq & mfreq = idf->second;
192     MFreq::iterator imf = mfreq.begin();
193     while (imf != mfreq.end() ) {
194     bool remove = false;
195     int pos = imf->first;
196     unsigned int bef2 = (mfreq.find(pos-2) != mfreq.end()) ? mfreq[pos-2] : 0;
197     unsigned int bef1 = (mfreq.find(pos-1) != mfreq.end()) ? mfreq[pos-1] : 0;
198     unsigned int aft1 = (mfreq.find(pos+1) != mfreq.end()) ? mfreq[pos+1] : 0;
199     unsigned int aft2 = (mfreq.find(pos+2) != mfreq.end()) ? mfreq[pos+2] : 0;
200     unsigned int aft3 = (mfreq.find(pos+3) != mfreq.end()) ? mfreq[pos+3] : 0;
201     if (mfreq[pos]==1 && bef1==0 && aft1==0) remove = true;
202     if (mfreq[pos]==1 && aft1==1 && aft2==0 && aft3==0 && bef1==0 && bef2==0) remove = true;
203     if (mfreq[pos]==2 && aft1==0 && aft2==0 && bef1==0 && bef2==0) remove = true;
204     if (remove) { mfreq.erase(imf++); } else { ++imf; }
205     }
206     }
207     DetFreq::iterator idf=posRpc.begin();
208     while (idf != posRpc.end()) if (idf->second.size()==0) posRpc.erase(idf++); else ++idf;
209    
210     }
211    
212     std::ostream & operator << (std::ostream &out, const GoldenPattern & o) {
213     out <<"GoldenPattern"<< o.theKey <<std::endl;
214     // RPC
215     for (GoldenPattern::DetFreq::const_iterator im = o.posRpc.begin(); im != o.posRpc.end(); im++) {
216     out <<"RPC Det:"<< im->first<<" Pos: ";
217     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
218     out << std::endl;
219     }
220     // DT pos
221     for (GoldenPattern::DetFreq::const_iterator im = o.posDt.begin(); im != o.posDt.end(); im++) {
222     out <<"DT Det:"<< im->first<<" Pos: ";
223     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
224     out << std::endl;
225     }
226     // DT bending
227     for (GoldenPattern::DetFreq::const_iterator im = o.bendingDt.begin(); im != o.bendingDt.end(); im++) {
228     out <<"DT Det:"<< im->first<<" Ben: ";
229     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
230     out << std::endl;
231     }
232    
233     // Csc pos
234     for (GoldenPattern::DetFreq::const_iterator im = o.posCsc.begin(); im != o.posCsc.end(); im++) {
235     out <<"Csc Det:"<< im->first<<" Pos: ";
236     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
237     out << std::endl;
238     }
239     // Csc bending
240     for (GoldenPattern::DetFreq::const_iterator im = o.bendingCsc.begin(); im != o.bendingCsc.end(); im++) {
241     out <<"Csc Det:"<< im->first<<" Ben: ";
242     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
243     out << std::endl;
244     }
245    
246     return out;
247     }
248