ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/L1RpcTriggerAnalysis/src/GoldenPattern.cc
Revision: 1.3
Committed: Fri May 24 16:47:50 2013 UTC (11 years, 11 months ago) by konec
Content type: text/plain
Branch: MAIN
Changes since 1.2: +12 -3 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 konec 1.3 return (value() > 0.1); // && hasStation1 && hasStation2);
62 konec 1.1 }
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 konec 1.3 double f = whereInDistribution(digi.halfStrip(), idm->second);
143     result.posRpcResult.push_back( std::make_pair(rawId, f) );
144     RPCDetId rpc(rawId);
145     if(rpc.station()==1) result.hasStation1 = true;
146     if(rpc.station()==2) result.hasStation2 = true;
147 konec 1.1 }
148     } else if (detId.subdetId() == MuonSubdetId::DT) {
149     DTphDigiSpec digi(rawId, is->second);
150     DetFreq::const_iterator idm = posDt.find(rawId);
151     if (idm != posDt.end() ) {
152     double f = whereInDistribution(digi.phi(), idm->second);
153     result.posDtResult.push_back( std::make_pair(rawId, f) );
154 konec 1.3 DTChamberId dt(rawId);
155     if(dt.station()==1) result.hasStation1 = true;
156     if(dt.station()==2) result.hasStation2 = true;
157 konec 1.1 }
158     idm = bendingDt.find(rawId);
159     if (idm != bendingDt.end() ) {
160     double f = whereInDistribution(digi.phiB(), idm->second);
161     result.benDtResult.push_back( std::make_pair(rawId, f) );
162     }
163     } else if (detId.subdetId() == MuonSubdetId::CSC) {
164     CSCDigiSpec digi(rawId, is->second);
165     DetFreq::const_iterator idm = posCsc.find(rawId);
166     if (idm != posCsc.end() ) {
167     double f = whereInDistribution(digi.strip(), idm->second);
168     result.posCscResult.push_back( std::make_pair(rawId, f) );
169 konec 1.3 CSCDetId csc(rawId);
170     if (csc.station()==1) result.hasStation1 = true;
171     if (csc.station()==2) result.hasStation1 = true;
172 konec 1.1 }
173     idm = bendingCsc.find(rawId);
174     if (idm != bendingCsc.end() ) {
175     double f = whereInDistribution(digi.pattern(), idm->second);
176     result.benCscResult.push_back( std::make_pair(rawId, f) );
177     }
178     }
179     }
180     return result;
181     }
182    
183     double GoldenPattern::whereInDistribution( int obj, const GoldenPattern::MFreq & m) const
184     {
185     double sum_before = 0;
186     double sum_after = 0;
187     double sum_obj = 0;
188     for (MFreq::const_iterator im = m.begin(); im != m.end(); im++) {
189     if (im->first < obj) sum_before+= im->second;
190     if (im->first == obj) sum_obj = im->second;
191     if (im->first > obj) sum_after += im->second;
192     }
193     double sum = std::max(1.,sum_before+sum_after+sum_obj );
194     return (sum_before+sum_obj/2.)/sum;
195     }
196    
197     void GoldenPattern::purge()
198     {
199     for (DetFreq::iterator idf=posRpc.begin(); idf != posRpc.end(); idf++) {
200     MFreq & mfreq = idf->second;
201     MFreq::iterator imf = mfreq.begin();
202     while (imf != mfreq.end() ) {
203     bool remove = false;
204     int pos = imf->first;
205     unsigned int bef2 = (mfreq.find(pos-2) != mfreq.end()) ? mfreq[pos-2] : 0;
206     unsigned int bef1 = (mfreq.find(pos-1) != mfreq.end()) ? mfreq[pos-1] : 0;
207     unsigned int aft1 = (mfreq.find(pos+1) != mfreq.end()) ? mfreq[pos+1] : 0;
208     unsigned int aft2 = (mfreq.find(pos+2) != mfreq.end()) ? mfreq[pos+2] : 0;
209     unsigned int aft3 = (mfreq.find(pos+3) != mfreq.end()) ? mfreq[pos+3] : 0;
210     if (mfreq[pos]==1 && bef1==0 && aft1==0) remove = true;
211     if (mfreq[pos]==1 && aft1==1 && aft2==0 && aft3==0 && bef1==0 && bef2==0) remove = true;
212     if (mfreq[pos]==2 && aft1==0 && aft2==0 && bef1==0 && bef2==0) remove = true;
213     if (remove) { mfreq.erase(imf++); } else { ++imf; }
214     }
215     }
216     DetFreq::iterator idf=posRpc.begin();
217     while (idf != posRpc.end()) if (idf->second.size()==0) posRpc.erase(idf++); else ++idf;
218    
219     }
220    
221     std::ostream & operator << (std::ostream &out, const GoldenPattern & o) {
222     out <<"GoldenPattern"<< o.theKey <<std::endl;
223     // RPC
224     for (GoldenPattern::DetFreq::const_iterator im = o.posRpc.begin(); im != o.posRpc.end(); im++) {
225     out <<"RPC Det:"<< im->first<<" Pos: ";
226     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
227     out << std::endl;
228     }
229     // DT pos
230     for (GoldenPattern::DetFreq::const_iterator im = o.posDt.begin(); im != o.posDt.end(); im++) {
231     out <<"DT Det:"<< im->first<<" Pos: ";
232     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
233     out << std::endl;
234     }
235     // DT bending
236     for (GoldenPattern::DetFreq::const_iterator im = o.bendingDt.begin(); im != o.bendingDt.end(); im++) {
237     out <<"DT Det:"<< im->first<<" Ben: ";
238     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
239     out << std::endl;
240     }
241    
242     // Csc pos
243     for (GoldenPattern::DetFreq::const_iterator im = o.posCsc.begin(); im != o.posCsc.end(); im++) {
244     out <<"Csc Det:"<< im->first<<" Pos: ";
245     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
246     out << std::endl;
247     }
248     // Csc bending
249     for (GoldenPattern::DetFreq::const_iterator im = o.bendingCsc.begin(); im != o.bendingCsc.end(); im++) {
250     out <<"Csc Det:"<< im->first<<" Ben: ";
251     for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
252     out << std::endl;
253     }
254    
255     return out;
256     }
257