File indexing completed on 2025-08-05 08:12:06
0001
0002 #include <iostream>
0003
0004
0005 #include <TROOT.h>
0006
0007 #include <TFile.h>
0008 #include <TKey.h>
0009 #include <TTree.h>
0010 #include <TH2F.h>
0011 #include <THnSparse.h>
0012
0013
0014 #include <eicsmear/smear/EventSmear.h>
0015 #include <eicsmear/erhic/EventPythia.h>
0016 #include <eicsmear/erhic/ParticleMC.h>
0017
0018 using namespace std;
0019
0020 int
0021 eic_sphenix_fill_xq2( TString filename_output,
0022 TString filename_mc,
0023 TString filename_mc_smeared,
0024 bool debug = false )
0025 {
0026
0027
0028
0029
0030
0031 double electron_pmin = 0.01;
0032
0033
0034 double electron_emin = 3.0;
0035
0036
0037 TFile *file_mc = new TFile(filename_mc, "OPEN");
0038 TFile *file_mc_smeared = new TFile(filename_mc_smeared, "OPEN");
0039
0040
0041 TTree *tree = (TTree*)file_mc->Get("EICTree");
0042 TTree *tree_smeared = (TTree*)file_mc_smeared->Get("Smeared");
0043
0044
0045
0046
0047
0048 double xsec = 0;
0049 unsigned nxsec = 0;
0050 int nevent = 0;
0051 TIter next(file_mc->GetListOfKeys());
0052 TKey *key;
0053 while ((key = (TKey*)next()))
0054 {
0055 if ( TString(key->GetName()) == "crossSection" )
0056 {
0057 xsec += (TString(((TObjString*)key->ReadObj())->GetName())).Atof();
0058 nxsec++;
0059 continue;
0060 }
0061
0062 if ( TString(key->GetName()) == "nEvents" )
0063 {
0064 nevent += (TString(((TObjString*)key->ReadObj())->GetName())).Atof();
0065 continue;
0066 }
0067 }
0068
0069
0070 if ( nxsec > 0 )
0071 xsec /= nxsec;
0072
0073 TObjString* gen_crossSection = (TObjString*)file_mc->Get("crossSection");
0074 TObjString* gen_nEvents = (TObjString*)file_mc->Get("nEvents");
0075
0076
0077 gen_crossSection->SetString( TString::Format("%f", xsec) );
0078 gen_nEvents->SetString( TString::Format("%d", nevent) );
0079
0080
0081 float xsec_fb = xsec * 1e9;
0082
0083
0084 float lumi_ifb = (float)nevent / (float)xsec_fb;
0085
0086
0087 TObjString* gen_lumi_ifb = new TObjString();
0088 gen_lumi_ifb->SetString( TString::Format("%f", lumi_ifb) );
0089
0090
0091 cout << "crossSection: " << gen_crossSection->GetName() << endl;
0092 cout << "nEvents: " << gen_nEvents->GetName() << endl;
0093 cout << "luminosity: " << gen_lumi_ifb->GetName() << endl;
0094
0095
0096 TFile *file_out = new TFile(filename_output, "RECREATE");
0097
0098
0099 tree->AddFriend(tree_smeared);
0100
0101 erhic::EventPythia *event = NULL;
0102 Smear::Event *eventS = NULL;
0103
0104 tree->SetBranchAddress("event", &event);
0105 tree->SetBranchAddress("eventS", &eventS);
0106
0107
0108 TH1F* h_eta = new TH1F("h_eta", ";#eta;dN/d#eta", 100, -5, 5);
0109 TH1F* h_eta_accept = (TH1F*)h_eta->Clone("h_eta_accept");
0110 h_eta_accept->SetLineColor(kGreen);
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126 const int nbins_x = 25;
0127
0128 double min_x = -5;
0129 double max_x = 0;
0130 double width_x = (max_x - min_x) / nbins_x;
0131 double bins_x[nbins_x+1];
0132 for( int i =0; i <= nbins_x; i++)
0133 {
0134
0135 bins_x[i] = TMath::Power( 10, min_x + i*width_x);
0136 }
0137
0138
0139 const int nbins_Q2 = 20;
0140
0141 double min_Q2 = -1;
0142 double max_Q2 = 4;
0143 double width_Q2 = (max_Q2 - min_Q2) / nbins_Q2;
0144 double bins_Q2[nbins_Q2+1];
0145 for( int i =0; i <= nbins_Q2; i++)
0146 bins_Q2[i] = TMath::Power( 10, min_Q2 + i*width_Q2);
0147
0148
0149 const int nbins_z = 20;
0150
0151 double min_z = 0.0;
0152 double max_z = 1.0;
0153 double width_z = (max_z - min_z) / nbins_z;
0154 double bins_z[nbins_z+1];
0155 for( int i =0; i <= nbins_z; i++)
0156 {
0157
0158 bins_z[i] = min_z + i*width_z;
0159 }
0160
0161
0162 const int nbins_pT1 = 5;
0163 const int nbins_pT2 = 4;
0164 const int nbins_pT = nbins_pT1 + nbins_pT2;
0165
0166 double min_pT = 0.0;
0167 double width_pT1 = 0.2;
0168 double width_pT2 = 1.0;
0169
0170 double bins_pT[nbins_pT+1];
0171 for( int i =0; i <= nbins_pT1; i++)
0172 {
0173
0174 bins_pT[i] = min_pT + i*width_pT1;
0175 }
0176 for( int i =1; i <= nbins_pT2; i++)
0177 {
0178
0179 bins_pT[nbins_pT1 + i] = min_pT + nbins_pT1*width_pT1 + i*width_pT2;
0180 }
0181
0182
0183
0184 const int hn_dis_ndim = 2;
0185 int hn_dis_nbins[] = { nbins_x,
0186 nbins_Q2 };
0187 double hn_dis_xmin[] = {0., 0. };
0188 double hn_dis_xmax[] = {0., 0. };
0189
0190 THnSparse* hn_dis = new THnSparseF("hn_dis_electron",
0191 "DIS Kinematis Per Event (Electron); x; Q2;",
0192 hn_dis_ndim,
0193 hn_dis_nbins,
0194 hn_dis_xmin,
0195 hn_dis_xmax );
0196
0197 hn_dis->GetAxis(0)->SetName("x");
0198 hn_dis->GetAxis(1)->SetName("Q2");
0199
0200 hn_dis->SetBinEdges(0,bins_x);
0201 hn_dis->SetBinEdges(1,bins_Q2);
0202
0203
0204 THnSparse* hn_dis_accept = (THnSparse*)hn_dis->Clone("hn_dis_electron_accept");
0205 hn_dis_accept->SetTitle("DIS Kinematis Per Event (Accepted)");
0206
0207
0208 const int hn_sidis_ndim = 4;
0209 int hn_sidis_nbins[] = { nbins_x,
0210 nbins_Q2,
0211 nbins_z,
0212 nbins_pT };
0213 double hn_sidis_xmin[] = {0., 0., 0., 0. };
0214 double hn_sidis_xmax[] = {0., 0., 0., 0. };
0215
0216 THnSparse* hn_sidis_pion_plus = new THnSparseF("hn_sidis_pion_plus",
0217 "SIDIS Kinematis Per Particle (Postitively Charged Pion);x;Q2;z;pT;",
0218 hn_sidis_ndim,
0219 hn_sidis_nbins,
0220 hn_sidis_xmin,
0221 hn_sidis_xmax );
0222
0223 hn_sidis_pion_plus->GetAxis(0)->SetName("x");
0224 hn_sidis_pion_plus->GetAxis(1)->SetName("Q2");
0225 hn_sidis_pion_plus->GetAxis(2)->SetName("z");
0226 hn_sidis_pion_plus->GetAxis(3)->SetName("pT");
0227
0228
0229 hn_sidis_pion_plus->SetBinEdges(0,bins_x);
0230 hn_sidis_pion_plus->SetBinEdges(1,bins_Q2);
0231 hn_sidis_pion_plus->SetBinEdges(2,bins_z);
0232 hn_sidis_pion_plus->SetBinEdges(3,bins_pT);
0233
0234
0235 THnSparse* hn_sidis_pion_minus = (THnSparseF*)hn_sidis_pion_plus->Clone("hn_sidis_pion_minus");
0236 hn_sidis_pion_minus->SetTitle("SIDIS Kinematis Per Particle (Negatively Charged Pion)");
0237
0238 THnSparse* hn_sidis_kaon_plus = (THnSparseF*)hn_sidis_pion_plus->Clone("hn_sidis_kaon_plus");
0239 hn_sidis_kaon_plus->SetTitle("SIDIS Kinematis Per Particle (Positively Charged Kaon)");
0240
0241 THnSparse* hn_sidis_kaon_minus = (THnSparseF*)hn_sidis_pion_plus->Clone("hn_sidis_kaon_minus");
0242 hn_sidis_kaon_minus->SetTitle("SIDIS Kinematis Per Particle (Negatively Charged Kaon)");
0243
0244
0245 THnSparse* hn_sidis_pion_plus_accept = (THnSparse*)hn_sidis_pion_plus->Clone("hn_sidis_pion_plus_accept");
0246 hn_sidis_pion_plus_accept->SetTitle("SIDIS Kinematis Per Particle (Positively Charged Pion, Accepted)");
0247
0248 THnSparse* hn_sidis_pion_minus_accept = (THnSparseF*)hn_sidis_pion_plus->Clone("hn_sidis_pion_minus_accept");
0249 hn_sidis_pion_minus_accept->SetTitle("SIDIS Kinematis Per Particle (Negatively Charged Pion, Accepted)");
0250
0251 THnSparse* hn_sidis_kaon_plus_accept = (THnSparseF*)hn_sidis_pion_plus->Clone("hn_sidis_kaon_plus_accept");
0252 hn_sidis_kaon_plus_accept->SetTitle("SIDIS Kinematis Per Particle (Positively Charged Kaon, Accepted)");
0253
0254 THnSparse* hn_sidis_kaon_minus_accept = (THnSparseF*)hn_sidis_pion_plus->Clone("hn_sidis_kaon_minus_accept");
0255 hn_sidis_kaon_minus_accept->SetTitle("SIDIS Kinematis Per Particle (Negatively Charged Kaon, Accepted)");
0256
0257
0258 TH2F* hxQ2 = (TH2F*)hn_dis->Projection(1,0);
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269 if ( debug )
0270 {
0271 for ( int bin_x = 1; bin_x <= hxQ2->GetNbinsX(); bin_x++ )
0272 {
0273 fprintf( stdout, "x = %.2e\n",
0274 TMath::Power(10, 0.5 * ( ( TMath::Log10( hxQ2->GetXaxis()->GetBinLowEdge(bin_x) ) )
0275 + ( TMath::Log10( hxQ2->GetXaxis()->GetBinLowEdge(bin_x) + hxQ2->GetXaxis()->GetBinWidth(bin_x) ) ) ) ) );
0276 }
0277 }
0278
0279
0280 if ( debug )
0281 {
0282 for ( int bin_Q2 = 1; bin_Q2 <= hxQ2->GetNbinsY(); bin_Q2++ )
0283 {
0284
0285 fprintf( stdout, "Q2 = %.2e\n",
0286 TMath::Power(10, 0.5 * ( ( TMath::Log10( hxQ2->GetYaxis()->GetBinLowEdge(bin_Q2) ) )
0287 + ( TMath::Log10( hxQ2->GetYaxis()->GetBinLowEdge(bin_Q2) + hxQ2->GetYaxis()->GetBinWidth(bin_Q2) ) ) ) ) );
0288 }
0289 }
0290
0291
0292 unsigned max_event = tree->GetEntries();
0293
0294 if ( debug )
0295 max_event = 10000;
0296
0297 for ( unsigned ievent = 0; ievent < max_event; ievent++ )
0298 {
0299 if ( ievent%1000 == 0 )
0300 cout << "Processing event " << ievent << endl;
0301
0302
0303 tree->GetEntry(ievent);
0304
0305
0306 float y = event->GetTrueY();
0307 if ( y > 0.95 || y < 0.01 )
0308 continue;
0309
0310 float x = event->GetTrueX();
0311 float Q2 = event->GetTrueQ2();
0312
0313 double fill_hn_dis[] = {x, Q2};
0314 hn_dis->Fill( fill_hn_dis );
0315
0316
0317 bool electron_detected = false;
0318 if ( eventS->ScatteredLepton() && eventS->ScatteredLepton()->GetP() > electron_pmin && eventS->ScatteredLepton()->GetE() > electron_emin )
0319 electron_detected = true;
0320
0321
0322 if ( !electron_detected )
0323 continue;
0324
0325
0326 hn_dis_accept->Fill( fill_hn_dis );
0327
0328
0329
0330 unsigned ntracks = event->GetNTracks();
0331
0332 for ( unsigned itrack = 0; itrack < ntracks; itrack++ )
0333 {
0334 erhic::ParticleMC * iparticle = event->GetTrack( itrack );
0335 Smear::ParticleMCS * ismeared = eventS->GetTrack( itrack );
0336
0337
0338 if ( iparticle->GetStatus() != 1 )
0339 continue;
0340
0341
0342 float z = iparticle->GetZ();
0343
0344
0345 if ( z <= 0.15 )
0346 continue;
0347
0348
0349 if ( iparticle->GetEta() < -4 || iparticle->GetEta() > 4 )
0350 continue;
0351
0352
0353 float pT = iparticle->GetPtVsGamma();
0354
0355
0356 double fill_hn_sidis[] = {x, Q2, z, pT};
0357
0358
0359 int pid = iparticle->Id().Code();
0360
0361
0362
0363
0364 if ( pid == 211 )
0365 {
0366 hn_sidis_pion_plus->Fill( fill_hn_sidis );
0367
0368 if ( ismeared->GetP() > 0.1 && ismeared->Id().Code() == 211 )
0369 hn_sidis_pion_plus_accept->Fill( fill_hn_sidis );
0370 }
0371
0372
0373 else if ( pid == -211 )
0374 {
0375 hn_sidis_pion_minus->Fill( fill_hn_sidis );
0376
0377 if ( ismeared->GetP() > 0.1 && ismeared->Id().Code() == -211 )
0378 hn_sidis_pion_minus_accept->Fill( fill_hn_sidis );
0379 }
0380
0381
0382 else if ( pid == 321 )
0383 {
0384 hn_sidis_kaon_plus->Fill( fill_hn_sidis );
0385
0386 if ( ismeared->GetP() > 0.1 && ismeared->Id().Code() == 321 )
0387 hn_sidis_kaon_plus_accept->Fill( fill_hn_sidis );
0388 }
0389
0390
0391 else if ( pid == -321 )
0392 {
0393 hn_sidis_kaon_minus->Fill( fill_hn_sidis );
0394
0395 if ( ismeared->GetP() > 0.1 && ismeared->Id().Code() == -321 )
0396 hn_sidis_kaon_minus_accept->Fill( fill_hn_sidis );
0397 }
0398 }
0399
0400 }
0401
0402
0403 hn_dis->Write();
0404
0405 hn_dis_accept->Write();
0406
0407 hn_sidis_pion_plus->Write();
0408 hn_sidis_pion_minus->Write();
0409 hn_sidis_kaon_plus->Write();
0410 hn_sidis_kaon_minus->Write();
0411
0412 hn_sidis_pion_plus_accept->Write();
0413 hn_sidis_pion_minus_accept->Write();
0414 hn_sidis_kaon_plus_accept->Write();
0415 hn_sidis_kaon_minus_accept->Write();
0416
0417 h_eta->Write();
0418 h_eta_accept->Write();
0419
0420 gen_crossSection->Write("crossSection");
0421 gen_nEvents->Write("nEvents");
0422
0423 gen_lumi_ifb->Write("luminosity");
0424
0425
0426 file_out->Close();
0427
0428 return 0;
0429 }
0430
0431
0432
0433 int main( int argc , char* argv[] )
0434 {
0435 if ( argc < 4 || argc > 5 )
0436 {
0437 cout << "Usage: " << argv[0] << " <filename_output> <filename_EICTree> <filename_EICTree_smeared> <optional: 'debug' for debug mode" << endl;
0438 return 1;
0439 }
0440
0441 cout << "Running eic_sphenix_fill_xq2 with: \n" << endl;
0442 cout << " - Output file: " << argv[1] << endl;
0443 cout << " - EICTree input file: " << argv[2] << endl;
0444
0445 if ( argc == 4 )
0446 {
0447 cout << " - EICTree (smeared) file: " << argv[3] << endl;
0448 eic_sphenix_fill_xq2( argv[1], argv[2], argv[3] );
0449 }
0450 else if ( argc == 5 )
0451 {
0452 cout << " - EICTree (smeared) file: " << argv[3] << endl;
0453 cout << " ==== DEBUG MODE ==== " << endl;
0454 eic_sphenix_fill_xq2( argv[1], argv[2], argv[3], true );
0455 }
0456
0457 return 0;
0458 }