StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bsQaShowMap.C
1 #include "TBox.h"
2 #include "TCanvas.h"
3 #include "TF1.h"
4 #include "TFile.h"
5 #include "TGaxis.h"
6 #include "TGraph.h"
7 #include "TGraphErrors.h"
8 #include "TH1.h"
9 #include "TH2.h"
10 #include "TMath.h"
11 #include "TLegend.h"
12 #include "TLegendEntry.h"
13 #include "TLine.h"
14 #include "TPad.h"
15 #include "TPaveText.h"
16 #include "TString.h"
17 #include "TSystem.h"
18 #include "TStyle.h"
19 
20 #include <cmath>
21 #include <fstream>
22 #include <iostream>
23 #include <map>
24 using namespace std;
25 
26 typedef struct { int detId, ch, bs; float x, y; } st_info;
27 
28 //Read bsQa results, then visualize it according to FMS geomery
29 //===================================================================================================================
30 void bsQaShowMap(const char* inList = "FmsBsGainSample.txt", const char* inMap = "fmsCellMap.txt", bool PRINT = true)
31 {
32  std::vector<st_info> Info;
33 
34  //Read
35  ifstream in1, in2;
36  in1.open(inList);
37  in2.open(inMap);
38  if (!in1.is_open()) { cout <<"Cannot open file: " <<inList <<endl; return; }
39  if (!in2.is_open()) { cout <<"Cannot open file: " <<inMap <<endl; return; }
40  while (in1.is_open())
41  {
42  int detId1, detId2, ch1, ch2, bs, valid;
43  float x, y;
44  in1 >> detId1 >> ch1 >> bs;
45  in2 >> detId2 >> ch2 >> valid >> x >> y;
46 
47  if (!in1.good() || !in2.good()) break;
48  if ((detId1!=detId2) || (ch1!=ch2)) { cout <<"WARNING! File format does NOT match!" <<endl; return; }
49  if (valid != true) continue;
50  //cout <<Form("%2i %3i %6.2f %6.2f %2i %s", detId1, ch1, x, y, bs, bs==0?"":"!") <<endl;
51 
52  st_info tempInfo;
53  tempInfo.detId = detId1;
54  tempInfo.ch = ch1;
55  tempInfo.bs = bs;
56  tempInfo.x = x;
57  tempInfo.y = y;
58  Info.push_back(tempInfo);
59  }
60  in1.close();
61  in2.close();
62 
63  //Draw
64  //-------------------------------------------
65 
66  string inListName = inList;
67  std::size_t strPos = inListName.find(".txt");
68  string outName = inListName.substr(0, strPos);
69 
70  gStyle->SetOptDate(0);
71  gStyle->SetOptStat(0);
72 
73  TCanvas* c1 = new TCanvas(Form("bsQaMap_%s", outName.c_str()), outName.c_str(), 1200, 900);
74  c1->cd()->SetRightMargin(0.3);
75  TH2F* H2Frame = new TH2F("Frame", "", 210,-105,105, 210,-105,105);
76  H2Frame->SetTitle(Form("%s;X;Y", c1->GetTitle()));
77  H2Frame->Draw();
78 
79  int nCell[4] = {0}; //# of total valid cells, by detId
80  int nNZBS[4] = {0}; //# of non-zero BS cells, by detId
81  int nCell_nstb[4] = {0}; //# of non-zero BS cells, by nstb
82  int nNZBS_nstb[4] = {0}; //# of non-zero BS eells, by nstb
83 
84  int Colors[] = {51, 56, 61, 66, 71, 17, 80, 85, 90, 95, 100};
85  for (unsigned int a=0; a<Info.size(); a++)
86  {
87  st_info tempInfo = Info[a];
88  const int detId = tempInfo.detId;
89  const int bs = tempInfo.bs;
90  const float x = tempInfo.x;
91  const float y = tempInfo.y;
92  const float cellHW = detId<10?(5.8/2):(3.8/2);
93 
94  nCell[detId-8]++;
95  if (bs != 0) nNZBS[detId-8]++;
96 
97  int nstb = -1;
98  if (x>0 && y>0) nstb = 0; //ST
99  else if (x>0 && y<0) nstb = 1; //SB
100  else if (x<0 && y>0) nstb = 2; //NT
101  else if (x<0 && y<0) nstb = 3; //NB
102  nCell_nstb[nstb]++;
103  if (bs != 0) nNZBS_nstb[nstb]++;
104 
105  TPaveText* pt = new TPaveText(x-cellHW, y-cellHW, x+cellHW, y+cellHW);
106  pt->SetFillStyle(3001);
107  pt->AddText(Form("%i", bs));
108  pt->Draw("same");
109 
110  TBox* box = new TBox(x-cellHW, y-cellHW, x+cellHW, y+cellHW);
111  box->SetFillColor(Colors[bs+5]);
112  box->SetFillStyle(3001);
113  box->SetLineColor(1);
114  box->SetLineWidth(1);
115  box->Draw("same");
116  }
117 
118  TLegend* L1 = new TLegend(0.72, 0.1, 0.95, 0.9);
119  L1->SetMargin(0.1);
120  L1->AddEntry((TObject*)0, "Non-zero BS ratio", "");
121  L1->AddEntry((TObject*)0, "", "");
122  L1->AddEntry((TObject*)0, "All:", "");
123  const int nCell_all = nCell[0] + nCell[1] + nCell[2] + nCell[2];
124  const int nNZBS_all = nNZBS[0] + nNZBS[1] + nNZBS[2] + nNZBS[3];
125  L1->AddEntry((TObject*)0, Form("%i/%i (%4.3f)", nNZBS_all, nCell_all, (float)nNZBS_all/(float)nCell_all), "");
126  L1->AddEntry((TObject*)0, "", "");
127  L1->AddEntry((TObject*)0, "by detId", "");
128  for (int a=0; a<4; a++)
129  {
130  const char* ENT = Form("detId %i: %i/%i (%4.3f)", a+8,nNZBS[a],nCell[a],(float)nNZBS[a]/(float)nCell[a]);
131  L1->AddEntry((TObject*)0, ENT, "");
132  }
133  L1->AddEntry((TObject*)0, "", "");
134  L1->AddEntry((TObject*)0, "by NSTB", "");
135  for (int i=0; i<4; i++)
136  {
137  const char* ENT = Form("%s%s: %i/%i (%4.3f)", i<2?"S":"N", i%2==0?"T":"B",
138  nNZBS_nstb[i], nCell_nstb[i], (float)nNZBS_nstb[i]/(float)nCell_nstb[i]);
139  L1->AddEntry((TObject*)0, ENT, "");
140  }
141  L1->AddEntry((TObject*)0, "", "");
142  L1->Draw();
143 
144  if (PRINT) c1->Print(Form("%s.png", c1->GetName()));
145  return;
146 }//Main