Skip to content

Commit 974b39f

Browse files
committed
First working version of filter, with couts
1 parent 3491457 commit 974b39f

File tree

2 files changed

+135
-50
lines changed

2 files changed

+135
-50
lines changed

L1Trigger/DTTriggerPhase2/interface/MPThetaMatching.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class MPThetaMatching : public MPFilter {
3030
const edm::EventSetup &iEventSetup,
3131
std::vector<cmsdt::metaPrimitive> &allMPaths,
3232
std::vector<cmsdt::metaPrimitive> &inMPaths,
33-
std::vector<cmsdt::metaPrimitive> &outMPaths) override{};
33+
std::vector<cmsdt::metaPrimitive> &outMPaths) override {};
3434
void run(edm::Event &iEvent,
3535
const edm::EventSetup &iEventSetup,
3636
MuonPathPtrs &inMPath,
37-
MuonPathPtrs &outMPath) override{};
37+
MuonPathPtrs &outMPath) override {};
3838

3939
void finish() override;
4040

@@ -52,18 +52,20 @@ class MPThetaMatching : public MPFilter {
5252
private:
5353
// Private methods
5454
std::vector<cmsdt::metaPrimitive> filter(std::vector<cmsdt::metaPrimitive> inMPs,
55-
int th_option,
56-
int th_quality,
55+
// int th_option,
56+
// int th_quality,
5757
double shift_back);
5858

5959
bool isThereThetaMPInChamber(int sector, int wheel, int station, std::vector<cmsdt::metaPrimitive> thetaMPs);
6060
std::vector<cmsdt::metaPrimitive> getBestThetaMPInChamber(std::vector<cmsdt::metaPrimitive> thetaMPs);
6161

6262
// Function to compare pairs based on the float value, ascending order
63-
static bool compare(const std::pair<cmsdt::metaPrimitive, float> &a,
64-
const std::pair<cmsdt::metaPrimitive, float> &b) {
65-
return a.second < b.second;
63+
static bool comparePairs(const std::tuple<cmsdt::metaPrimitive, cmsdt::metaPrimitive, float> &a,
64+
const std::tuple<cmsdt::metaPrimitive, cmsdt::metaPrimitive, float> &b) {
65+
return std::get<2>(a) < std::get<2>(b);
6666
};
67+
void orderAndSave(std::vector<std::tuple<cmsdt::metaPrimitive, cmsdt::metaPrimitive, float>> deltaTimePosPhiCands,
68+
std::vector<cmsdt::metaPrimitive> *outMPaths, std::vector<cmsdt::metaPrimitive> *savedThetas);
6769

6870
// Private attributes
6971
const bool debug_;

L1Trigger/DTTriggerPhase2/src/MPThetaMatching.cc

Lines changed: 126 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@ using namespace edm;
66
using namespace std;
77
using namespace cmsdt;
88

9+
namespace {
10+
struct {
11+
bool operator()(const metaPrimitive &mp1, const metaPrimitive &mp2) const {
12+
DTChamberId chId1(mp1.rawId);
13+
DTSuperLayerId slId1(mp1.rawId);
14+
15+
int sector1 = chId1.sector();
16+
int wheel1 = chId1.wheel();
17+
int station1 = chId1.station();
18+
19+
DTChamberId chId2(mp2.rawId);
20+
DTSuperLayerId slId2(mp2.rawId);
21+
22+
int sector2 = chId2.sector();
23+
int wheel2 = chId2.wheel();
24+
int station2 = chId2.station();
25+
26+
// First, compare by chamber
27+
if (sector1 != sector2)
28+
return sector1 < sector2;
29+
if (wheel1 != wheel2)
30+
return wheel1 < wheel2;
31+
if (station1 != station2)
32+
return station1 < station2;
33+
34+
// If they are in the same category, sort by the value (4th index)
35+
return mp1.quality > mp2.quality;
36+
}
37+
} const compareMPs;
38+
} //namespace
39+
940
// ============================================================================
1041
// Constructors and destructor
1142
// ============================================================================
@@ -36,22 +67,19 @@ void MPThetaMatching::run(edm::Event &iEvent,
3667
else if (scenario_ == SLICE_TEST)
3768
shift_back = 400;
3869

39-
auto filteredMPs = filter(inMPaths, th_option_, th_quality_, shift_back);
70+
auto filteredMPs = filter(inMPaths, shift_back);
4071
for (auto &mp : filteredMPs)
4172
outMPaths.push_back(mp);
4273

43-
cout<<"Before filter #TPs:" << inMPaths.size()<<" after: "<<outMPaths.size()<<endl;
44-
74+
//if(doLog) cout << "Before filter #TPs:" << inMPaths.size() << " after: " << outMPaths.size() << endl;
4575
}
4676

47-
void MPThetaMatching::finish(){};
77+
void MPThetaMatching::finish() {};
4878

4979
///////////////////////////
5080
/// OTHER METHODS
5181

5282
std::vector<metaPrimitive> MPThetaMatching::filter(std::vector<metaPrimitive> inMPs,
53-
int th_option,
54-
int th_quality,
5583
double shift_back) {
5684
std::vector<metaPrimitive> outMPs;
5785
std::vector<metaPrimitive> thetaMPs;
@@ -67,11 +95,15 @@ std::vector<metaPrimitive> MPThetaMatching::filter(std::vector<metaPrimitive> in
6795
else
6896
phiMPs.push_back(mp);
6997
}
98+
bool doLog=false;
99+
if(thetaMPs.size()>0 || phiMPs.size()>0) {cout << "Before #TPs Theta: " << thetaMPs.size() << " Phi: " << phiMPs.size() << endl;
100+
doLog=true;}
70101

71-
cout<<"Before #TPs Theta: "<<thetaMPs.size()<<" Phi: " <<phiMPs.size()<<endl;
72-
102+
//Order MPs by quality
103+
std::sort(thetaMPs.begin(), thetaMPs.end(), compareMPs);
73104
//Use only best quality theta MP in chamber
74105
thetaMPs = getBestThetaMPInChamber(thetaMPs);
106+
if(doLog) cout << "Filtered #TPs Theta: " << thetaMPs.size() << endl;
75107

76108
// Loop on phi, save those at station without Theta MPs
77109
for (auto &mp : phiMPs) {
@@ -91,12 +123,20 @@ std::vector<metaPrimitive> MPThetaMatching::filter(std::vector<metaPrimitive> in
91123
outMPs.push_back(mp); // No theta MPs in chamber to match, save MP
92124
continue;
93125
}
126+
127+
if ((mp.quality > th_quality_))
128+
outMPs.push_back(mp); //don't do theta matching for q > X, save
94129
}
95130

96-
// Loop on theta
97-
for (auto &mp1 : thetaMPs) {
98-
std::vector<std::pair<metaPrimitive, float>> deltaTimePosPhiCands;
131+
// Loop on theta (already ordered)
132+
int oldSector = 0;
133+
int oldStation = 0;
134+
int oldWheel = 0;
135+
std::vector<std::tuple<metaPrimitive, metaPrimitive, float>>
136+
deltaTimePosPhiCands; //thetaMP, phiMP, difference in TimePosition
137+
std::vector<metaPrimitive> savedThetaMPs;
99138

139+
for (auto &mp1 : thetaMPs) {
100140
DTChamberId chId(mp1.rawId);
101141
DTSuperLayerId slId(mp1.rawId);
102142

@@ -109,14 +149,24 @@ std::vector<metaPrimitive> MPThetaMatching::filter(std::vector<metaPrimitive> in
109149
continue;
110150
}
111151

152+
if (sector != oldSector || wheel != oldWheel || station != oldStation) { //new chamber
153+
if (deltaTimePosPhiCands.size() > 0) orderAndSave(deltaTimePosPhiCands, &outMPs, &savedThetaMPs);
154+
155+
deltaTimePosPhiCands.clear();
156+
oldSector = sector;
157+
oldWheel = wheel;
158+
oldStation = station;
159+
}
160+
161+
112162
// float t0 = (mp1.t0 - shift_back * LHC_CLK_FREQ) * ((float) TIME_TO_TDC_COUNTS / (float) LHC_CLK_FREQ);
113163
float t0 = ((int)round(mp1.t0 / (float)LHC_CLK_FREQ)) - shift_back;
114164
float posRefZ = zFE[wheel + 2];
115-
//cout << "wh st se: " << wheel << " " << station << " " << sector << endl;
165+
if(doLog) cout << "Theta wh se st q t0: " << wheel << " " << sector << " " << station << " " << mp1.quality<< " " << mp1.t0 << endl;
116166

117167
if (wheel == 0 && (sector == 1 || sector == 4 || sector == 5 || sector == 8 || sector == 9 || sector == 12))
118168
posRefZ = -posRefZ;
119-
float posZ = abs(mp1.phi); //???
169+
float posZ = abs(mp1.phi);
120170
//cout << "t0: " << t0 << endl;
121171
//cout << "posZ: " << posZ << endl;
122172

@@ -128,16 +178,17 @@ std::vector<metaPrimitive> MPThetaMatching::filter(std::vector<metaPrimitive> in
128178
int sector2 = chId2.sector();
129179
int wheel2 = chId2.wheel();
130180
int station2 = chId2.station();
181+
if(doLog) cout << "Phi wheel: " << wheel2<< " sector "<< sector2 << " station: " << station2 << " q: " << mp2.quality
182+
<< " t0: " << mp2.t0 << endl;
183+
131184
if (station2 == 4)
132185
continue;
133186

134-
if (station2 != station || sector2 != sector || wheel2 != wheel)
187+
if ((mp2.quality > th_quality_))
135188
continue;
136189

137-
if ((mp2.quality > th_quality)) {
138-
outMPs.push_back(mp2); //don't do theta matching for q > X, save
190+
if (station2 != station || sector2 != sector || wheel2 != wheel)
139191
continue;
140-
}
141192

142193
// float t02 = (mp2.t0 - shift_back * LHC_CLK_FREQ) * ((float) TIME_TO_TDC_COUNTS / (float) LHC_CLK_FREQ);
143194
float t02 = ((int)round(mp2.t0 / (float)LHC_CLK_FREQ)) - shift_back;
@@ -154,32 +205,31 @@ std::vector<metaPrimitive> MPThetaMatching::filter(std::vector<metaPrimitive> in
154205
else if (wheel < 0)
155206
LR = pow(-1, -wheel + sector);
156207
//cout << "wh st se: " << wheel << " " << station << " " << sector << " LR: " << LR << endl;
208+
157209
float posRefX = LR * xFE[station - 1];
158210
float ttheta = t0 - (mp2.x / 1000 - posRefX) / vwire;
159211
//cout << "posRefX: " << posRefX << " X: " << mp2.x / 1000 << endl;
160212
//cout << "ttheta: " << ttheta << endl;
161-
deltaTimePosPhiCands.push_back({mp2, abs(tphi - ttheta)});
162213

214+
deltaTimePosPhiCands.push_back({mp1, mp2, abs(tphi - ttheta)});
215+
//cout << "deltaTimePosPhiCands: " << deltaTimePosPhiCands.size() << endl;
163216
} //loop in phis
164217

165-
//reorder deltaTimePosPhiCands according to tphi-ttheta distance
166-
std::sort(deltaTimePosPhiCands.begin(), deltaTimePosPhiCands.end(), compare);
167-
int count = 0;
168-
for (const std::pair<metaPrimitive, float> &p : deltaTimePosPhiCands) { //save up to nth nearest Phi candidate
169-
cout << "Count: " << count << " abs(tphi-ttheta): " << p.second << endl;
170-
if (count < th_option || th_option == 0)
171-
outMPs.push_back(p.first);
172-
else
173-
break;
174-
count++;
175-
}
218+
if (deltaTimePosPhiCands.size() == 0)
219+
{outMPs.push_back(mp1); //save ThetaMP when there is no phi TPs or if they are High qualityi (>th_quality)
220+
savedThetaMPs.push_back(mp1);}
176221
} // loop in thetas
177222

223+
if(deltaTimePosPhiCands.size()>0) orderAndSave(deltaTimePosPhiCands, &outMPs, &savedThetaMPs); //do once more for last theta TP in loop
224+
225+
178226
//finally add Theta TPs too
179-
for (auto &mp : thetaMPs)
180-
outMPs.push_back(mp);
227+
//for (auto &mp : thetaMPs)
228+
// outMPs.push_back(mp);
181229

182-
cout<<"After #TPs Theta: "<<thetaMPs.size()<<" Phi: " <<outMPs.size()-thetaMPs.size()<<endl;
230+
if(doLog) cout << "After #TPs Theta: " << savedThetaMPs.size() << " Phi: " << outMPs.size() - savedThetaMPs.size() << endl;
231+
if(inMPs.size()>outMPs.size()) cout<<" ########### TP size reduced from: " <<inMPs.size()<<" to: "<< outMPs.size()<<endl;
232+
else if(outMPs.size()>inMPs.size()) cout<<" $$$$$$$$$$$$$ TP size enlarged from: " <<inMPs.size()<<" to: "<< outMPs.size()<<endl;
183233
return outMPs;
184234
};
185235

@@ -206,32 +256,65 @@ std::vector<metaPrimitive> MPThetaMatching::getBestThetaMPInChamber(std::vector<
206256
DTChamberId chId1(mp1.rawId);
207257
DTSuperLayerId slId1(mp1.rawId);
208258

259+
//if there are more than 1 theta TPs in chamber, use and save only the one with highest quality
209260
int sector1 = chId1.sector();
210261
int wheel1 = chId1.wheel();
211262
int station1 = chId1.station();
212-
263+
cout << "Theta wheel: " << wheel1 << " sector " << sector1 << " station1: " << station1 << " q: " << mp1.quality
264+
<< " t0: " << mp1.t0 << endl;
213265
// Theta TPs (SL2) can be only q=1 (3hits) or q=3 (4 hits)
214-
if(mp1.quality>1) { bestThetaMPs.push_back(mp1); continue;}
266+
if (mp1.quality > 1) {
267+
bestThetaMPs.push_back(mp1);
268+
continue;
269+
}
215270

216-
// if q=1 and there is no q=3, save it
217-
for (const auto &mp2: thetaMPs) {
271+
int nTPs = 0;
272+
bool saved = false;
273+
// if q=1
274+
for (const auto &mp2 : thetaMPs) {
218275
DTChamberId chId2(mp2.rawId);
219276
DTSuperLayerId slId2(mp2.rawId);
220277

221278
int sector2 = chId2.sector();
222279
int wheel2 = chId2.wheel();
223280
int station2 = chId2.station();
224-
225-
//if there are more than 1 theta TPs in chamber, use and save only the one with highest quality
281+
226282
if (sector1 == sector2 && wheel1 == wheel2 && station1 == station2) {
227-
if (mp1.quality > mp2.quality) {
228-
continue; //there is a q=3 and it was alreacy saved
229-
} else if (mp2.quality > mp1.quality) { //not likely case unless topological TPs
230-
bestThetaMPs.push_back(mp2);
283+
if (mp2.quality > mp1.quality) {
284+
saved = true;
285+
break; //there is a q=3 and it was already saved
286+
} else if (mp2.quality == mp1.quality && mp2.t0 != mp1.t0) {
287+
saved = true;
288+
bestThetaMPs.push_back(mp1);
289+
break; //if there are more than 1 with same q=1, save both
231290
}
232-
else bestThetaMPs.push_back(mp2); //if there are more than 1 with same q=1, save both
291+
nTPs++;
233292
}
234293
}
294+
if (nTPs == 1 && !saved)
295+
bestThetaMPs.push_back(mp1); //only one Theta TP in chamber and it is q=1
235296
}
236297
return bestThetaMPs;
237298
};
299+
300+
void MPThetaMatching::orderAndSave(std::vector<std::tuple<metaPrimitive, metaPrimitive, float>> deltaTimePosPhiCands,
301+
std::vector<metaPrimitive>* outMPs, std::vector<metaPrimitive>* savedThetaMPs)
302+
{
303+
//reorder deltaTimePosPhiCands according to tphi-ttheta distance
304+
std::sort(deltaTimePosPhiCands.begin(), deltaTimePosPhiCands.end(), comparePairs);
305+
int count = 0;
306+
for (const std::tuple<metaPrimitive, metaPrimitive, float> &p :
307+
deltaTimePosPhiCands) { //save up to nth nearest Theta-Phi pair candidate
308+
cout << "Count: " << count << " abs(tphi-ttheta): " << std::get<2>(p) << " qTheta: "<< std::get<0>(p).quality << " qPhi: "<<std::get<1>(p).quality<<endl;
309+
if (count < th_option_ || th_option_ == 0) {
310+
outMPs->push_back(std::get<1>(p)); //add PhiMP
311+
outMPs->push_back(std::get<0>(p)); //add ThetaMP
312+
savedThetaMPs->push_back(std::get<0>(p)); //for accounting
313+
} else
314+
break;
315+
316+
count++;
317+
}
318+
}
319+
320+

0 commit comments

Comments
 (0)