forked from urasantonio/MFT-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudyMFTPurity.C
282 lines (219 loc) · 9.65 KB
/
StudyMFTPurity.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using MFTCluster = o2::BaseCluster<float>;
using MFTTrack = o2::mft::TrackMFT;
using o2::MCTrack;
using o2::itsmft::CompClusterExt;
std::vector<MFTTrack> mMFTTracks;
std::vector<MFTCluster> mMFTClusters;
std::vector<int> mtrackExtClsIDs;
TH2D *histPhiRecVsPhiGen=0, *histEtaRecVsEtaGen=0, *histPhiVsEtaPurityTrue=0, *histPhiVsEtaPurityRec=0;
enum typeOfTracks {kGen, kRecoTrue, kReco, kTypeOfTracks};
std::vector<string> nameOfTracks = {"Gen","RecTrue","Rec"};//of size kTypeOfTracks
void BookHistos();
//StudyMFTPurity("output.root","./mcarchive/tf10/sgn_10_Kine.root","./mcarchive/tf10/mftclusters.root", "./mcarchive/tf10/mfttracks.root")
std::pair<long long int, int> findEntryWithLargestValue(std::map<long long int, int> sampleMap);
//______________________________________________________________________________
void StudyMFTPurity(const Char_t *ofname = "outputfile_studyTracks.root", const Char_t *kineFileName = "o2sim_Kine.root", const Char_t *clusterFileName = "mftclusters.root", const Char_t *trkFileName = "mfttracks.root")
{
BookHistos();
TFile of(ofname, "RECREATE");//output file
double eta[kTypeOfTracks]={0}, phi[kTypeOfTracks]={0}, pt[kTypeOfTracks]={0};
double zVtx[kTypeOfTracks]={0}, z[kTypeOfTracks-1]={0}, R[kTypeOfTracks-1]={0}; //Of the MCTrack
// Load the file containing MFT tracks
// Load the tree branches "MFTTrack","MFTTrackMCTruth" and "MFTTrackClusIdx"
TFile trkFile(trkFileName);
TTree *trkTree = (TTree*)trkFile.Get("o2sim");
std::vector<o2::mft::TrackMFT> trackVec, *trackVecP = &trackVec;
trkTree->SetBranchAddress("MFTTrack", &trackVecP);
std::vector<o2::MCCompLabel>* trkLabels = nullptr;
if (trkTree->GetBranch("MFTTrackMCTruth"))
{
trkTree->SetBranchAddress("MFTTrackMCTruth", &trkLabels);
}
else
{
printf("No Monte-Carlo information in this file\n");
return;
}
std::vector<int> trackExtClsVec, *trackExtClsVecP = &trackExtClsVec;
trkTree->SetBranchAddress("MFTTrackClusIdx", &trackExtClsVecP);
trkTree->GetEntry(0);//the trkTree has only one Entry
//______________________________________________________________________________
// Cluster pattern dictionary
std::string dictfile = "MFTdictionary.bin";
o2::itsmft::TopologyDictionary dict;
std::ifstream file(dictfile.c_str());
if (file.good()) {
printf("Running with dictionary: %s \n", dictfile.c_str());
dict.readBinaryFile(dictfile);
} else {
printf("Can not run without dictionary !\n");
return;
}
//Load the file containing clusters
//Initialize cluster tree
TFile clusterFile(clusterFileName);
TTree* clsTree = (TTree*)clusterFile.Get("o2sim");
std::vector<CompClusterExt> clsVec, *clsVecP = &clsVec;
clsTree->SetBranchAddress("MFTClusterComp", &clsVecP);
o2::dataformats::MCTruthContainer<o2::MCCompLabel>* clsLabels = nullptr; // This goes to global variables
if (clsTree->GetBranch("MFTClusterMCTruth"))
{
clsTree->SetBranchAddress("MFTClusterMCTruth", &clsLabels);
}
else
{
printf("No Monte-Carlo information in this file\n");
return;
}
clsTree -> GetEntry(0);
// MC tracks file: initializing generated kinematics tree
TFile kineFile(kineFileName);//kineFileName contains the kinematic of generated MCTracks
TTree *kineTree = (TTree*)kineFile.Get("o2sim");
std::vector<o2::MCTrack> mcTrkVec, *mcTrkVecP = &mcTrkVec;
kineTree->SetBranchAddress("MCTrack",&mcTrkVecP);
o2::dataformats::MCEventHeader* eventHeader = nullptr;
kineTree->SetBranchAddress("MCEventHeader.", &eventHeader);
int srcID, trkID, evnID;
bool fake;
Int_t iTrack = 0;
for (auto &track : trackVec)
{
auto ncls = track.getNumberOfPoints();
auto offset = track.getExternalClusterIndexOffset();
std::map<long long int, int> mcLabels;
for (int icls = 0; icls < ncls; ++icls)
{
auto clsEntry = trackExtClsVec[offset + icls];
auto cluster = clsVec[clsEntry];
for (int ilabel = 0; ilabel < (clsLabels->getLabels(clsEntry)).size(); ++ilabel)
{
auto& clsLabel = (clsLabels->getLabels(clsEntry))[ilabel];
clsLabel.get(trkID, evnID, srcID, fake);
if (!clsLabel.isNoise())
{
mcLabels[clsLabel.getRawValue()] += 1;
}
}
}//end of cluster loop 1
std::pair<long long int, int> entryWithMaxValue = findEntryWithLargestValue(mcLabels);
Int_t thisEvnID = -1, thisSrcID = -1, thisTrkID = -1, thisEventIDLabel = -1, nPoints = track.getNumberOfPoints();
bool thisFake=false;
//cout << "Entry with highest value: "<< entryWithMaxValue.first << " = "<< entryWithMaxValue.second << endl;
//printf("Number of points for this track = %d\n", nPoints);
for (int icls = 0; icls < ncls; ++icls)
{
auto clsEntry = trackExtClsVec[offset + icls];
auto cluster = clsVec[clsEntry];
for (int ilabel = 0; ilabel < (clsLabels->getLabels(clsEntry)).size(); ++ilabel)
{
auto& clsLabel = (clsLabels->getLabels(clsEntry))[ilabel];
clsLabel.get(trkID, evnID, srcID, fake);
if ((!clsLabel.isNoise()) && (clsLabel.getRawValue()==entryWithMaxValue.first))
{
if (((Float_t)(mcLabels[clsLabel.getRawValue()]) / (Float_t)(nPoints)) >= 0.8)
{ // Must have at least 80% of its clusters from the same MC Track
thisTrkID = trkID;
thisSrcID = srcID;
thisEvnID = evnID;
thisEventIDLabel = icls;
thisFake = fake;
}
}
}
}//end of cluster loop 2
//after that, if thisTrkID, thisSrcID, thisEvnID are still equal to -1, it means the track isn't considered a true track !
if (thisTrkID==-1)
{
printf("NO This track number %d in the reco track tree is not a true track\n", iTrack);
}
else
{
printf("YES this track number %d in the reco track tree of trkID= %d is true\n", iTrack, thisTrkID);
kineTree->GetEntry(thisEvnID);
MCTrack* thisTrack = &(mcTrkVec)[thisTrkID];
//zVtx[kGen] = eventHeader->GetZ();
//pt[kGen] = thisTrack->GetPt();
eta[kGen] = -1*thisTrack->GetEta();
phi[kGen] = thisTrack->GetPhi();
eta[kRecoTrue]=-1*track.getEta();
if (track.getPhi()>=TMath::Pi()/2)
{
phi[kRecoTrue]=-track.getPhi()+TMath::Pi()/2+2*TMath::Pi();
}
else
{
phi[kRecoTrue]=-track.getPhi()+TMath::Pi()/2;
}
printf("phigen=%f, phirec=%f\n", phi[kGen], phi[kRecoTrue]);
histPhiRecVsPhiGen->Fill(phi[kGen], phi[kRecoTrue]);
histEtaRecVsEtaGen->Fill(eta[kGen], eta[kRecoTrue]);
histPhiVsEtaPurityTrue->Fill(eta[kRecoTrue], phi[kRecoTrue]);
}
eta[kReco]=-1*track.getEta();
if (track.getPhi()>=TMath::Pi()/2)
{
phi[kReco]=-track.getPhi()+TMath::Pi()/2+2*TMath::Pi();
}
else
{
phi[kReco]=-track.getPhi()+TMath::Pi()/2;
}
histPhiVsEtaPurityRec->Fill(eta[kReco], phi[kReco]);
iTrack++;
}
auto cnv_phi = new TCanvas("cnv_phi","",800,800);
histPhiRecVsPhiGen->Draw("colz");
auto cnv_eta = new TCanvas("cnv_eta","",800,800);
histEtaRecVsEtaGen->Draw("colz");
auto cnv_RecoTrue = new TCanvas("cnv_RecoTrue","",800,800);
histPhiVsEtaPurityTrue->Draw("colz");
auto cnv_AllReco = new TCanvas("cnv_AllReco","",800,800);
histPhiVsEtaPurityRec->Draw("colz");
of.cd();
histPhiRecVsPhiGen ->Write();
histEtaRecVsEtaGen ->Write();
histPhiVsEtaPurityTrue ->Write();
histPhiVsEtaPurityRec ->Write();
of.Close();
}//end of StudyMFTPurity()
// Function to find the Entry
// with largest Value in a Map
std::pair<long long int, int> findEntryWithLargestValue(std::map<long long int, int> sampleMap)
{
// Reference variable to help find
// the entry with the highest value
std::pair<long long int, int> entryWithMaxValue = std::make_pair(0, 0);
// Iterate in the map to find the required entry
std::map<long long int, int>::iterator currentEntry;
for (currentEntry = sampleMap.begin(); currentEntry != sampleMap.end(); ++currentEntry)
{
// If this entry's value is more
// than the max value
// Set this entry as the max
if (currentEntry->second > entryWithMaxValue.second)
{
entryWithMaxValue= std::make_pair(currentEntry->first,currentEntry->second);
}
}
return entryWithMaxValue;
}
void BookHistos()
{
//histPhiRecVsPhiGen = new TH2D("histPhiRecVsPhiGen", "Phi Rec Vs Phi Gen of true reco tracks ", 12, -TMath::Pi(), TMath::Pi(), 12, -TMath::Pi(), TMath::Pi());
histPhiRecVsPhiGen = new TH2D("histPhiRecVsPhiGen", "Phi Rec Vs Phi Gen of true reco tracks ", 24, 0., 2*TMath::Pi(), 24, 0., 2*TMath::Pi());
histPhiRecVsPhiGen->SetXTitle((string("#phi of ")+nameOfTracks[kGen]).c_str());
histPhiRecVsPhiGen->SetYTitle((string("#phi of ")+nameOfTracks[kRecoTrue]).c_str());
histPhiRecVsPhiGen->Sumw2();
histEtaRecVsEtaGen = new TH2D("histEtaRecVsEtaGen", "Eta Rec Vs Eta Gen of true reco tracks ", 35, 1.0, 4.5, 35, 1.0, 4.5);
histEtaRecVsEtaGen->SetXTitle((string("#eta of ")+nameOfTracks[kGen]).c_str());
histEtaRecVsEtaGen->SetYTitle((string("#eta of ")+nameOfTracks[kRecoTrue]).c_str());
histEtaRecVsEtaGen->Sumw2();
histPhiVsEtaPurityTrue = new TH2D("histPhiVsEtaPurityTrue", "Phi Vs Eta of true reco tracks ", 35, 1.0, 4.5, 24, 0., 2*TMath::Pi());
histPhiVsEtaPurityTrue->SetXTitle((string("#eta of ")+nameOfTracks[kRecoTrue]).c_str());
histPhiVsEtaPurityTrue->SetYTitle((string("#phi of ")+nameOfTracks[kRecoTrue]).c_str());
histPhiVsEtaPurityTrue->Sumw2();
histPhiVsEtaPurityRec = new TH2D("histPhiVsEtaPurityRec", "Phi Vs Eta of all reco tracks ", 35, 1.0, 4.5, 24, 0., 2*TMath::Pi());
histPhiVsEtaPurityRec->SetXTitle((string("#eta of ")+nameOfTracks[kReco]).c_str());
histPhiVsEtaPurityRec->SetYTitle((string("#phi of ")+nameOfTracks[kReco]).c_str());
histPhiVsEtaPurityRec->Sumw2();
}