ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/L1RpcTriggerAnalysis/src/PatternManager.cc
Revision: 1.12
Committed: Fri Jun 28 08:31:12 2013 UTC (11 years, 10 months ago) by akalinow
Content type: text/plain
Branch: MAIN
CVS Tags: Artur_28_06_2013
Changes since 1.11: +45 -25 lines
Log Message:
*updates by AK

File Contents

# User Rev Content
1 konec 1.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 konec 1.8 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
12 konec 1.1
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 konec 1.5 std::cout <<" Events used for pattenrs: " << theEvUsePatCounter << std::endl;
41 konec 1.1 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 akalinow 1.12
54     if (theConfig.exists("patternInpFile") &&
55     theConfig.exists("patternOutFile")) return;
56    
57 konec 1.1 if (!hitSpec) return;
58     if (hitSpec->rawId() == 0 ) return;
59 akalinow 1.12
60     int stripRef = -1;
61     for (VDigiSpec::const_iterator is= vDigi.begin(); is!=vDigi.end(); ++is) {
62     if(is->first!=hitSpec->rawId()) continue;
63     RPCDigiSpec digi(is->first, is->second);
64     stripRef = digi.halfStrip();
65     }
66    
67     ///Use only events with a digi in reference station
68     if(stripRef<0) return;
69    
70     //float phiref = hitSpec->position().phi();
71     float phiref = stripRef;
72     float ptref = simu->pt();
73 konec 1.1 int chargeref = simu->charge();
74     unsigned int detref = hitSpec->rawId();
75    
76     GoldenPattern::Key key( detref, ptref, chargeref, phiref);
77 akalinow 1.12 //if(detref!=637602109 || chargeref<0 || phiref!=80) return;
78     //std::cout<<key<<std::endl;
79 konec 1.1
80     Pattern pattern;
81     theEvForPatCounter++;
82 akalinow 1.11 for (VDigiSpec::const_iterator is= vDigi.begin(); is!=vDigi.end(); ++is) {
83 konec 1.1 bool isOK = pattern.add(*is);
84     if (!isOK) return;
85     }
86     if (pattern.size() == 0) return;
87     theEvUsePatCounter++;
88    
89     if (theGPs.find(key)==theGPs.end()) theGPs[key]=GoldenPattern(key);
90     theGPs[key].add(pattern);
91    
92     }
93    
94     L1Obj PatternManager::check(const EventObj* ev, const TrackObj * simu, const HitSpecObj * hitSpec, const VDigiSpec & vDigi)
95     {
96     L1Obj candidate;
97 akalinow 1.12
98     if (theConfig.exists("patternInpFile") &&
99     theConfig.exists("patternOutFile")) return candidate;
100    
101 konec 1.1 if (!hitSpec) return candidate;
102     if (hitSpec->rawId() == 0 ) return candidate;
103 akalinow 1.12 float phiref = hitSpec->position().phi();
104     float ptref = simu->pt();
105 konec 1.1 int chargeref = simu->charge();
106     unsigned int detref = hitSpec->rawId();
107 konec 1.5 /*
108 akalinow 1.11 if (detref != 637602109 && detref != 637634877 &&
109 konec 1.1 detref != 637599914 && detref != 637632682 ) return candidate;
110    
111     bool precisePos = ( fabs(hitSpec->position().phi()-1.025) < 0.001);
112     if (!precisePos) return candidate;
113     if ( simu->pt() < 26. || simu->pt() > 27. ) return candidate;
114    
115 konec 1.2 */
116 konec 1.9 // std::cout <<" ------------------ EVENT: "<<*ev <<" number of digis: "<<vDigi.size()<< std::endl;
117 konec 1.6 std::vector<Pattern> vpattern(1);
118 konec 1.1 theEvForPatCounter++;
119 konec 1.8 static bool skipRpcData = theConfig.getUntrackedParameter<bool>("skipRpcData", false);
120     static bool skipDtCscData = theConfig.getUntrackedParameter<bool>("skipDtCscData",false);
121 akalinow 1.11 for (VDigiSpec::const_iterator is= vDigi.begin(); is!=vDigi.end(); ++is) {
122 konec 1.8 DetId detId( is->first);
123     if (skipRpcData && detId.subdetId()==MuonSubdetId::RPC) continue;
124     if (skipDtCscData && (detId.subdetId()==MuonSubdetId::DT || detId.subdetId()==MuonSubdetId::CSC) ) continue;
125 akalinow 1.11 if(detId.subdetId()==MuonSubdetId::DT && DTphDigiSpec(is->first, is->second).bxNum()!=0) continue; //AK. Correct?
126     // std::cout << "adding------- "<< is-vDigi.begin()+1 <<" digi det: "<<is->first<<"(rpc:"<<(detId.subdetId()==MuonSubdetId::RPC)<<") data: "<<is->second<< std::endl;
127 konec 1.8 Pattern::add(vpattern,*is);
128 akalinow 1.11 // std::cout <<" after vpattern has size: "<<vpattern.size() << std::endl;
129 konec 1.9 if (vpattern.size() > 100) break;
130 konec 1.8 }
131 konec 1.6 if (vpattern[0].size() == 0) return candidate;
132 akalinow 1.10 //std::cout <<" ------------------ END EVENT, NOW COMPARE, has #patterns: "<<vpattern.size()<<" vpattern[0].size="<<vpattern[0].size() << std::endl;
133 konec 1.1
134 konec 1.5 GoldenPattern::Key thisKey(detref, ptref, chargeref, phiref );
135 akalinow 1.12 //std::cout << thisKey << std::endl;
136 konec 1.1
137     GoldenPattern::Result bestMatching;
138     GoldenPattern::Key bestKey;
139 akalinow 1.11 for (auto ip=vpattern.cbegin(); ip!= vpattern.cend();++ip) {
140     const Pattern & pattern = *ip;
141     // std::cout << " HAS PATTERN "<<pattern << std::endl;
142     for (std::map< GoldenPattern::Key, GoldenPattern>::iterator igps = theGPs.begin(); igps != theGPs.end(); ++igps) {
143 akalinow 1.12 //if (!(thisKey==igps->first)) continue;
144 akalinow 1.11 GoldenPattern & gp = igps->second;
145     GoldenPattern::Result result = gp.compare(pattern);
146 akalinow 1.12
147     std::cout <<"PATT KEY: "<<igps->first<<" "<<result<<std::endl;
148 akalinow 1.11 if (bestMatching < result) {
149     bestMatching = result;
150     bestKey = igps->first;
151     // std::cout <<" ----"<<" pt: "<< bestKey.ptValue()<<std::endl;
152     }
153     //else std::cout <<std::endl;
154     }
155 konec 1.6 }
156 konec 1.5
157 akalinow 1.11 // std::cout <<" ------------------ END COMPARE: " << std::endl;
158     // std::cout <<"BEST KEY: "<<bestKey<<" "<< bestMatching<<std::cout <<" ######"<<" pt: "<< bestKey.ptValue()<<std::endl;
159 konec 1.8 // abort();
160 konec 1.1 if (bestMatching) {
161     candidate.pt = bestKey.ptValue();
162 akalinow 1.11 candidate.eta = bestKey.etaValue();
163 konec 1.1 candidate.phi = bestKey.phiValue();
164     candidate.q = bestMatching.nMatchedTot();
165     candidate.type = L1Obj::OTF;
166     }
167     return candidate;
168    
169     }
170    
171    
172     void PatternManager::beginJob()
173     {
174     if ( !theConfig.exists("patternInpFile") ) return;
175     std::string patternInpFileName = theConfig.getParameter<std::string>("patternInpFile");
176     TFile patternInpFile( patternInpFileName.c_str());
177     TTree * tree = (TTree*) patternInpFile.Get("FlatPatterns");
178     static ENTRY entry;
179     tree->SetBranchAddress("entry",&entry);
180     Int_t nentries = (Int_t) tree->GetEntries();
181 akalinow 1.11 for (Int_t i=0; i<nentries; ++i) {
182 konec 1.1 tree->GetEntry(i);
183     GoldenPattern::Key key;
184     key.theDet = entry.key_det;
185     key.thePtCode = entry.key_pt;
186     key.thePhiCode = entry.key_phi;
187     key.theCharge = entry.key_ch;
188 akalinow 1.12
189     /*
190     if(entry.key_det!=637602109 ||
191     entry.key_pt!=7 ||
192     entry.key_phi!=166 ||
193     entry.key_ch!=-1) continue;
194     */
195    
196 konec 1.1 GoldenPattern::PosBenCase pat_Case = static_cast<GoldenPattern::PosBenCase>(entry.pat_Case);
197     if (theGPs.find(key)==theGPs.end()) theGPs[key]=GoldenPattern(key);
198     theGPs[key].add(pat_Case, entry.patDet, entry.posOrBend, entry.freq);
199     }
200     delete tree;
201     patternInpFile.Close();
202     }
203    
204 akalinow 1.11 void PatternManager::endJob(){
205 konec 1.1
206     if ( !theConfig.exists("patternOutFile") ) return;
207     std::string patternOutFileName = theConfig.getParameter<std::string>("patternOutFile");
208     TFile patternOutFile( patternOutFileName.c_str(),"RECREATE");
209 akalinow 1.12
210     if (theConfig.exists("patternInpFile") && theConfig.exists("patternOutFile")){
211     for (auto igps = theGPs.begin(); igps != theGPs.end();) {
212     GoldenPattern & gp = igps->second;
213     if(!gp.purge()) {theGPs.erase(igps++); } else { ++igps; }
214     }
215 akalinow 1.11 }
216 akalinow 1.12
217 konec 1.1 static ENTRY entry;
218     TTree *tree = new TTree("FlatPatterns","FlatPatterns");
219     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");
220    
221 akalinow 1.11 for (std::map< GoldenPattern::Key, GoldenPattern>::const_iterator igps = theGPs.begin(); igps != theGPs.end(); ++igps) {
222 konec 1.1 const GoldenPattern & gp = igps->second;
223     entry.key_det = gp.theKey.theDet;
224     entry.key_pt = gp.theKey.thePtCode;
225     entry.key_phi = gp.theKey.thePhiCode;
226     entry.key_ch = gp.theKey.theCharge;
227 akalinow 1.11
228     for (auto it=gp.PattCore.cbegin();it!=gp.PattCore.cend();++it){
229     entry.pat_Case = it->first;
230     for (GoldenPattern::DetFreq::const_iterator idf = it->second.cbegin(); idf !=it->second.cend(); ++idf) {
231 konec 1.1 entry.patDet = idf->first;
232 akalinow 1.11 for (GoldenPattern::MFreq::const_iterator imf = idf->second.cbegin(); imf != idf->second.cend(); ++imf) {
233 konec 1.1 entry.posOrBend = imf->first;
234     entry.freq = imf->second;
235     tree->Fill();
236     }
237     }
238     }
239     }
240     patternOutFile.Write();
241     patternOutFile.Close();
242     // delete tree;
243     }
244