11 #include <tracking/trackFindingVXD/sectorMapTools/SectorMapComparer.h>
12 #include <tracking/dataobjects/FullSecID.h>
14 #include <framework/logging/LogConfig.h>
21 #include <TObjArray.h>
34 const std::string& SMFileSecond) : m_SMFileName_first(SMFileFirst),
35 m_SMFileName_second(SMFileSecond)
44 std::string str = std::to_string(l1) +
"-" + std::to_string(l2) +
"-" + std::to_string(l3);
54 std::unordered_map<std::string, uint>& SecIDVals)
56 TObjArray* leafList = t->GetListOfLeaves();
57 for (TObject* o : *leafList) {
59 std::string name = l->GetName();
61 TString classname = l->ClassName();
64 if (classname.EqualTo(
"TLeafI")) {
66 l->SetAddress(&(SecIDVals[name]));
67 }
else if (classname.EqualTo(
"TLeafD")) {
68 filterVals[name] = 0.0;
69 l->SetAddress(&(filterVals[name]));
71 B2WARNING(
"Unsupported TLeaf type: " << l->ClassName() <<
". Will skip this TLeaf.");
86 std::vector<std::string> bNames = {
"innerFullSecID",
"centerFullSecID",
"outerFullSecID"};
87 std::vector<uint> bVals = {0, 0, 0};
89 for (uint i = 0; i < bNames.size(); i++) {
90 TLeaf* l = tree->GetLeaf(bNames[i].c_str());
92 l->SetAddress(&(bVals[i]));
97 for (
long i = 0; i < tree->GetEntries(); i++) {
99 std::string hash =
getHash(bVals[0], bVals[1], bVals[2]);
117 std::unordered_map<std::string, long> indexmap_t_second;
122 std::unordered_map<std::string, double> vals_t_first;
123 std::unordered_map<std::string, uint> ids_t_first;
126 std::unordered_map<std::string, double> vals_t_second;
127 std::unordered_map<std::string, uint> ids_t_second;
132 bool isTriplet = t_first->GetBranch(
"centerFullSecID") !=
nullptr;
135 if (vals_t_second.size() != vals_t_first.size()) {
136 B2WARNING(
"Number of filters stored in the two SectorMaps seem to differ! This is per se not dangerous, but some cuts may be compared to zero.");
140 TObjArray* leafList = t_first->GetListOfLeaves();
141 for (TObject* o : *leafList) {
142 TLeaf* l_first = (TLeaf*)o;
144 TString leafName = l_first->GetName();
145 if (leafName.Contains(
"FullSecID"))
continue;
147 double min = std::min(t_first->GetMinimum(leafName) , t_second->GetMinimum(leafName));
148 double max = std::max(t_first->GetMaximum(leafName) , t_second->GetMaximum(leafName));
149 double range = std::fabs(min - max);
158 B2ERROR(
"ERROR: Histogram for key " << leafName.Data() <<
" already exists");
162 m_histo_map_first[ leafName.Data() ] = TH1F(leafName +
"_first", leafName, Nbins, min, max);
164 m_histo_map_second[ leafName.Data() ] = TH1F(leafName +
"_second", leafName , Nbins, min, max);
166 m_histo_map_diff[ leafName.Data() ] = TH1F(leafName +
"_diff",
"difference " + leafName +
" (SM1 - SM2)", Nbins, -0.2 * range,
171 TString niceName = leafName;
172 niceName.ReplaceAll(
"_min",
"");
173 niceName.ReplaceAll(
"_max",
"");
174 m_histo_map_range_first[ leafName.Data() ] = TH1F(leafName +
"_range_first", niceName +
" range (max - min)", Nbins, 0, range);
176 m_histo_map_range_second[ leafName.Data() ] = TH1F(leafName +
"_range_second", niceName +
" range (max - min)", Nbins, 0, range);
183 for (
long i = 0; i < t_first->GetEntries(); i++) {
184 t_first->GetEntry(i);
186 std::string hash =
getHash(ids_t_first[
"innerFullSecID"], ids_t_first[
"centerFullSecID"], ids_t_first[
"outerFullSecID"]);
195 if (indexmap_t_second.find(hash) == indexmap_t_second.end()) {
197 if (!unmatchedEntries)
continue;
200 t_second->GetEntry(indexmap_t_second[hash]);
201 if (unmatchedEntries)
continue;
210 for (TObject* o : *leafList) {
211 TString leafName = o->GetName();
213 if (leafName.Contains(
"FullSecID"))
continue;
215 if (!unmatchedEntries) {
217 m_histo_map_diff[ leafName.Data() ].Fill(vals_t_first[leafName.Data()] - vals_t_second[leafName.Data()]);
220 if (leafName.Contains(
"_max")) {
221 TString minLeafName = leafName;
222 minLeafName.ReplaceAll(
"_max",
"_min");
223 m_histo_map_range_first[ leafName.Data() ].Fill(vals_t_first[leafName.Data()] - vals_t_first[minLeafName.Data()]);
225 vals_t_second[minLeafName.Data()]);
236 TFile* f = TFile::Open(secmapFileName);
238 B2WARNING(
"File not opened: " << secmapFileName);
248 TString* setupKeyName =
nullptr;
250 if (setupKeyName ==
nullptr) {
251 B2WARNING(
"setupKeyName not found");
255 std::cout <<
"Following setups found for file: " << secmapFileName << std::endl;
256 std::cout <<
"================================ " << std::endl;
257 for (
long i = 0; i < t->GetEntries(); ++i) {
259 std::cout << (*setupKeyName) << std::endl;
262 if (setupKeyName)
delete setupKeyName;
270 TList* keys = aDir->GetListOfKeys();
271 for (TObject* akey : *keys) {
274 TObject* o = aDir->Get(akey->GetName());
277 if (o->InheritsFrom(TDirectory::Class()))
findTrees((TDirectory*)o, listOfTrees);
278 if (o->InheritsFrom(TTree::Class())) {
279 listOfTrees.emplace_back(std::string(aDir->GetPath()) + std::string(
"/") + std::string(o->GetName()));
291 B2WARNING(
"You need to run SectorMapComparer::CompareMaps before this function!");
298 std::string aName = hist.first;
299 TCanvas* can =
new TCanvas((aName +
"_can").c_str(), aName.c_str());
302 can->cd(1)->SetLogy((
int)logScale);
305 TLegend* l =
new TLegend(0.15, 0.7, 0.3, 0.9);
307 l->AddEntry(hbuff,
"first SectorMap",
"lpf");
310 l->AddEntry(hbuff,
"second SectorMap",
"lpf");
311 if (drawLegend) l->Draw();
313 can->cd(2)->SetLogy((
int)logScale);;
316 if (TString(aName).Contains(
"_max")) {
317 can->cd(3)->SetLogy((
int)logScale);
324 if (pdfFileName !=
"") {
325 if (n == 1) can->SaveAs(pdfFileName);
327 if (counter == 1) can->SaveAs(pdfFileName +
"(");
328 else if (counter == n) can->SaveAs(pdfFileName +
")");
329 else can->SaveAs(pdfFileName);
341 B2INFO(
"comparing SectorMaps in the following files");
348 if (!f_first->IsOpen() || !f_first->IsOpen()) {
349 B2ERROR(
"ERROR: one of the files not open");
351 if (f_first) f_first->Close();
352 if (f_second) f_second->Close();
357 std::vector<std::string> listOfTrees;
363 for (
const std::string& tname_first : listOfTrees) {
366 if (!TString(tname_first).Contains(setupName))
continue;
370 std::string tname_second = tname_first;
375 TTree* t_first = (TTree*)f_first->Get(tname_first.c_str());
376 TTree* t_second = (TTree*)f_second->Get(tname_second.c_str());
379 if (TString(tname_first).Contains(
"CompactSecIDs")) {
380 if (t_first->GetEntries() != t_second->GetEntries())
381 B2FATAL(
"The number of entries in CompactSecIDs should be identical! This indicates that two sectormaps with"
382 " different sectors on sensors are compared which will lead to wrong results! " << std::endl <<
383 "# CompactSecIDs SM1: " << t_first->GetEntries() << std::endl <<
384 "# CompactSecIDs SM2: " << t_second->GetEntries() << std::endl
390 B2INFO(
"Comparing tree: " << tname_first <<
" with tree: " << tname_second << std::endl);
392 if (!t_first || !t_second) {
393 B2ERROR(
"One of the trees not found!");
411 for (
const auto& entry : map) {
417 if (sector >= 0 && fullSID.
getSecID() != sector)
continue;
419 result += entry.second;
429 B2WARNING(
"You need to run SectorMapComparer::CompareMaps before this function!");
433 TH1F h_2hits(
"h_2hits",
"number of two hit connections starting at ith layer; layer; number connections", 7, -0.5, 6.5);
435 h_2hits.SetStats(
false);
437 TH1F h_2hits_matched(h_2hits);
438 h_2hits_matched.SetName(
"h2hits_matched");
439 h_2hits_matched.SetLineColor(kRed);
441 TH1F h_3hits(h_2hits);
442 h_3hits.SetName(
"h_3hits");
443 h_3hits.SetTitle(
"number of three hit connections starting at ith layer; layer; number connections");
444 TH1F h_3hits_matched(h_3hits);
445 h_3hits_matched.SetName(
"h_3hits_matched");
446 h_3hits_matched.SetLineColor(kRed);
448 for (
int ilayer = 1; ilayer < 7; ilayer++) {
451 int bin = h_2hits.FindBin(ilayer);
458 TCanvas* can =
new TCanvas(
"secStats",
"secStats");
460 can->cd(1)->SetLogy((
int)logScale);
462 TLegend* l =
new TLegend(0.15, 0.7, 0.3, 0.9);
464 TH1* hbuff = h_2hits.DrawCopy();
465 l->AddEntry(hbuff,
"all from first map",
"lpf");
466 hbuff = h_2hits_matched.DrawCopy(
"same");
467 l->AddEntry(hbuff,
"only matched ",
"lpf");
468 if (drawLegend) l->Draw();
470 can->cd(2)->SetLogy((
int)logScale);
472 h_3hits_matched.DrawCopy(
"same");
477 std::vector< std::unordered_map<std::string, int> > sensorsInLayer2Hits(6);
480 sensorsInLayer2Hits[
id.getLayerNumber() - 1][(std::string)
id ] = 1;
482 std::vector< std::unordered_map<std::string, int> > sensorsInLayer3Hits(6);
485 sensorsInLayer3Hits[
id.getLayerNumber() - 1][(std::string)
id] = 1;
488 for (
int i = 1; i <= 6; i++) {
491 bin = h_3hits.FindBin(i);
493 int nSensors2Hits = sensorsInLayer2Hits[i - 1].size();
494 int nSensors3Hits = sensorsInLayer3Hits[i - 1].size();
496 if (nSensors2Hits > 0) {
497 h_2hits.SetBinContent(bin, h_2hits.GetBinContent(bin) / nSensors2Hits);
498 h_2hits_matched.SetBinContent(bin, h_2hits_matched.GetBinContent(bin) / nSensors2Hits);
500 B2WARNING(
"No sensors found in layer " << i);
503 if (nSensors3Hits > 0) {
504 h_3hits.SetBinContent(bin, h_3hits.GetBinContent(bin) / nSensors3Hits);
505 h_3hits_matched.SetBinContent(bin, h_3hits_matched.GetBinContent(bin) / nSensors3Hits);
507 B2WARNING(
"No sensors found in layer " << i);
511 TCanvas* can2 =
new TCanvas(
"ConPerSensor",
"Connections per sensor");
513 can2->cd(1)->SetLogy((
int)logScale);
515 TLegend* l2 =
new TLegend(0.15, 0.7, 0.3, 0.9);
517 h_2hits.SetTitle(
"average number of 2Hit combinations per sensor starting at ith layer; layer; avg. connections/sensor");
518 hbuff = h_2hits.DrawCopy();
519 l2->AddEntry(hbuff,
"all from first map",
"lpf");
520 hbuff = h_2hits_matched.DrawCopy(
"same");
521 l2->AddEntry(hbuff,
"only matched ",
"lpf");
522 if (drawLegend) l2->Draw();
524 can2->cd(2)->SetLogy((
int)logScale);
525 h_3hits.SetTitle(
"average number of 3Hit combinations per sensor starting at ith layer; layer; avg. connections/sensor");
527 h_3hits_matched.DrawCopy(
"same");
531 if (pdfFileName !=
"") {
532 can->SaveAs(pdfFileName +
"(");
533 can2->SaveAs(pdfFileName +
")");