ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/L1RpcTriggerAnalysis/src/GoldenPattern.cc
Revision: 1.5
Committed: Tue Jun 25 10:49:35 2013 UTC (11 years, 10 months ago) by akalinow
Content type: text/plain
Branch: MAIN
Changes since 1.4: +18 -15 lines
Log Message:
* initial cleanup by AK

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 konec 1.4 theValue = ( nTot > 4) ? pow(fract, 1./((double) nTot)) : 0.;
24 konec 1.1 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 akalinow 1.5
75 konec 1.1 std::ostream & operator << (std::ostream &out, const GoldenPattern::Result& o)
76     {
77     o.run();
78     out <<"Result: "
79     << " value: "<<o.theValue
80     <<" nPos+Ben: ("<<o.nMatchedPosCsc<<"/"<<o.posCscResult.size()<<"+"<<o.nMatchedBenCsc<<"/"<<o.benCscResult.size()
81     <<", "<<o.nMatchedPosDt <<"/"<<o.posDtResult.size()<<"+"<<o.nMatchedBenDt<<"/"<<o.benDtResult.size()
82     <<", "<<o.nMatchedPosRpc<<"/"<<o.posRpcResult.size()<<", tot:"<<o.nMatchedTot()<<")";
83     return out;
84     }
85    
86    
87    
88     void GoldenPattern::add( GoldenPattern::PosBenCase aCase, uint32_t rawId, int posOrBen, unsigned int freq)
89     {
90     switch ( aCase ) {
91     case POSRPC : posRpc[rawId][posOrBen] += freq; break;
92     case POSCSC : posCsc[rawId][posOrBen] += freq; break;
93     case BENCSC : bendingCsc[rawId][posOrBen] += freq; break;
94     case POSDT : posDt[rawId][posOrBen] += freq; break;
95     case BENDT : bendingDt[rawId][posOrBen] += freq; break;
96     };
97     }
98    
99    
100     void GoldenPattern::add(const Pattern & p)
101     {
102     const Pattern::DataType & detdigi = p ;
103     for (Pattern::DataType::const_iterator is = detdigi.begin(); is != detdigi.end(); ++is) {
104     uint32_t rawId = is->first;
105     DetId detId(rawId);
106     if (detId.det() != DetId::Muon) std::cout << "PROBLEM ;;;"<<std::endl;
107     switch (detId.subdetId()) {
108     case MuonSubdetId::RPC: {
109     RPCDigiSpec digi(rawId, is->second);
110     posRpc[rawId][digi.halfStrip()]++;
111     break;
112     }
113     case MuonSubdetId::DT: {
114     DTphDigiSpec digi(rawId, is->second);
115     if (digi.bxNum() != 0 || digi.bxCnt() != 0 || digi.ts2() != 0) break;
116     posDt[rawId][digi.phi()]++;
117     bendingDt[rawId][digi.phiB()]++;
118     break;
119     }
120     case MuonSubdetId::CSC: {
121     CSCDigiSpec digi(rawId, is->second);
122     posCsc[rawId][digi.strip()]++;
123     bendingCsc[rawId][digi.pattern()]++;
124     break;
125     }
126     default: {std::cout <<" Unexpeced sebdet case, id ="<<is->first <<std::endl; return; }
127     };
128     }
129     }
130    
131     GoldenPattern::Result GoldenPattern::compare(const Pattern &p) const
132     {
133     Result result;
134 akalinow 1.5
135 konec 1.1 const Pattern::DataType & detdigi = p;
136     for (Pattern::DataType::const_iterator is = detdigi.begin(); is != detdigi.end(); ++is) {
137     uint32_t rawId = is->first;
138     DetId detId(rawId);
139     if (detId.det() != DetId::Muon) std::cout << "PROBLEM ;;;"<<std::endl;
140     if (detId.subdetId() == MuonSubdetId::RPC) {
141     RPCDigiSpec digi(rawId, is->second);
142     DetFreq::const_iterator idm = posRpc.find(rawId);
143     if (idm != posRpc.end() ) {
144 konec 1.3 double f = whereInDistribution(digi.halfStrip(), idm->second);
145     result.posRpcResult.push_back( std::make_pair(rawId, f) );
146     RPCDetId rpc(rawId);
147     if(rpc.station()==1) result.hasStation1 = true;
148     if(rpc.station()==2) result.hasStation2 = true;
149 konec 1.1 }
150     } else if (detId.subdetId() == MuonSubdetId::DT) {
151     DTphDigiSpec digi(rawId, is->second);
152     DetFreq::const_iterator idm = posDt.find(rawId);
153     if (idm != posDt.end() ) {
154     double f = whereInDistribution(digi.phi(), idm->second);
155     result.posDtResult.push_back( std::make_pair(rawId, f) );
156 konec 1.3 DTChamberId dt(rawId);
157     if(dt.station()==1) result.hasStation1 = true;
158     if(dt.station()==2) result.hasStation2 = true;
159 konec 1.1 }
160     idm = bendingDt.find(rawId);
161     if (idm != bendingDt.end() ) {
162     double f = whereInDistribution(digi.phiB(), idm->second);
163     result.benDtResult.push_back( std::make_pair(rawId, f) );
164     }
165     } else if (detId.subdetId() == MuonSubdetId::CSC) {
166     CSCDigiSpec digi(rawId, is->second);
167     DetFreq::const_iterator idm = posCsc.find(rawId);
168     if (idm != posCsc.end() ) {
169     double f = whereInDistribution(digi.strip(), idm->second);
170     result.posCscResult.push_back( std::make_pair(rawId, f) );
171 konec 1.3 CSCDetId csc(rawId);
172     if (csc.station()==1) result.hasStation1 = true;
173     if (csc.station()==2) result.hasStation1 = true;
174 konec 1.1 }
175     idm = bendingCsc.find(rawId);
176     if (idm != bendingCsc.end() ) {
177     double f = whereInDistribution(digi.pattern(), idm->second);
178     result.benCscResult.push_back( std::make_pair(rawId, f) );
179     }
180     }
181     }
182     return result;
183     }
184    
185     double GoldenPattern::whereInDistribution( int obj, const GoldenPattern::MFreq & m) const
186     {
187     double sum_before = 0;
188     double sum_after = 0;
189     double sum_obj = 0;
190     for (MFreq::const_iterator im = m.begin(); im != m.end(); im++) {
191     if (im->first < obj) sum_before+= im->second;
192     if (im->first == obj) sum_obj = im->second;
193     if (im->first > obj) sum_after += im->second;
194     }
195     double sum = std::max(1.,sum_before+sum_after+sum_obj );
196     return (sum_before+sum_obj/2.)/sum;
197     }
198    
199     void GoldenPattern::purge()
200     {
201 akalinow 1.5
202 konec 1.1 for (DetFreq::iterator idf=posRpc.begin(); idf != posRpc.end(); idf++) {
203     MFreq & mfreq = idf->second;
204     MFreq::iterator imf = mfreq.begin();
205     while (imf != mfreq.end() ) {
206     bool remove = false;
207     int pos = imf->first;
208     unsigned int bef2 = (mfreq.find(pos-2) != mfreq.end()) ? mfreq[pos-2] : 0;
209     unsigned int bef1 = (mfreq.find(pos-1) != mfreq.end()) ? mfreq[pos-1] : 0;
210     unsigned int aft1 = (mfreq.find(pos+1) != mfreq.end()) ? mfreq[pos+1] : 0;
211     unsigned int aft2 = (mfreq.find(pos+2) != mfreq.end()) ? mfreq[pos+2] : 0;
212     unsigned int aft3 = (mfreq.find(pos+3) != mfreq.end()) ? mfreq[pos+3] : 0;
213     if (mfreq[pos]==1 && bef1==0 && aft1==0) remove = true;
214     if (mfreq[pos]==1 && aft1==1 && aft2==0 && aft3==0 && bef1==0 && bef2==0) remove = true;
215     if (mfreq[pos]==2 && aft1==0 && aft2==0 && bef1==0 && bef2==0) remove = true;
216 akalinow 1.5 //if(remove) std::cout<<"Cleaning pattern: "<<*this<<std::endl;
217 konec 1.1 if (remove) { mfreq.erase(imf++); } else { ++imf; }
218     }
219     }
220     DetFreq::iterator idf=posRpc.begin();
221     while (idf != posRpc.end()) if (idf->second.size()==0) posRpc.erase(idf++); else ++idf;
222    
223 akalinow 1.5 }
224 konec 1.1
225     std::ostream & operator << (std::ostream &out, const GoldenPattern & o) {
226     out <<"GoldenPattern"<< o.theKey <<std::endl;
227     // RPC
228     for (GoldenPattern::DetFreq::const_iterator im = o.posRpc.begin(); im != o.posRpc.end(); im++) {
229 akalinow 1.5 out <<"POSRPC Det: "<< im->first<<" Value: ";
230 konec 1.1 for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
231     out << std::endl;
232     }
233 akalinow 1.5 // Csc pos
234     for (GoldenPattern::DetFreq::const_iterator im = o.posCsc.begin(); im != o.posCsc.end(); im++) {
235     out <<"POSCSC Det: "<< im->first<<" Value: ";
236 konec 1.1 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 akalinow 1.5 // Csc bending
240     for (GoldenPattern::DetFreq::const_iterator im = o.bendingCsc.begin(); im != o.bendingCsc.end(); im++) {
241     out <<"BENCSC Det: "<< im->first<<" Value: ";
242 konec 1.1 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 akalinow 1.5 // DT pos
246     for (GoldenPattern::DetFreq::const_iterator im = o.posDt.begin(); im != o.posDt.end(); im++) {
247     out <<"POSDT Det: "<< im->first<<" Value: ";
248 konec 1.1 for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
249     out << std::endl;
250     }
251 akalinow 1.5 // DT bending
252     for (GoldenPattern::DetFreq::const_iterator im = o.bendingDt.begin(); im != o.bendingDt.end(); im++) {
253     out <<"BENDT Det: "<< im->first<<" Value: ";
254 konec 1.1 for (GoldenPattern::MFreq::const_iterator it = im->second.begin(); it != im->second.end(); it++) { out << it->first<<":"<<it->second<<", "; }
255     out << std::endl;
256     }
257    
258     return out;
259     }
260