ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/L1RpcTriggerAnalysis/src/PatternManager.cc
Revision: 1.10
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.9: +16 -12 lines
Log Message:
* initial cleanup by AK

File Contents

# Content
1 #include "UserCode/L1RpcTriggerAnalysis/interface/PatternManager.h"
2
3 #include "UserCode/L1RpcTriggerAnalysis/interface/EventObj.h"
4 #include "UserCode/L1RpcTriggerAnalysis/interface/TrackObj.h"
5 #include "UserCode/L1RpcTriggerAnalysis/interface/HitSpecObj.h"
6
7 #include "UserCode/L1RpcTriggerAnalysis/interface/RPCDetIdUtil.h"
8 #include "UserCode/L1RpcTriggerAnalysis/interface/DTphDigiSpec.h"
9 #include "UserCode/L1RpcTriggerAnalysis/interface/CSCDigiSpec.h"
10 #include "UserCode/L1RpcTriggerAnalysis/interface/RPCDigiSpec.h"
11 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
12
13 #include "UserCode/L1RpcTriggerAnalysis/interface/Pattern.h"
14 #include "UserCode/L1RpcTriggerAnalysis/interface/GoldenPattern.h"
15
16 #include "TFile.h"
17 #include "TTree.h"
18
19 namespace {
20 typedef struct {
21 UInt_t key_det;
22 unsigned int key_pt;
23 unsigned int key_phi;
24 int key_ch;
25 unsigned int pat_Case;
26 UInt_t patDet;
27 int posOrBend;
28 unsigned int freq;
29 } ENTRY;
30 }
31
32 PatternManager::PatternManager(const edm::ParameterSet &cfg)
33 : theConfig(cfg), theEvForPatCounter(0), theEvUsePatCounter(0)
34
35 {}
36
37 PatternManager::~PatternManager()
38 {
39 std::cout <<" Events checked for pattenrs: " << theEvForPatCounter << std::endl;
40 std::cout <<" Events used for pattenrs: " << theEvUsePatCounter << std::endl;
41 std::cout <<" Size of GoldenPatterns: " << theGPs.size() << std::endl;
42 if (theConfig.getUntrackedParameter<bool>("dump",false)) {
43 for (std::map< GoldenPattern::Key, GoldenPattern>::const_iterator
44 im = theGPs.begin(); im!= theGPs.end(); ++im) {
45 std::cout <<" GP: "<< (*im).second << std::endl;
46 }
47 }
48
49 }
50
51 void PatternManager::run(const EventObj* ev, const TrackObj * simu, const HitSpecObj * hitSpec, const VDigiSpec & vDigi)
52 {
53
54
55 if (!hitSpec) return;
56 if (hitSpec->rawId() == 0 ) return;
57 double phiref = hitSpec->position().phi();
58 double ptref = simu->pt();
59 int chargeref = simu->charge();
60 unsigned int detref = hitSpec->rawId();
61 /*
62 if (detref != 637602109 && detref != 637634877 &&
63 detref != 637599914 && detref != 637632682 ) return;
64
65 bool precisePos = ( fabs(hitSpec->position().phi()-1.025) < 0.001);
66 if (!precisePos) return;
67 if ( simu->pt() < 16.) return;
68 */
69
70
71 GoldenPattern::Key key( detref, ptref, chargeref, phiref);
72
73 Pattern pattern;
74 theEvForPatCounter++;
75 for (VDigiSpec::const_iterator is= vDigi.begin(); is!=vDigi.end(); is++) {
76 bool isOK = pattern.add(*is);
77 if (!isOK) return;
78 }
79
80 if (pattern.size() == 0) return;
81 theEvUsePatCounter++;
82
83 if (theGPs.find(key)==theGPs.end()) theGPs[key]=GoldenPattern(key);
84 theGPs[key].add(pattern);
85
86 }
87
88 L1Obj PatternManager::check(const EventObj* ev, const TrackObj * simu, const HitSpecObj * hitSpec, const VDigiSpec & vDigi)
89 {
90 L1Obj candidate;
91
92 if (!hitSpec) return candidate;
93 if (hitSpec->rawId() == 0 ) return candidate;
94 double phiref = hitSpec->position().phi();
95 double ptref = simu->pt();
96 int chargeref = simu->charge();
97 unsigned int detref = hitSpec->rawId();
98 /*
99 if (detref != 637602109 && detref != 637634877 &&
100 detref != 637599914 && detref != 637632682 ) return candidate;
101
102 bool precisePos = ( fabs(hitSpec->position().phi()-1.025) < 0.001);
103 if (!precisePos) return candidate;
104 if ( simu->pt() < 26. || simu->pt() > 27. ) return candidate;
105
106 */
107 // std::cout <<" ------------------ EVENT: "<<*ev <<" number of digis: "<<vDigi.size()<< std::endl;
108 std::vector<Pattern> vpattern(1);
109 theEvForPatCounter++;
110 static bool skipRpcData = theConfig.getUntrackedParameter<bool>("skipRpcData", false);
111 static bool skipDtCscData = theConfig.getUntrackedParameter<bool>("skipDtCscData",false);
112 for (VDigiSpec::const_iterator is= vDigi.begin(); is!=vDigi.end(); is++) {
113 DetId detId( is->first);
114 if (skipRpcData && detId.subdetId()==MuonSubdetId::RPC) continue;
115 if (skipDtCscData && (detId.subdetId()==MuonSubdetId::DT || detId.subdetId()==MuonSubdetId::CSC) ) continue;
116 //std::cout << "adding------- "<< is-vDigi.begin()+1 <<" digi det: "<<is->first<<"(rpc:"<<(detId.subdetId()==MuonSubdetId::RPC)<<") data: "<<is->second<< std::endl;
117 Pattern::add(vpattern,*is);
118 //std::cout <<" after vpattern has size: "<<vpattern.size() << std::endl;
119 if (vpattern.size() > 100) break;
120 }
121 if (vpattern[0].size() == 0) return candidate;
122 //std::cout <<" ------------------ END EVENT, NOW COMPARE, has #patterns: "<<vpattern.size()<<" vpattern[0].size="<<vpattern[0].size() << std::endl;
123
124 GoldenPattern::Key thisKey(detref, ptref, chargeref, phiref );
125 //std::cout << thisKey << std::endl;
126
127 GoldenPattern::Result bestMatching;
128 GoldenPattern::Key bestKey;
129 for (auto ip=vpattern.begin(); ip!= vpattern.end();++ip) {
130 const Pattern & pattern = *ip;
131 //std::cout << " HAS PATTERN "<<pattern << std::endl;
132 for (auto igps = theGPs.begin(); igps != theGPs.end(); ++igps) {
133 // if (!(thisKey==igps->first)) continue;
134 GoldenPattern & gp = igps->second;
135 GoldenPattern::Result result = gp.compare(pattern);
136 // if (!result.hasRpcDet(637602109) && !result.hasRpcDet(637634877) && !result.hasRpcDet(637599914) && !result.hasRpcDet(637632682)) continue;
137 // if (!result.hasRpcDet(igps->first.theDet)) continue;
138 // if (result.nMatchedTot() < 5 )continue;
139 // if (!result) continue;
140 //std::cout <<"PATT KEY: "<<igps->first<<" "<<result ; //<<std::endl;
141 if (bestMatching < result) {
142 bestMatching = result;
143 bestKey = igps->first;
144 //std::cout <<" ----"<<" pt: "<< bestKey.ptValue()<<std::endl;
145 }
146 //else std::cout <<std::endl;
147 }
148 }
149
150 //std::cout <<" ------------------ END COMPARE: " << std::endl;
151 //std::cout <<"BEST KEY: "<<bestKey<<" "<< bestMatching<<std::cout <<" ######"<<" pt: "<< bestKey.ptValue()<<std::endl;
152 // abort();
153 if (bestMatching) {
154 candidate.pt = bestKey.ptValue();
155 candidate.eta = 1.;
156 candidate.phi = bestKey.phiValue();
157 candidate.q = bestMatching.nMatchedTot();
158 candidate.type = L1Obj::OTF;
159 }
160 return candidate;
161
162 }
163
164
165 void PatternManager::beginJob()
166 {
167 if ( !theConfig.exists("patternInpFile") ) return;
168 std::string patternInpFileName = theConfig.getParameter<std::string>("patternInpFile");
169 TFile patternInpFile( patternInpFileName.c_str());
170 TTree * tree = (TTree*) patternInpFile.Get("FlatPatterns");
171 static ENTRY entry;
172 tree->SetBranchAddress("entry",&entry);
173 Int_t nentries = (Int_t) tree->GetEntries();
174 for (Int_t i=0; i<nentries; i++) {
175 tree->GetEntry(i);
176 GoldenPattern::Key key;
177 key.theDet = entry.key_det;
178 key.thePtCode = entry.key_pt;
179 key.thePhiCode = entry.key_phi;
180 key.theCharge = entry.key_ch;
181 GoldenPattern::PosBenCase pat_Case = static_cast<GoldenPattern::PosBenCase>(entry.pat_Case);
182 if (theGPs.find(key)==theGPs.end()) theGPs[key]=GoldenPattern(key);
183 theGPs[key].add(pat_Case, entry.patDet, entry.posOrBend, entry.freq);
184 }
185 delete tree;
186 patternInpFile.Close();
187 }
188
189 void PatternManager::endJob()
190 {
191 for (std::map< GoldenPattern::Key, GoldenPattern>::iterator igps = theGPs.begin(); igps != theGPs.end(); igps++) {
192 GoldenPattern & gp = igps->second;
193 gp.purge();
194 }
195
196 if ( !theConfig.exists("patternOutFile") ) return;
197 std::string patternOutFileName = theConfig.getParameter<std::string>("patternOutFile");
198 TFile patternOutFile( patternOutFileName.c_str(),"RECREATE");
199
200 static ENTRY entry;
201 TTree *tree = new TTree("FlatPatterns","FlatPatterns");
202 tree->Branch("entry",&entry,"key_det/i:key_pt/i:key_phi/i:key_ch/I:pat_Case/i:patDet/i:posOrBend/I:freq/i");
203
204 for (std::map< GoldenPattern::Key, GoldenPattern>::const_iterator igps = theGPs.begin(); igps != theGPs.end(); igps++) {
205 const GoldenPattern & gp = igps->second;
206 entry.key_det = gp.theKey.theDet;
207 entry.key_pt = gp.theKey.thePtCode;
208 entry.key_phi = gp.theKey.thePhiCode;
209 entry.key_ch = gp.theKey.theCharge;
210 for (unsigned int iCase =1; iCase <=5; ++iCase) {
211 entry.pat_Case = iCase;
212 const GoldenPattern::DetFreq * detFreq = 0;
213 if (iCase==GoldenPattern::POSRPC) detFreq = &gp.posRpc;
214 if (iCase==GoldenPattern::POSCSC) detFreq = &gp.posCsc;
215 if (iCase==GoldenPattern::BENCSC) detFreq = &gp.bendingCsc;
216 if (iCase==GoldenPattern::POSDT ) detFreq = &gp.posDt;
217 if (iCase==GoldenPattern::BENDT ) detFreq = &gp.bendingDt;
218 for (GoldenPattern::DetFreq::const_iterator idf = detFreq->begin(); idf != detFreq->end(); idf++) {
219 entry.patDet = idf->first;
220 for (GoldenPattern::MFreq::const_iterator imf = idf->second.begin(); imf != idf->second.end(); imf++) {
221 entry.posOrBend = imf->first;
222 entry.freq = imf->second;
223 tree->Fill();
224 }
225 }
226 }
227 }
228 patternOutFile.Write();
229 patternOutFile.Close();
230 // delete tree;
231 }
232