Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Mat_map_surface_plot.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Mat_map_surface_plot.C
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <TROOT.h>
10 
11 #include "materialPlotHelper.cpp"
12 
13 #include <fstream>
14 #include <iostream>
15 #include <sstream>
16 
18 
19 void plot(std::vector<TH2F*> Map, const sinfo& surface_info, const std::string& name){
20 
21  std::string out_name = name+"/"+surface_info.name+"/"+surface_info.name+"_"+surface_info.idname;
22  gSystem->Exec( Form("mkdir %s", (name+"/"+surface_info.name).c_str()) );
23 
24  // Disk
25  if(surface_info.type == 2 || surface_info.type == 4){
26 
27  TText *vol = new TText(.1,.95,surface_info.name.c_str());
28  vol->SetNDC();
29  TText *surface = new TText(.1,.9,surface_info.id.c_str());
30  surface->SetNDC();
31  TText *surface_z = new TText(.1,.85,("Z = " + to_string(surface_info.pos)).c_str() );
32  surface_z->SetNDC();
33 
34  TCanvas *c1 = new TCanvas("c1","mat_X0",1200,1200);
35  c1->SetRightMargin(0.14);
36  c1->SetTopMargin(0.14);
37  c1->SetLeftMargin(0.14);
38  c1->SetBottomMargin(0.14);
39  Map[0]->Draw("COLZ");
40  vol->Draw();
41  surface->Draw();
42  surface_z->Draw();
43  c1->Print( (out_name+"_X0.pdf").c_str());
44  //c1->Print( (out_name+"_X0.root").c_str());
45 
46  delete c1;
47 
48  delete vol;
49  delete surface;
50  delete surface_z;
51  }
52 
53  // Cylinder
54  if(surface_info.type == 1){
55 
56  TText *vol = new TText(.1,.95,surface_info.name.c_str());
57  vol->SetNDC();
58  TText *surface = new TText(.1,.9,surface_info.id.c_str());
59  surface->SetNDC();
60  TText *surface_r = new TText(.1,.85,("R = " + to_string(surface_info.pos)).c_str() );
61  surface_r->SetNDC();
62  TCanvas *c1 = new TCanvas("c1","mat_X0",1200,1200);
63  c1->SetRightMargin(0.14);
64  c1->SetTopMargin(0.14);
65  c1->SetLeftMargin(0.14);
66  c1->SetBottomMargin(0.14);
67  Map[0]->Draw("COLZ");
68  vol->Draw();
69  surface->Draw();
70  surface_r->Draw();
71  c1->Print( (out_name+"_X0.pdf").c_str());
72  //c1->Print( (out_name+"_X0.root").c_str());
73 
74  delete c1;
75 
76  delete vol;
77  delete surface;
78  delete surface_r;
79  }
80  return;
81 }
82 
84 
85 void Initialise_hist(std::vector<TH2F*>& surface_hist,
86  const sinfo& surface_info){
87 
88  TH2F * Map_X0;
89  TH2F * Map_L0;
90 
91  TH2F * Map_scale;
92 
93  if(surface_info.type == 1){
94  Map_X0 = new TH2F(("Map_X0_"+surface_info.idname).c_str(),("Map_X0_"+surface_info.idname).c_str(),
95  50,-6,6,50,-3.2,3.2);
96  Map_L0 = new TH2F(("Map_L0_"+surface_info.idname).c_str(),("Map_L0_"+surface_info.idname).c_str(),
97  50,-6,6,50,-3.2,3.2);
98  Map_scale = new TH2F(("Map_scale_"+surface_info.idname).c_str(),("Map_scale_"+surface_info.idname).c_str(),
99  50,-6,6,50,-3.2,3.2);
100  Map_X0->GetXaxis()->SetTitle("Eta");
101  Map_X0->GetYaxis()->SetTitle("Phi");
102  Map_X0->GetZaxis()->SetTitle("X0");
103  Map_L0->GetXaxis()->SetTitle("Eta");
104  Map_L0->GetYaxis()->SetTitle("Phi");
105  Map_L0->GetZaxis()->SetTitle("L0");
106  }
107 
108  if(surface_info.type == 2 || surface_info.type == 4){
109  Map_X0 = new TH2F(("Map_X0_"+surface_info.idname).c_str(),("Map_X0_"+surface_info.idname).c_str(),
110  50,-1*surface_info.range_max,surface_info.range_max,50,-1*surface_info.range_max, surface_info.range_max);
111  Map_L0 = new TH2F(("Map_L0_"+surface_info.idname).c_str(),("Map_L0_"+surface_info.idname).c_str(),
112  50,-1*surface_info.range_max,surface_info.range_max,50,-1*surface_info.range_max, surface_info.range_max);
113  Map_scale = new TH2F(("Map_scale_"+surface_info.idname).c_str(),("Map_scale_"+surface_info.idname).c_str(),
114  50,-1*surface_info.range_max,surface_info.range_max,50,-1*surface_info.range_max, surface_info.range_max);
115  Map_X0->GetXaxis()->SetTitle("X [mm]");
116  Map_X0->GetYaxis()->SetTitle("Y [mm]");
117  Map_X0->GetZaxis()->SetTitle("X0");
118  Map_L0->GetXaxis()->SetTitle("X [mm]");
119  Map_L0->GetYaxis()->SetTitle("Y [mm]");
120  Map_L0->GetZaxis()->SetTitle("L0");
121  }
122  std::vector<TH2F*> v_hist;
123  v_hist.push_back(Map_X0);
124  v_hist.push_back(Map_L0);
125  v_hist.push_back(Map_scale);
126  surface_hist = v_hist;
127 }
128 
130 
131 void Fill(std::map<uint64_t,std::vector<TH2F*>>& surface_hist, std::map<uint64_t,sinfo>& surface_info,
132  const std::string& input_file, const int& nbprocess){
133 
134  std::map<std::string,std::string> surface_name;
135 
136  std::map<uint64_t,float> surface_weight;
137 
138  //Get file, tree and set top branch address
139  TFile *tfile = new TFile(input_file.c_str());
140  TTree *tree = (TTree*)tfile->Get("material-tracks");
141 
142  float v_phi = 0;
143  float v_eta = 0;
144  std::vector<float> *mat_X0 = 0;
145  std::vector<float> *mat_L0 = 0;
146  std::vector<float> *mat_step_length = 0;
147 
148  std::vector<uint64_t> *sur_id = 0;
149  std::vector<int32_t> *sur_type = 0;
150  std::vector<float> *sur_x = 0;
151  std::vector<float> *sur_y = 0;
152  std::vector<float> *sur_z = 0;
153  std::vector<float> *sur_range_min = 0;
154  std::vector<float> *sur_range_max = 0;
155 
156  tree->SetBranchAddress("v_phi",&v_phi);
157  tree->SetBranchAddress("v_eta",&v_eta);
158  tree->SetBranchAddress("mat_X0",&mat_X0);
159  tree->SetBranchAddress("mat_L0",&mat_L0);
160  tree->SetBranchAddress("mat_step_length",&mat_step_length);
161 
162  tree->SetBranchAddress("sur_id",&sur_id);
163  tree->SetBranchAddress("sur_type",&sur_type);
164  tree->SetBranchAddress("sur_x",&sur_x);
165  tree->SetBranchAddress("sur_y",&sur_y);
166  tree->SetBranchAddress("sur_z",&sur_z);
167  tree->SetBranchAddress("sur_range_min",&sur_range_min);
168  tree->SetBranchAddress("sur_range_max",&sur_range_max);
169 
170  int nentries = tree->GetEntries();
171  if(nentries > nbprocess && nbprocess != -1) nentries = nbprocess;
172  // Loop over all the material tracks.
173  for (Long64_t i=0;i<nentries; i++) {
174  if(i%10000==0) std::cout << "processed " << i << " events out of " << nentries << std::endl;
175  tree->GetEntry(i);
176 
177  // Reset the weight
178  for (auto weight_it = surface_weight.begin(); weight_it != surface_weight.end(); weight_it++){
179  weight_it->second = 0;
180  }
181  // loop over all the material hits to do initialisation and compute weight
182  for(int j=0; j<mat_X0->size(); j++ ){
183 
184  // Ignore surface of incorrect type
185  if(sur_type->at(j) == -1) continue;
186  // If a surface was never encountered initialise the hist, info and weight
187  if(surface_hist.find(sur_id->at(j))==surface_hist.end()){
188  float pos;
189  if(sur_type->at(j) == 1){
190  pos = sqrt(sur_x->at(j)*sur_x->at(j)+sur_y->at(j)*sur_y->at(j));
191  }
192  if(sur_type->at(j) == 2 || sur_type->at(j) == 4){
193  pos = sur_z->at(j);
194  }
195  surface_weight[sur_id->at(j)] = 0;
196  Initialise_info(surface_info[sur_id->at(j)], surface_name, sur_id->at(j), sur_type->at(j), pos, sur_range_min->at(j), sur_range_max->at(j));
197  Initialise_hist(surface_hist[sur_id->at(j)], surface_info[sur_id->at(j)]);
198  }
199  // Weight for each surface = number of hit associated to it.
200  surface_weight[sur_id->at(j)]++;
201  }
202 
203  // loop over all the material hit to fill the histogram
204  for(int j=0; j<mat_X0->size(); j++ ){
205 
206  // Ignore surface of incorrect type
207  if(sur_type->at(j) == -1) continue;
208 
209  if(sur_type->at(j) == 1){
210  surface_hist[sur_id->at(j)][0]->Fill(v_eta, v_phi, (mat_step_length->at(j)/mat_X0->at(j)));
211  surface_hist[sur_id->at(j)][1]->Fill(v_eta, v_phi, (mat_step_length->at(j)/mat_L0->at(j)));
212  surface_hist[sur_id->at(j)][2]->Fill(v_eta, v_phi, (1/surface_weight[sur_id->at(j)]));
213  }
214  if(sur_type->at(j) == 2 || sur_type->at(j) == 4){
215  surface_hist[sur_id->at(j)][0]->Fill(sur_x->at(j), sur_y->at(j), (mat_step_length->at(j)/mat_X0->at(j)));
216  surface_hist[sur_id->at(j)][1]->Fill(sur_x->at(j), sur_y->at(j), (mat_step_length->at(j)/mat_L0->at(j)));
217  surface_hist[sur_id->at(j)][2]->Fill(sur_x->at(j), sur_y->at(j), (1/surface_weight[sur_id->at(j)]));
218  }
219  }
220  }
221  // Normalise the histograms
222  for (auto hist_it = surface_hist.begin(); hist_it != surface_hist.end(); hist_it++){
223  hist_it->second[0]->Divide(hist_it->second[2]);
224  hist_it->second[1]->Divide(hist_it->second[2]);
225  }
226 }
227 
231 
232 void Mat_map_surface_plot(std::string input_file = "", int nbprocess = -1, std::string name = ""){
233 
234  gStyle->SetOptStat(0);
235  gStyle->SetOptTitle(0);
236 
237  std::map<uint64_t,std::vector<TH2F*>> surface_hist;
238  std::map<uint64_t,sinfo> surface_info;
239 
240  Fill(surface_hist, surface_info, input_file, nbprocess);
241  for (auto hist_it = surface_hist.begin(); hist_it != surface_hist.end(); hist_it++){
242  plot(hist_it->second, surface_info[hist_it->first], name);
243  for (auto hist : hist_it->second){
244  delete hist;
245  }
246  hist_it->second.clear();
247  }
248 }