Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pwg2_reader.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file pwg2_reader.cxx
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 
16 #include <iostream>
17 #include <fstream>
18 #include <memory>
19 #include <chrono>
20 #include <thread>
21 
22 #include "gzstream.h"
23 #include "PartonShower.h"
24 #include "JetScapeLogger.h"
25 #include "JetScapeReader.h"
26 #include "JetScapeBanner.h"
27 #include "fjcore.hh"
28 
29 #include <GTL/dfs.h>
30 
31 #include <TH1.h>
32 #include <TFile.h>
33 
34 using namespace std;
35 using namespace Jetscape;
36 
37 using namespace fjcore;
38 
39 // -------------------------------------
40 
41 // Forward declaration
43 ostream & operator<<(ostream & ostr, const PseudoJet & jet);
44 
45 // -------------------------------------
46 
47 // Create a pdf of the shower graph:
48 // Use with graphviz (on Mac: brew install graphviz --with-app)
49 // in shell: dot GVfile.gv -Tpdf -o outputPDF.pdf
50 // [or you can also use the GraphViz app for Mac Os X (in the "cellar" of homebrew)]
51 
52 // -------------------------------------
53 
54 int main(int argc, char** argv)
55 {
56 
57  // Parse arguments
58  string inname = "test_out.dat.gz";
59  string outname = "test_out.root";
60  string filetype = ".dat.gz";
61  std::size_t pos=0;
62  switch ( argc ){
63  break;
64  case 3:
65  inname = argv[1];
66  outname = argv[2];
67  break;
68  case 2:
69  inname = argv[1];
70  outname = inname;
71  pos = outname.find( filetype);
72  if ( pos==std::string::npos ){
73  throw std::runtime_error ("Please use a filename ending in .dat.gz for an automatically generated output name");
74  return -1;
75  }
76  outname.replace(pos, filetype.length(), ".root");
77  break;
78  case 1:
79  case 0:
80  break;
81  default:
82  cout << "Usage: readerTestWithRoot [inputname] [outputname]" << endl;
83  return -1;
84  }
85 
86  JetScapeLogger::Instance()->SetInfo(false);
87  JetScapeLogger::Instance()->SetDebug(false);
88  JetScapeLogger::Instance()->SetRemark(false);
89  JetScapeLogger::Instance()->SetVerboseLevel(0);
90 
91  cout<<endl;
92 
93  // VERY crude way to pull event information that
94  // resides at the foot of the file in a comment section
95  // (for technical reasons);
96  igzstream inFile;
97  inFile.open(inname.c_str());
98  string line;
99  int nAccepted=-1;
100  double sigmaGen=-1;
101  string cs="# ";
102  string tofind;
103  while (getline(inFile,line)) {
104  if ( line.compare(0, cs.length(), cs) != 0) continue;
105 
106  tofind = "# nAccepted = ";
107  pos = line.find( tofind );
108  if ( pos!=std::string::npos ) nAccepted=atoi( string ( line.begin()+pos+tofind.length(), line.end()).c_str());
109 
110  tofind = "# sigmaGen = ";
111  pos = line.find( tofind );
112  if ( pos!=std::string::npos ) sigmaGen=atof( string ( line.begin()+pos+tofind.length(), line.end()).c_str());
113  }
114  if ( sigmaGen<0 || nAccepted<0 ){
115  throw std::runtime_error("Couldn't find sigmaGen and nAccepted in the footer.");
116  return -1;
117  }
118 
119  INFO << "Using sigmaGen = " << sigmaGen << " and nAccepted = " << nAccepted;
120 
121  double weight = sigmaGen / nAccepted;
122  // weight =1;
123 
124  // Simple jetfinding ...
125  float R=0.4;
126  JetDefinition jet_def(antikt_algorithm, R);
127 
128  // Constituent selectors
129  // ---------------------
130  float max_track_rap = 2;
131  Selector select_track_rap = SelectorAbsRapMax(max_track_rap);
132  Selector select_pt = SelectorPtMin( 0.2 );
133  Selector select_cons = select_track_rap * select_pt;
134 
135  // jet selectors
136  Selector select_jet_eta = SelectorAbsEtaMax( max_track_rap-R );
137  Selector select_jet_pt = SelectorPtRange( 10,1000 );
138  Selector select_jet = select_jet_eta * select_jet_pt;
139 
140 
141  // dijet cuts
142  float ptlead = 40;
143  float ptsublead = 20;
144 
145  auto reader=make_shared<JetScapeReaderAsciiGZ>(inname);
146 
147  // Open output root file
148  TFile* fout = new TFile( outname.data(), "RECREATE");
149 
150  // turn on error bars by default
151  TH1::SetDefaultSumw2();
152 
153  // Histograms
154  TH1D* partonpt = new TH1D ("partonpt","Final Parton p_{T};p_{T} [GeV/c]",120, 0, 120);
155  TH1D* partoneta = new TH1D ("partoneta","Final Parton #eta; #eta",100, -5, 5);
156  TH1D* hardpartonpt = new TH1D ("hardpartonpt","Hard Parton p_{T};p_{T} [GeV/c]",80, 0, 40);
157  TH1D* hardpartoneta = new TH1D ("hardpartoneta","Hard Parton #eta; #eta",100, -5, 5);
158 
159  TH1D* leadjetpt = new TH1D ("leadjetpt","Leading Jet p_{T};p_{T}^{jet} [GeV/c]",120, 0, 120);
160  TH1D* subleadjetpt = new TH1D ("subleadjetpt","Sub-Leading Jet p_{T};p_{T}^{jet} [GeV/c]",120, 0, 120);
161 
162  TH1D* aj = new TH1D ("aj","A_{J};", 40, -1, 1 );
163 
164  TH1D* jetpt = new TH1D ("jetpt","Jet p_{T};p_{T}^{jet} [GeV/c]",120,0,120);
165  TH1D* jeteta = new TH1D ("jeteta","Jet #eta;#eta^{jet} [GeV/c]",100, -3, 3);
166 
167  // Histogram for jet fragmentation function
168  TH1D* jetz = new TH1D ("jetz","Jet z;p_{T}^{constituent} / p_{T}^{jet}",100, 0, 1);
169 
170  TH1D* dR = new TH1D ("dR","#DeltaR;counts",50, 0, 1);
171 
172  // reads in multiple events and multiple shower per event
173  vector<shared_ptr<PartonShower>> mShowers;
174 
175  long event = 0;
176  while (!reader->Finished()){
177  reader->Next();
178  if ( !(event%1000) ) cout << "Working on " << event << endl;
179  event++;
180 
181  // INFO<<"Analyze current event = "<<reader->GetCurrentEvent();
182  mShowers=reader->GetPartonShowers();
183 
184  // Collect jets from all showers
185  vector<PseudoJet> jets;
186  for (int i=0;i<mShowers.size();i++){
187  // Actual jetfinding
188  vector<PseudoJet> particles = select_cons(mShowers[i]->GetFinalPartonsForFastJet());
189  ClusterSequence cs(particles, jet_def);
190 
191  // mShowers[i]->PrintVertices();
192  // mShowers[i]->PrintPartons();
193 
194  // inital hard partons
195  hardpartonpt->Fill ( mShowers[i]->GetPartonAt(0)->pt(), weight );
196  hardpartoneta->Fill ( mShowers[i]->GetPartonAt(0)->eta(), weight );
197 
198  // final particles
199  for ( auto &p : mShowers[i]->GetFinalPartonsForFastJet() ){
200  partonpt->Fill ( p.pt(), weight );
201  partoneta->Fill ( p.eta(), weight );
202  }
203 
204  // jets
205  for (auto j : select_jet( cs.inclusive_jets()) ){
206  // additional cuts?
207  jets.push_back(j);
208 
209  // save some constituent information
210  for ( auto &consts : j.constituents() ){
211  // if ( consts.pt() > 40 ) {
212  // cout << "---" << endl;
213  // cout << consts << endl;
214  // // cout << " --> " << *(mShowers[i]->GetPartonAt(0)) << endl;
215  // cout << " --> shower initiator at pt = " << mShowers[i]->GetPartonAt(0)->pt()
216  // << " eta = " << mShowers[i]->GetPartonAt(0)->eta() << endl;
217  // // for ( auto &parts : mShowers[i]->GetFinalPartons() ){
218  // // cout << " ----> " << *parts << endl;
219  // // cout << " ----> " << parts->pt() << endl;
220  // // }
221 
222  // }
223  jetz->Fill( consts.pt() / j.pt(), weight );
224  dR->Fill( consts.delta_R(j), weight );
225  }
226 
227  }
228 
229 
230  }
231 
232  // analyze jets
233  if ( jets.size()==0 ) continue;
234 
235  jets = sorted_by_pt( jets );
236 
237  // misc information
238  for (auto j : jets){
239  // Fill histograms
240  jetpt->Fill( j.pt(), weight );
241  jeteta->Fill( j.eta(), weight );
242 
243  }
244 
245  // Dijets
246  if ( jets.size()<2 ) continue;
247  PseudoJet lead = jets.at(0);
248  PseudoJet sublead = jets.at(1);
249 
250  if ( lead.pt()<ptlead || sublead.pt()<ptsublead ) continue;
251 
252  // back to back?
253  float dphi = lead.delta_phi_to( sublead ); // always in -pi,pi
254  if ( dphi < -Jetscape::pi || dphi > Jetscape::pi ) {
255  cerr << " dphi = " << dphi << endl;
256  return -1;
257  }
258 
259  if ( dphi > -Jetscape::pi+R && dphi < Jetscape::pi-R ) continue;
260 
261  // Fill histos
262  leadjetpt->Fill( lead.pt(), weight );
263  subleadjetpt->Fill( sublead.pt(), weight );
264  aj->Fill( (lead.pt()-sublead.pt()) / (lead.pt()+sublead.pt()), weight);
265 
266 
267  }
268  reader->Close();
269  fout->Write();
270 
271  INFO_NICE<<"Finished!";
272  INFO << "Read from " << inname;
273  INFO << "Wrote to " << outname;
274  cout << endl;
275  return 0;
276 
277 }
278 
279 // -------------------------------------
280 
282 {
283  INFO<<"Some GTL graph/shower analysis/dfs search output:";
284 
285  dfs search;
286  search.calc_comp_num(true);
287  search.scan_whole_graph(true);
288  search.start_node();// defaulted to first node ...
289  search.run(*mS);
290 
291  cout<<endl;
292  cout<<"DFS graph search feature from GTL:"<<endl;
293  cout<<"Number of Nodes reached from node 0 = "<< search.number_of_reached_nodes () <<endl;
294  cout<<"Node/Vertex ordering result from DFS:"<<endl;
295  dfs::dfs_iterator itt2, endt2;
296  for (itt2 = search.begin(), endt2=search.end(); itt2 !=endt2; ++itt2)
297  {
298  cout<<*itt2<<" ";//<<"="<<search.dfs_num(*itt2)<<" ";
299  }
300  cout<<endl;
301 
302  cout<<"Edge/Parton ordering result from DFS:"<<endl;
303  dfs::tree_edges_iterator itt, endt;
304  for (itt = search.tree_edges_begin(), endt=search.tree_edges_end(); itt !=endt; ++itt)
305  {
306  cout<<*itt;//<<endl;
307  }
308  cout<<endl;
309 
310  dfs::roots_iterator itt3, endt3;
311  cout<<"List of root nodes found in graph/shower : ";
312  for (itt3 = search.roots_begin(), endt3=search.roots_end(); itt3 !=endt3; ++itt3)
313  {
314  cout<<**itt3;
315  }
316  cout<<endl;
317  cout<<endl;
318 }
319 
320 //----------------------------------------------------------------------
322 ostream & operator<<(ostream & ostr, const PseudoJet & jet) {
323  if (jet == 0) {
324  ostr << " 0 ";
325  } else {
326  ostr << " pt = " << jet.pt()
327  << " m = " << jet.m()
328  << " y = " << jet.rap()
329  << " phi = " << jet.phi();
330  }
331  return ostr;
332 }
333 
334 
335 //----------------------------------------------------------------------