Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:12:32

0001 #include <TFile.h>
0002 
0003 #include <TTree.h>
0004 
0005 #include <TDirectory.h>
0006 
0007 // #include "InttEvent.cc"
0008 
0009 #include "mbdtree.C"
0010 
0011 #include <iostream>
0012 
0013 //R__LOAD_LIBRARY(libInttEvent.so)
0014 
0015 void sync_mbd_intt_Z() {
0016 
0017     TDirectory * gDir = gDirectory;
0018 
0019     TFile * f_mbd = TFile::Open("/sphenix/tg/tg01/commissioning/INTT/subsystems/MBD/auau2023_v1/beam_seb18-00020869-0000_mbd.root");
0020     gDirectory = gDir;
0021     TTree * t_mbd = (TTree * ) f_mbd -> Get("t");
0022     cout << " " << t_mbd << endl;
0023     if (!t_mbd) return;
0024     mbdtree mbdt(t_mbd);
0025 
0026     string folder_direction = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_beam_inttall-00020869-0000_event_base_ana_cluster_full_survey_3.32_excludeR40000_200kEvent_3HotCut_advanced_test";
0027     TFile * f_intt = TFile::Open(Form("%s/INTT_zvtx.root", folder_direction.c_str()));
0028     gDirectory = gDir;
0029     TTree * t_intt = (TTree * ) f_intt -> Get("tree_Z");
0030     if (!t_intt) return;
0031 
0032     int intt_eID, intt_N_cluster_outer, intt_N_cluster_inner, intt_N_good, intt_ES_N_good, intt_N_group;
0033     double intt_ES_zvtx, intt_ES_zvtxE, intt_ES_rangeL, intt_ES_rangeR, intt_ES_width_density;
0034     double intt_LB_Gaus_mean, intt_LB_Gaus_meanE, intt_LB_Gaus_width, intt_LB_Gaus_offset, intt_LB_Gaus_chi2, intt_LB_Gaus_size_width, intt_LB_geo_mean;
0035     double MC_true_zvtx;
0036     bool intt_good_zvtx_tag;
0037     Long64_t intt_bco_full; 
0038 
0039     t_intt -> SetBranchAddress("eID",&intt_eID);
0040     t_intt -> SetBranchAddress("bco_full",&intt_bco_full);
0041     t_intt -> SetBranchAddress("nclu_inner",&intt_N_cluster_inner);
0042     t_intt -> SetBranchAddress("nclu_outer",&intt_N_cluster_outer);
0043     t_intt -> SetBranchAddress("ES_zvtx",&intt_ES_zvtx);
0044     t_intt -> SetBranchAddress("ES_zvtxE",&intt_ES_zvtxE);
0045     t_intt -> SetBranchAddress("ES_rangeL",&intt_ES_rangeL);
0046     t_intt -> SetBranchAddress("ES_rangeR",&intt_ES_rangeR);
0047     t_intt -> SetBranchAddress("ES_N_good",&intt_ES_N_good);
0048     t_intt -> SetBranchAddress("ES_Width_density",&intt_ES_width_density);
0049     t_intt -> SetBranchAddress("LB_Gaus_mean",&intt_LB_Gaus_mean);
0050     t_intt -> SetBranchAddress("LB_Gaus_meanE",&intt_LB_Gaus_meanE);
0051     t_intt -> SetBranchAddress("LB_Gaus_width",&intt_LB_Gaus_width);
0052     t_intt -> SetBranchAddress("LB_Gaus_offset", &intt_LB_Gaus_offset);
0053     t_intt -> SetBranchAddress("LB_Gaus_chi2", &intt_LB_Gaus_chi2);
0054     t_intt -> SetBranchAddress("LB_Gaus_size_width", &intt_LB_Gaus_size_width);
0055     t_intt -> SetBranchAddress("LB_geo_mean", &intt_LB_geo_mean);
0056     t_intt -> SetBranchAddress("good_zvtx_tag", &intt_good_zvtx_tag);
0057     t_intt -> SetBranchAddress("N_group", &intt_N_group);
0058     t_intt -> SetBranchAddress("MC_true_zvtx",&MC_true_zvtx);
0059 
0060     cout << t_mbd -> GetEntries() << " " << t_intt -> GetEntries() << endl;
0061 
0062     TFile * out_file = new TFile(Form("%s/INTT_MBD_zvtx.root",folder_direction.c_str()),"RECREATE");
0063 
0064     int out_eID, out_N_cluster_outer, out_N_cluster_inner, out_N_good, out_ES_N_good, out_N_group;
0065     double out_ES_zvtx, out_ES_zvtxE, out_ES_rangeL, out_ES_rangeR, out_ES_width_density;
0066     double out_LB_Gaus_mean, out_LB_Gaus_meanE, out_LB_Gaus_width, out_LB_Gaus_offset, out_LB_Gaus_chi2, out_LB_Gaus_size_width, out_LB_geo_mean;
0067     double out_MC_true_zvtx;
0068     bool out_good_zvtx_tag;
0069     Long64_t out_bco_full;  
0070 
0071     double out_mbd_bz, out_mbd_bqs, out_mbd_bqn;
0072 
0073     TTree * tree_out =  new TTree ("tree_Z", "INTT Z info.");
0074 
0075     tree_out -> Branch("eID",&out_eID);
0076     tree_out -> Branch("bco_full",&out_bco_full);
0077     tree_out -> Branch("nclu_inner",&out_N_cluster_inner);
0078     tree_out -> Branch("nclu_outer",&out_N_cluster_outer);
0079     tree_out -> Branch("ES_zvtx",&out_ES_zvtx);
0080     tree_out -> Branch("ES_zvtxE",&out_ES_zvtxE);
0081     tree_out -> Branch("ES_rangeL",&out_ES_rangeL);
0082     tree_out -> Branch("ES_rangeR",&out_ES_rangeR);
0083     tree_out -> Branch("ES_N_good",&out_ES_N_good);
0084     tree_out -> Branch("ES_Width_density",&out_ES_width_density);
0085     tree_out -> Branch("LB_Gaus_mean",&out_LB_Gaus_mean);
0086     tree_out -> Branch("LB_Gaus_meanE",&out_LB_Gaus_meanE);
0087     tree_out -> Branch("LB_Gaus_width",&out_LB_Gaus_width);
0088     tree_out -> Branch("LB_Gaus_offset", &out_LB_Gaus_offset);
0089     tree_out -> Branch("LB_Gaus_chi2", &out_LB_Gaus_chi2);
0090     tree_out -> Branch("LB_Gaus_size_width", &out_LB_Gaus_size_width);
0091     tree_out -> Branch("LB_geo_mean", &out_LB_geo_mean);
0092     tree_out -> Branch("good_zvtx_tag", &out_good_zvtx_tag);
0093     tree_out -> Branch("N_group", &out_N_group);
0094     tree_out -> Branch("MC_true_zvtx",&out_MC_true_zvtx);
0095 
0096     tree_out -> Branch("mbd_bz",&out_mbd_bz); // note : mbd branch
0097     tree_out -> Branch("mbd_bqs",&out_mbd_bqs);
0098     tree_out -> Branch("mbd_bqn",&out_mbd_bqn);
0099 
0100     gDirectory = gDir;
0101 
0102     TH2F * h_qmbd_nintt = new TH2F("h_qmbd_nintt", "BbcQ vs Intt N", 200, 0, 9000, 200, 0, 4000);
0103     TH2F * intt_mbd_bco = new TH2F("intt_mbd_bco", "INTT - MBD", 100, 0, 50000, 100, -10, 100000);
0104 
0105     int prev_mbdclk = 0;
0106     ULong64_t prev_bco = 0;
0107 
0108     bool found_firstevt = false;
0109     int mbd_evt_offset = 0;
0110     int intt_evt_offset = 0;
0111 
0112     long long number_Nevent = t_intt -> GetEntries();
0113 
0114     // note : if the for loop is based on the one with smaller number of entries, the closer part should be paied attention.
0115     // note : because that the for loop ends when "i < t_intt -> GetEntries()", but we have the "intt_evt_offset" which is not zero if some missing event from the MBD was found.
0116     // note : when we use "GetEntry(i+intt_evt_offset)", it will try to access the event out of the range.
0117     // note : but some how it works, and return the last event it can access which is the last one. 
0118     for (int i = 0; i < number_Nevent; i++) {
0119         mbdt.LoadTree(i + mbd_evt_offset);
0120         mbdt.GetEntry(i + mbd_evt_offset);
0121         t_intt -> GetEntry(i + intt_evt_offset); 
0122 
0123         float bbcq = mbdt.bqn + mbdt.bqs;
0124 
0125         unsigned short mbdclk = mbdt.femclk;
0126         ULong64_t bco = intt_bco_full;
0127         ULong64_t bco16 = bco & 0xFFFF;
0128 
0129         int mbd_prvdif = (mbdclk - prev_mbdclk) & 0xFFFF;
0130         ULong64_t intt_prvdif = bco - prev_bco;
0131 
0132         prev_mbdclk = mbdclk;
0133         prev_bco = bco;
0134 
0135         out_mbd_bqn = mbdt.bqn;
0136         out_mbd_bqs = mbdt.bqs;
0137         out_mbd_bz  = mbdt.bz;
0138 
0139         out_eID = intt_eID;
0140         out_bco_full = intt_bco_full;
0141         out_N_cluster_inner = intt_N_cluster_inner;
0142         out_N_cluster_outer = intt_N_cluster_outer;
0143         out_ES_zvtx = intt_ES_zvtx;
0144         out_ES_zvtxE = intt_ES_zvtxE;
0145         out_ES_rangeL = intt_ES_rangeL;
0146         out_ES_rangeR = intt_ES_rangeR;
0147         out_ES_N_good = intt_ES_N_good;
0148         out_ES_width_density = intt_ES_width_density;
0149         out_LB_Gaus_mean = intt_LB_Gaus_mean;
0150         out_LB_Gaus_meanE = intt_LB_Gaus_meanE;
0151         out_LB_Gaus_width = intt_LB_Gaus_width;
0152         out_LB_Gaus_offset = intt_LB_Gaus_offset;
0153         out_LB_Gaus_chi2 = intt_LB_Gaus_chi2;
0154         out_LB_Gaus_size_width = intt_LB_Gaus_size_width;
0155         out_LB_geo_mean = intt_LB_geo_mean;
0156         out_good_zvtx_tag = intt_good_zvtx_tag;
0157         out_MC_true_zvtx = MC_true_zvtx;
0158         out_N_group = intt_N_group;
0159 
0160         if (intt_N_cluster_inner != -1 && intt_N_cluster_outer != -1){
0161             if (mbdt.bqs > 100 && mbdt.bqn > 100){
0162                 h_qmbd_nintt -> Fill(intt_N_cluster_inner + intt_N_cluster_outer, bbcq);
0163             }
0164             
0165             intt_mbd_bco -> Fill(i, (mbdclk - bco16) & 0xFFFF);
0166 
0167             tree_out -> Fill();
0168         }
0169         
0170 
0171         if ((i % 1000) == 0) {
0172             cout << i << " " << hex << setw(6) << mbdclk << " " << setw(6) << bco16 << " (mbd-intt)" << setw(6) << ((mbdclk - bco16) & 0xFFFF) <<
0173                 "      (femclk-prev)" << setw(6) << mbd_prvdif << " (bco-prev)" << setw(6) << intt_prvdif << dec << endl;
0174         }
0175 
0176         // note : if the total access event touches the last event of the INTT tree, break the loop.
0177         if (i + intt_evt_offset == number_Nevent) {break;}
0178 
0179         t_intt -> GetEntry(i + 1 + intt_evt_offset);
0180         ULong64_t next_bco16 = (intt_bco_full) & 0xFFFF;
0181         mbdt.LoadTree(i + 1);
0182         mbdt.GetEntry(i + 1);
0183         unsigned short next_mbdclk = mbdt.femclk;
0184         if (((next_mbdclk - next_bco16) & 0xFFFF) != ((mbdclk - bco16) & 0xFFFF)) intt_evt_offset += 1;
0185     }
0186 
0187     out_file -> cd();
0188     tree_out->SetDirectory(out_file);
0189     tree_out -> Write("", TObject::kOverwrite);
0190     out_file -> Close();
0191     
0192     TFile * froot = new TFile(Form("%s/sync.root",folder_direction.c_str()), "recreate");
0193     h_qmbd_nintt -> Write();
0194     intt_mbd_bco -> Write();
0195     froot -> Close();
0196 
0197     
0198 }