Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:41

0001 #ifndef INTT_INTTXYVTX_H
0002 #define INTT_INTTXYVTX_H
0003 
0004 #include "InttVertexUtil.h"
0005 
0006 #include <TCanvas.h>
0007 #include <TFile.h>
0008 #include <TGraphErrors.h>
0009 #include <TH1.h>
0010 #include <TH2.h>
0011 #include <TLatex.h>
0012 #include <TLegend.h>
0013 #include <TProfile.h>
0014 
0015 #include <filesystem>
0016 #include <iostream>
0017 #include <map>
0018 #include <numeric>
0019 #include <string>
0020 #include <vector>
0021 
0022 // note : this class mainly focus on two things
0023 // note : 1. find a single vertex for the whole run
0024 // note :     a. the functions prepared for this purpose are : ProcessEvt(), GetFinalVTXxy(), MacroVTXSquare()
0025 // note : 2. find the vertex for each event
0026 // note :     a. the functions prepared for this purpose are : ProcessEvt()
0027 
0028 struct type_pos
0029 {
0030   double x;
0031   double y;
0032 };
0033 
0034 class INTTXYvtx
0035 {
0036  public:
0037   struct clu_info
0038   {
0039     int column;
0040     //--    int chip_id;
0041     double avg_chan;
0042     int sum_adc;
0043     int sum_adc_conv;
0044     int size;
0045 
0046     double x;
0047     double y;
0048     double z;
0049     int layer;
0050     double phi;
0051     // std::vector<double> bco_diff_vec; // note : for the multi-hit cluster, more than one hit was included. so more than one bco_diff
0052   };
0053 
0054  public:
0055   INTTXYvtx(const std::string& runType,
0056             const std::string& outFolderDirectory,
0057             std::pair<double, double> beamOrigin,
0058             double phiDiffCut = 0.11,
0059             std::pair<double, double> DCACut = {-1, 1},
0060             int NCluCutl = 20,
0061             int NCluCut = 10000,
0062             double angleDiffNew_l = 0.0,
0063             double angleDiffNew_r = 3,
0064             double peekCut = 3.32405,
0065             bool printMessageOpt = true);
0066 
0067   virtual ~INTTXYvtx();
0068 
0069   void Init();
0070 
0071   void SetBeamOrigin(double beamx, double beamy)
0072   {
0073     beam_origin = std::make_pair(beamx, beamy);
0074     current_vtxX = beam_origin.first;
0075     current_vtxY = beam_origin.second;
0076   }
0077 
0078   virtual void SetSaveHisto(const bool save) { m_savehist = save; }
0079   virtual void EnableDrawHisto(const bool enable) { m_enable_drawhist = enable; }
0080   virtual void EnableQA(const bool enable) { m_enable_qa = enable; }
0081   virtual void PrintMessageOpt(const bool flag) { print_message_opt = flag; }
0082 
0083   //////////////////////////////////////////////////////
0084   // read cluster and make cluster pair in vector
0085   void ProcessEvt(int event_i,
0086                   const std::vector<clu_info>& temp_sPH_inner_nocolumn_vec,
0087                   const std::vector<clu_info>& temp_sPH_outer_nocolumn_vec,
0088                   const std::vector<std::vector<double>>& temp_sPH_nocolumn_vec,
0089                   const std::vector<std::vector<double>>& temp_sPH_nocolumn_rz_vec,
0090                   int NvtxMC,
0091                   double TrigZvtxMC,
0092                   bool PhiCheckTag,
0093                   Long64_t bco_full);
0094 
0095   //////////////////////////////////////////////////////
0096   // calculate XY vertex
0097   //  quadorant method
0098   std::vector<std::pair<double, double>> MacroVTXSquare(double length, int N_trial);
0099   // |- subMacroVTXxyCorrection(i,i1, draw_plot_opt);
0100   //     |- GetVTXxyCorrection_new(true_trial_index);
0101   //         |- subMacroPlotWorking
0102   //             |- calculateDistanceAndClosestPoint(
0103   //             |- calculateAngleBetweenVectors(
0104   //
0105   // |- DrawTGraphErrors(
0106   // |- Draw2TGraph(
0107 
0108   //  linefilled method
0109   std::vector<std::pair<double, double>> FillLine_FindVertex(
0110       std::pair<double, double> window_center,
0111       double segmentation = 0.005,
0112       double window_width = 5.0,
0113       int N_bins = 100);
0114   // |- calculateDistanceAndClosestPoint(
0115   // |- calculateAngleBetweenVectors(
0116   // |- TH2F_FakeClone(
0117   // |- TH2F_threshold_advanced_2(
0118 
0119   virtual void ClearEvt();
0120   virtual void PrintPlots();
0121   virtual void EndRun();
0122 
0123   //////////////////////////////////////////////////////
0124   // access to internal variables, necessary?
0125   unsigned long GetVecNele();
0126   std::pair<double, double> GetFinalVTXxy();
0127   std::pair<std::vector<TH2F*>, std::vector<TH1F*>> GetHistFinal();
0128 
0129  protected:
0130   std::string run_type;
0131   std::string out_folder_directory;
0132   std::pair<double, double> beam_origin;
0133   double phi_diff_cut;
0134   std::pair<double, double> DCA_cut;
0135   int N_clu_cutl;
0136   int N_clu_cut;
0137   double angle_diff_new_l;
0138   double angle_diff_new_r;
0139   double peek;
0140   bool print_message_opt{false};
0141 
0142   bool m_savehist{false};
0143   bool m_enable_drawhist{false};
0144   bool m_enable_qa{false};
0145 
0146   bool m_initialized{false};
0147   std::string m_quad_pdfname{"New_Trial_square.pdf"};
0148 
0149  protected:
0150   ////////////////////////////
0151   std::vector<double> subMacroVTXxyCorrection(int test_index, int trial_index, bool draw_plot_opt);
0152   std::vector<double> GetVTXxyCorrection_new(int trial_index);
0153   virtual void subMacroPlotWorking(bool phi_correction, double cos_fit_rangel, double cos_fit_ranger, double guas_fit_range);
0154 
0155   std::vector<double> calculateDistanceAndClosestPoint(double x1, double y1, double x2, double y2, double target_x, double target_y);
0156   double calculateAngleBetweenVectors(double x1, double y1, double x2, double y2, double targetX, double targetY);
0157 
0158   // void                    PrintPlotsVTXxy(std::string sub_out_folder_name);
0159   void PrintPlotsVTXxy();
0160 
0161   std::vector<std::pair<double, double>> Get4vtx(std::pair<double, double> origin, double length);
0162   void TH1F_FakeClone(TH1F* hist_in, TH1F* hist_out);
0163   void TH2F_FakeClone(TH2F* hist_in, TH2F* hist_out);
0164 
0165   void ClearHist(int print_option = 0);
0166 
0167   void DrawTGraphErrors(std::vector<double> x_vec, std::vector<double> y_vec,
0168                         std::vector<double> xE_vec, std::vector<double> yE_vec,
0169                         const std::string& output_directory, std::vector<std::string> plot_name);
0170   void Draw2TGraph(std::vector<double> x1_vec, std::vector<double> y1_vec,
0171                    std::vector<double> x2_vec, std::vector<double> y2_vec,
0172                    const std::string& output_directory, std::vector<std::string> plot_name);
0173 
0174   void TH2F_threshold(TH2F* hist, double threshold);
0175   std::vector<double> SumTH2FColumnContent(TH2F* hist_in);
0176   void Hist_1D_bkg_remove(TH1F* hist_in, double factor);
0177   void TH2F_threshold_advanced_2(TH2F* hist, double threshold);
0178 
0179   // note : from the INTTXYvtxEvt.h // for FillLine_FindVertex
0180   void TH2FSampleLineFill(TH2F* hist_in,
0181                           double segmentation,
0182                           std::pair<double, double>
0183                               inner_clu,
0184                           std::pair<double, double> outer_clu);
0185 
0186  protected:
0187   std::vector<TH1*> m_v_hist{};
0188 
0189   /////////////////
0190   // QA histograms in process_evt // in m_v_hist
0191   TH2F* N_cluster_correlation{nullptr};        // QA fill: ProcessEvt, draw: PrintPlot
0192   TH2F* N_cluster_correlation_close{nullptr};  // QA fill: ProcessEvt, draw: PrintPlot
0193   TH2F* inner_pos_xy{nullptr};                 // QA fill: ProcessEvt, draw: PrintPlot
0194   TH2F* outer_pos_xy{nullptr};                 // QA fill: ProcessEvt, draw: PrintPlot
0195   TH2F* inner_outer_pos_xy{nullptr};           // QA fill: ProcessEvt, draw: PrintPlot
0196 
0197   // Quadorant method // in m_v_hist
0198   TH2F* DCA_distance_inner_phi{nullptr};  // fill: subMacroPlotWorking
0199   TH2F* angle_diff_inner_phi{nullptr};    // fill: subMacroPlotWorking
0200 
0201   TH1F* angle_diff{nullptr};                 // QA fill: subMacroPlotWorking
0202   TH1F* angle_diff_new{nullptr};             // QA fill: subMacroPlotWorking
0203   TH1F* angle_diff_new_bkg_remove{nullptr};  // QA fill: subMacroPlotWorking
0204   TH1F* DCA_distance{nullptr};               // QA fill: subMacroPlotWorking
0205 
0206   TH2F* DCA_distance_outer_phi{nullptr};  // QA fill: subMacroPlotWorking
0207   TH2F* angle_diff_outer_phi{nullptr};    // QA fill: subMacroPlotWorking
0208 
0209   TH2F* angle_correlation{nullptr};     // QA Fill: subMacroPlotWorking
0210   TH2F* angle_diff_DCA_dist{nullptr};   // QA Fill: subMacroPlotWorking
0211   TH2F* DCA_point{nullptr};             // QA fill: subMacroPlotWorking
0212   TH2F* DCA_distance_inner_X{nullptr};  // QA fill: subMacroPlotWorking
0213   TH2F* DCA_distance_inner_Y{nullptr};  // QA fill: subMacroPlotWorking
0214   TH2F* DCA_distance_outer_X{nullptr};  // QA fill: subMacroPlotWorking
0215   TH2F* DCA_distance_outer_Y{nullptr};  // QA fill: subMacroPlotWorking
0216 
0217   /// histograms & graphs created in subMacroPlotWorking
0218   TH2F* DCA_distance_inner_phi_peak{nullptr};                  // fill: subMacroPlotWorking
0219   TProfile* DCA_distance_inner_phi_peak_profile{nullptr};      // fill: subMacroPlotWorking
0220   TGraph* DCA_distance_inner_phi_peak_profile_graph{nullptr};  // fill: subMacroPlotWorking
0221 
0222   TH2F* angle_diff_inner_phi_peak{nullptr};                  // fill: subMacroPlotWorking
0223   TProfile* angle_diff_inner_phi_peak_profile{nullptr};      // fill: subMacroPlotWorking
0224   TGraph* angle_diff_inner_phi_peak_profile_graph{nullptr};  // fill: subMacroPlotWorking
0225 
0226   TH2F* DCA_distance_outer_phi_peak{nullptr};                  // QA fill: subMacroPlotWorking
0227   TProfile* DCA_distance_outer_phi_peak_profile{nullptr};      // QA fill: subMacroPlotWorking
0228   TGraph* DCA_distance_outer_phi_peak_profile_graph{nullptr};  // QA fill: subMacroPlotWorking
0229 
0230   TH2F* angle_diff_outer_phi_peak{nullptr};                  // QA fill: subMacroPlotWorking
0231   TProfile* angle_diff_outer_phi_peak_profile{nullptr};      // QA fill: subMacroPlotWorking
0232   TGraph* angle_diff_outer_phi_peak_profile_graph{nullptr};  // QA fill: subMacroPlotWorking
0233 
0234   // note : it's for the geometry correction // in m_v_hist
0235   TH2F* angle_diff_inner_phi_peak_final{nullptr};    // fill: MacroVTXSquare
0236   TH2F* DCA_distance_inner_phi_peak_final{nullptr};  // fill: MacroVTXSquare
0237 
0238   TH2F* angle_diff_outer_phi_peak_final{nullptr};    // QA fill: MacroVTXSquare
0239   TH2F* DCA_distance_outer_phi_peak_final{nullptr};  // QA fill: MacroVTXSquare
0240   TH1F* angle_diff_new_bkg_remove_final{nullptr};    // QA fill: MacroVTXSquare
0241 
0242   TF1* horizontal_fit_inner{nullptr};             // subMacroPlotWorking
0243   TF1* horizontal_fit_angle_diff_inner{nullptr};  // subMacroPlotWorking
0244   TF1* horizontal_fit_outer{nullptr};             // subMacroPlotWorking
0245   TF1* horizontal_fit_angle_diff_outer{nullptr};  // subMacroPlotWorking
0246 
0247   TF1* cos_fit{nullptr};   // subMacroPlotWorking, QA, not used for ana,
0248   TF1* gaus_fit{nullptr};  // subMacroPlotWorking, QA, not used for ana
0249 
0250   // LineFill method
0251   TH2F* xy_hist{nullptr};        // fill: FillLine_FindVertex(
0252   TH2F* xy_hist_bkgrm{nullptr};  // fill: FillLine_FindVertex(
0253 
0254   // note : to keep the cluster pair information
0255   // note : this is the vector for the whole run, not event by event
0256   std::vector<std::pair<type_pos, type_pos>> cluster_pair_vec{};
0257 
0258   double Clus_InnerPhi_Offset{0};
0259   double Clus_OuterPhi_Offset{0};
0260   double current_vtxX{0};
0261   double current_vtxY{0};
0262 
0263   int zvtx_cal_require = 15;
0264 
0265   std::string plot_text;
0266   long total_NClus{0};
0267 
0268   TCanvas* c1{nullptr};        // PrintPlotsVTXxy, PrintPlots(), FillLine_FindVertex, DrawTGraphErrors, Draw2TGraph
0269   TLatex* ltx{nullptr};        // PrintPlotsVTXxy, PrintPlots(), FillLine_FindVertex, DrawTGraphErrors, Draw2TGraph
0270   TLatex* draw_text{nullptr};  // PrintPlotsVTXxy,               FillLine_FindVertex, DrawTGraphErrors, Draw2TGraph
0271   /////////////////////
0272 
0273   void InitHist();
0274   void InitGraph();
0275   void InitRest();
0276 
0277   //--        void InitTreeOut();
0278 
0279   //--        // TFile * file_out;
0280   //--        // TTree * tree_out;
0281   //--        // double out_quadrant_corner_X;
0282   //--        // double out_quadrant_corner_Y;
0283   //--        // double out_quadrant_center_X;
0284   //--        // double out_quadrant_center_X;
0285   //--        // double out_line_filled_mean_X;
0286   //--        // double out_line_filled_mean_Y;
0287   //--        // double out_line_filled_stddev_X;
0288   //--        // double out_line_filled_stddev_Y;
0289 };
0290 
0291 #endif