EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TaggingStudyModule.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TaggingStudyModule.cc
1 #include "TaggingStudyModule.h"
2 
3 #include "TClonesArray.h"
4 
5 #include "AnalysisFunctions.cc"
6 
7 #include <iostream>
8 #include <iomanip>
9 #include <fstream>
10 
11 #include "TreeHandler.h"
12 
14  : Module(data)
15 {
16 
17 }
18 
20 {
21 
22 }
23 
25 {
26  TreeHandler *tree_handler = tree_handler->getInstance();
27 
28  if (tree_handler->getTree() != nullptr) {
29  tree_handler->getTree()->Branch("jet_pt", &_jet_pt, "jet_pt/F");
30  tree_handler->getTree()->Branch("jet_eta", &_jet_eta, "jet_eta/F");
31  tree_handler->getTree()->Branch("jet_flavor", &_jet_flavor, "jet_flavor/F");
32  // tree_handler->getTree()->Branch("jet_tagged", &_jet_tagged, "jet_tagged/F");
33  tree_handler->getTree()->Branch("jet_btag", &_jet_btag, "jet_btag/F");
34  }
35 
36  // create the study variations map
37  study_variations = std::map<std::string, std::map<std::string, int>>();
38 
39  study_variations["all"]["charm"] = 0;
40  study_variations["all"]["light"] = 0;
41  for (auto minTrack : minTrackVar) {
42  for (auto minTrkPt : minTrkPTVar) {
43  for (auto minSignif : minSignifVar) {
44  std::string varName = std::string(Form("MinTrk: %d;TrkPT: %.2f;MinSig: %.2f",minTrack,minTrkPt,minSignif));
45  //if (study_variations.find(varName) == study_variations.end()
46  study_variations[varName]["charm"] = 0;
47  study_variations[varName]["light"] = 0;
48  }
49  }
50  }
51 }
52 
54 {
55  std::vector<std::string> jets{"light", "charm"};
56  // Report the yield for each variation as well as the Punzi Figure of Merit
57 
58  float allLight = float(study_variations["all"]["light"]);
59  float allCharm = float(study_variations["all"]["charm"]);
60 
61  float maxFOM = 0.0;
62  std::string bestVariation = "";
63 
64  std::cout << std::setw(40) << "VARIATION" << std::setw(6) << "LIGHT"
65  << std::setw(6) << "CHARM" << std::setw(8) << "PUNZI" << std::endl;
66 
67 
68  ofstream csvfile;
69  csvfile.open("tagging_study.csv");
70 
71  csvfile << "Variation,Light,Charm" << std::endl;
72 
73  for (auto& [variation, jet] : study_variations) {
74  float nLight = float(study_variations[variation]["light"]);
75  float nCharm = float(study_variations[variation]["charm"]);
76  float PunziFOM = -1;
77  if (allCharm>0)
78  PunziFOM = (nCharm/allCharm)/( 1.5 + TMath::Sqrt(nLight));
79 
80  std::cout << std::setw(40) << variation << std::setw(6) << int(nLight)
81  << std::setw(6) << int(nCharm) << std::setw(8) << Form("%.4f", PunziFOM) << std::endl;
82 
83  csvfile << "\"" << variation << "\"," << int(nLight) << "," << nCharm << std::endl;
84 
85  if (PunziFOM > maxFOM) {
86  maxFOM = PunziFOM;
87  bestVariation = variation;
88  }
89  }
90 
91  std::cout << "======================================================================" << std::endl;
92  std::cout << "Best Variation: " << bestVariation << " (FOM: " << maxFOM << ")" << std::endl;
93 
94  csvfile.close();
95 
96 
97 }
98 bool TaggingStudyModule::execute(std::map<std::string, std::any>* DataStore)
99 {
100  auto data = getData();
101  TreeHandler *tree_handler = tree_handler->getInstance();
102 
103  // If event contains at least 1 jet
104  std::vector<Jet*> all_jets;
105  for (int ijet = 0; ijet < getJets()->GetEntries(); ijet++)
106  {
107  // Take first jet
108  Jet *jet = (Jet*) getJets()->At(ijet);
109  all_jets.push_back(jet);
110  }
111 
112  // MET cut first
113  bool passed = true;
114 
115 
116  // Get the MET object and use it
117  MissingET* MET = nullptr;
118  for (int imet = 0; imet < getMET()->GetEntries(); imet++) {
119  MET = static_cast<MissingET*>(getMET()->At(imet));
120  }
121 
122  if (MET == nullptr) {
123  passed = false;
124  }
125 
126  if (passed == true && MET->MET > 10.0) {
127  passed = true;
128  } else {
129  passed = false;
130  }
131 
132 
133  if (passed == false)
134  return false;
135 
136  std::vector<Jet*> fiducial_jets = SelectorFcn<Jet>(all_jets, [](Jet* j){ return (TMath::Abs(j->Eta) < 3.0 && j->PT > 5.0); });
137 
138  std::vector<Jet*> charmJets = SelectorFcn<Jet>(fiducial_jets, [](Jet* j){ return (j->Flavor == 4); });
139 
140  std::vector<Jet*> lightJets = SelectorFcn<Jet>(fiducial_jets, [](Jet* j){ return (j->Flavor < 4 || j->Flavor == 21); });
141 
142  // Resolution settings
143  // float d0err = 0.020; // mm
144  // float z0err = 0.020; // mm
145 
146 
147  // Clone the tracks and adjust their errors to suit this study
148  auto ModifiedTracks = static_cast<TClonesArray*>(getEFlowTracks()->Clone());
149  for (int i = 0; i < ModifiedTracks->GetEntries(); i++) {
150  auto track = static_cast<Track*>(ModifiedTracks->At(i));
151  // track->ErrorD0 = d0err;
152  // track->ErrorDZ = z0err;
153  }
154 
155 
156  study_variations["all"]["charm"] += charmJets.size();
157  for (auto charmjet : charmJets) {
158  _jet_pt = charmjet->PT;
159  _jet_eta = charmjet->Eta;
160  _jet_flavor = charmjet->Flavor;
161  // _jet_tagged = Tagged_sIP3D(charmjet);
162  _jet_btag = charmjet->BTag;
163  // tree_handler->getTree()->Fill();
164 
165  for (auto minTrack : minTrackVar) {
166  for (auto minTrkPt : minTrkPTVar) {
167  for (auto minSignif : minSignifVar) {
168  std::string varName = std::string(Form("MinTrk: %d;TrkPT: %.2f;MinSig: %.2f",minTrack,minTrkPt,minSignif));
169  if (Tagged_sIP3D(charmjet, *ModifiedTracks, minSignif, minTrkPt, minTrack))
170  study_variations[varName]["charm"] += 1;
171  }
172  }
173  }
174 
175  }
176 
177  study_variations["all"]["light"] += lightJets.size();
178  for (auto lightjet : lightJets) {
179  _jet_pt = lightjet->PT;
180  _jet_eta = lightjet->Eta;
181  _jet_flavor = lightjet->Flavor;
182  // _jet_tagged = Tagged_sIP3D(lightjet);
183  _jet_btag = lightjet->BTag;
184  // tree_handler->getTree()->Fill();
185 
186  for (auto minTrack : minTrackVar) {
187  for (auto minTrkPt : minTrkPTVar) {
188  for (auto minSignif : minSignifVar) {
189  std::string varName = std::string(Form("MinTrk: %d;TrkPT: %.2f;MinSig: %.2f",minTrack,minTrkPt,minSignif));
190  if (Tagged_sIP3D(lightjet, *ModifiedTracks, minSignif, minTrkPt, minTrack))
191  study_variations[varName]["light"] += 1;
192  }
193  }
194  }
195  }
196 
197  if (ModifiedTracks)
198  delete ModifiedTracks;
199 
200  return true;
201 }
202 
203 
204 // bool TaggingStudyModule::Tagged(Jet* jet,
205 // float minSignif, float minPT, int minTracks, float errd0, float errz0)
206 // {
207 // bool tagged = false;
208 
209 // const TLorentzVector &jetMomentum = jet->P4();
210 // float jpx = jetMomentum.Px();
211 // float jpy = jetMomentum.Py();
212 // float jpz = jetMomentum.Pz();
213 
214 // auto jet_constituents = *(getEFlowTracks());
215 
216 // int N_sIPtrack = 0;
217 
218 // for (int iconst = 0; iconst < jet_constituents.GetEntries(); iconst++) {
219 
220 
221 // if (N_sIPtrack >= minTracks)
222 // break;
223 
224 // auto constituent = jet_constituents.At(iconst);
225 
226 // if(constituent == 0) continue;
227 
228 // if (constituent->IsA() == Track::Class()) {
229 // auto track = static_cast<Track*>(constituent);
230 
231 // const TLorentzVector &trkMomentum = track->P4();
232 // float tpt = trkMomentum.Pt();
233 // if(tpt < minPT) continue;
234 
235 // if (trkMomentum.DeltaR(jetMomentum) > 0.5)
236 // continue;
237 
238 // float d0 = TMath::Abs(track->D0);
239 // if (d0 > 3.0)
240 // continue;
241 
242 // float xd = track->Xd;
243 // float yd = track->Yd;
244 // float zd = track->Zd;
245 // float dd0 = errd0;
246 // if (dd0 < 0)
247 // TMath::Abs(track->ErrorD0);
248 // float dz = TMath::Abs(track->DZ);
249 // float ddz = errz0;
250 // if (ddz < 0)
251 // TMath::Abs(track->ErrorDZ);
252 
253 // int sign = (jpx * xd + jpy * yd + jpz * zd > 0.0) ? 1 : -1;
254 // //add transverse and longitudinal significances in quadrature
255 // float sip = sign * TMath::Sqrt(TMath::Power(d0 / dd0, 2) + TMath::Power(dz / ddz, 2));
256 
257 // if(sip > minSignif) N_sIPtrack++;
258 // }
259 
260 
261 
262 
263 // }
264 
265 // tagged = (N_sIPtrack >= minTracks);
266 
267 // return tagged;
268 // }