Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:14:58

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <TROOT.h>
0010 
0011 #include "materialPlotHelper.cpp"
0012 
0013 #include <fstream>
0014 #include <iostream>
0015 #include <sstream>
0016 
0017 // Draw the plot for each surface.
0018 void plot(TGraph* Dist, const sinfo& surface_info, const std::string& name){
0019 
0020   std::string out_name = name+"/"+surface_info.name+"/"+surface_info.name+"_"+surface_info.idname;
0021   gSystem->Exec( Form("mkdir %s", (name+"/"+surface_info.name).c_str()) );
0022 
0023   TCanvas *c = new TCanvas("c","dist",1200,1200);
0024   c->SetRightMargin(0.14);
0025   c->SetTopMargin(0.14);
0026   c->SetLeftMargin(0.14);
0027   c->SetBottomMargin(0.14);
0028 
0029   Dist->Draw("AP");
0030   TLine *line_pos;
0031 
0032   TText *vol = new TText(.1,.95,surface_info.name.c_str());
0033   TText *surface = new TText(.1,.9,surface_info.id.c_str());
0034   TText *surface_z = new TText(.1,.85,("Z = " + to_string(surface_info.pos)).c_str() );
0035   TText *surface_r = new TText(.1,.85,("R = " + to_string(surface_info.pos)).c_str() );
0036   vol->SetNDC();
0037   surface->SetNDC();
0038   surface_z->SetNDC();
0039   surface_r->SetNDC();
0040 
0041   // Disk
0042   if(surface_info.type == 2 || surface_info.type == 4){
0043     vol->Draw();
0044     surface->Draw();
0045     surface_z->Draw();
0046 
0047     Dist->GetYaxis()->SetRangeUser(surface_info.range_min-(surface_info.range_max-surface_info.range_min)/10,
0048                                    surface_info.range_max+(surface_info.range_max-surface_info.range_min)/10);
0049 
0050     // Position of the disk surface
0051     line_pos = new TLine(surface_info.pos,surface_info.range_min,surface_info.pos,surface_info.range_max);
0052     line_pos->SetLineColor(kRed);
0053     line_pos->Draw("Same");
0054   }
0055 
0056   // Cylinder
0057   if(surface_info.type == 1){
0058     vol->Draw();
0059     surface->Draw();
0060     surface_r->Draw();
0061 
0062     Dist->GetYaxis()->SetRangeUser(surface_info.range_min-(surface_info.range_max-surface_info.range_min)/20,
0063                                    surface_info.range_max+(surface_info.range_max-surface_info.range_min)/20);
0064 
0065     // Position of the cylinder surface
0066     line_pos = new TLine(-1*surface_info.range_max, surface_info.pos, surface_info.range_max, surface_info.pos);
0067     line_pos->SetLineColor(kRed);
0068     line_pos->Draw("Same");
0069   }
0070 
0071   c->Print( (out_name+"_Dist.pdf").c_str());
0072   //c->Print( (out_name+"_Dist.root").c_str());
0073 
0074   delete c;
0075 
0076   delete vol;
0077   delete surface;
0078   delete surface_z;
0079   delete line_pos;
0080 }
0081 
0082 
0083 /// Create the Tgraphs from each surface based on a vector of positions.
0084 
0085 void Initialise_hist(TGraph*& surface_hist,
0086   const std::pair<std::vector<float>,std::vector<float>>& surface_pos, const sinfo& surface_info){
0087 
0088   if(surface_info.type != -1){
0089     TGraph * Dist = new TGraph(surface_pos.first.size(), &surface_pos.second[0], &surface_pos.first[0]);
0090     Dist->Draw();
0091     Dist->GetXaxis()->SetTitle("Z [mm]");
0092     Dist->GetYaxis()->SetTitle("R [mm]");
0093     surface_hist = Dist;
0094   }
0095 }
0096 
0097 /// Fill the histograms for each surfaces.
0098 
0099 void Fill(std::map<std::uint64_t,TGraph*>& surface_hist,  std::map<std::uint64_t,sinfo>& surface_info,
0100   const std::string& input_file, const int& nbprocess){
0101   std::map<std::string,std::string> surface_name;
0102   std::map<std::uint64_t,std::pair<std::vector<float>,std::vector<float>>> surface_pos;
0103 
0104   //Get file, tree and set top branch address
0105   TFile *tfile = new TFile(input_file.c_str());
0106   TTree *tree = (TTree*)tfile->Get("material-tracks");
0107 
0108   std::vector<float> *mat_x = 0;
0109   std::vector<float> *mat_y = 0;
0110   std::vector<float> *mat_z = 0;
0111 
0112   std::vector<std::uint64_t> *sur_id = 0;
0113   std::vector<std::int32_t> *sur_type = 0;
0114   std::vector<float> *sur_x = 0;
0115   std::vector<float> *sur_y = 0;
0116   std::vector<float> *sur_z = 0;
0117   std::vector<float> *sur_range_min = 0;
0118   std::vector<float> *sur_range_max = 0;
0119 
0120   tree->SetBranchAddress("mat_x",&mat_x);
0121   tree->SetBranchAddress("mat_y",&mat_y);
0122   tree->SetBranchAddress("mat_z",&mat_z);
0123 
0124   tree->SetBranchAddress("sur_id",&sur_id);
0125   tree->SetBranchAddress("sur_type",&sur_type);
0126   tree->SetBranchAddress("sur_x",&sur_x);
0127   tree->SetBranchAddress("sur_y",&sur_y);
0128   tree->SetBranchAddress("sur_z",&sur_z);
0129   tree->SetBranchAddress("sur_range_min",&sur_range_min);
0130   tree->SetBranchAddress("sur_range_max",&sur_range_max);
0131 
0132   int nentries = tree->GetEntries();
0133   if(nentries > nbprocess && nbprocess != -1) nentries = nbprocess;
0134   // Limit the number of event processed event to 10000
0135   // more could lead to errors with the TGraphs
0136   if(nentries > 10000){
0137     nentries = 10000;
0138     std::cout << "Number of event reduced to 10000" << std::endl;
0139   }
0140   // Loop over all the material tracks.
0141   for (Long64_t i=0;i<nentries; i++) {
0142     if(i%1000==0) std::cout << "processed " << i << " events out of " << nentries << std::endl;
0143     tree->GetEntry(i);
0144 
0145     // loop over all the material hits.
0146     for(int j=0; j<mat_x->size(); j++ ){
0147 
0148       // Ignore surface of incorrect type
0149       if(sur_type->at(j) == -1) continue;
0150 
0151       // If a surface was never encountered initialise the position info
0152       if(surface_hist.find(sur_id->at(j))==surface_hist.end()){
0153 
0154         float pos;
0155         float range;
0156         if(sur_type->at(j) == 1){
0157           pos = sqrt(sur_x->at(j)*sur_x->at(j)+sur_y->at(j)*sur_y->at(j));
0158         }
0159         if(sur_type->at(j) == 2 || sur_type->at(j) == 4){
0160           pos = sur_z->at(j);
0161         }
0162         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));
0163       }
0164       // Fill the vector of positions for each layer.
0165       surface_pos[sur_id->at(j)].first.push_back(sqrt(mat_y->at(j)*mat_y->at(j)+mat_x->at(j)*mat_x->at(j)));
0166       surface_pos[sur_id->at(j)].second.push_back(mat_z->at(j));
0167 
0168     }
0169   }
0170   // Use the vector of positions to create the TGraphs
0171   for (auto pos_it = surface_pos.begin(); pos_it != surface_pos.end(); pos_it++){
0172     Initialise_hist(surface_hist[pos_it->first], pos_it->second, surface_info[pos_it->first]);
0173   }
0174 }
0175 
0176 
0177 /// Plot the position of material interaction with respect with the associated surface.
0178 /// nbprocess : number of parameter to be processed.
0179 /// name : name of the output directory.
0180 
0181 
0182 void Mat_map_surface_plot_dist(std::string input_file = "", int nbprocess = -1, std::string name = ""){
0183 
0184   gStyle->SetOptStat(0);
0185   gStyle->SetOptTitle(0);
0186 
0187   std::map<std::uint64_t,TGraph*> surface_hist;
0188   std::map<std::uint64_t,sinfo> surface_info;
0189   Fill(surface_hist, surface_info, input_file, nbprocess);
0190   for (auto hist_it = surface_hist.begin(); hist_it != surface_hist.end(); hist_it++){
0191     if(hist_it->second)
0192     plot(hist_it->second, surface_info[hist_it->first], name);
0193   }
0194 }