stablejet is hosted by Hepforge, IPPP Durham
StableJet
stableTest2.cc
1 //a program to analyze jet stability
4 
5 
6 #include <iostream>
7 #include <fstream>
8 #include <cstdlib>
9 #include <unistd.h>
10 #include <vector>
11 #include <algorithm>
12 
13 #include "fastjet/ClusterSequence.hh"
14 #include "fastjet/PseudoJet.hh"
15 
16 #include "JetStabilityDefs.h"
17 #include "JetStability.h"
18 #include "DiffusionDistFastJet.h"
19 #include "SimpleSmear.h"
20 #include "SimpleEvent.hh"
21 #include "SimulatedEvent.hh"
22 #include "QCDandCmsLikeSmear.h"
23 
24 #include "PythiaJetGun.hh"
25 
26 #include "geners/BinaryFileArchive.hh"
27 #include "geners/Reference.hh"
28 
29 #include "TFile.h"
30 #include "TTree.h"
31 
32 
33 //-------- teypdefs and stuff -------------------
34 using namespace gs;
35 
36 bool oddSort ( const std::pair<unsigned,double> & a, const std::pair<unsigned,double> &b ) {
37  return a.second > b.second;
38 }
39 
40 
41 void printUsage(int argc, char **argv ) {
42  if ( argv[0] )
43  std::cout << argv[0] << " -a archive [-A nEvents] [-C nCycles] [-o outputFile] [-q fuzzyFile] [-m scanMin] [-M scanMax] [-n nScan] [-f fixedVar] [-E (set exclusive)] [-d distCut] [-N nJets] [-r R] dataDir" << std::endl;
44 }
45 
46 
47 int main (int argc, char **argv ) {
48 
49 
50  // options loaded from command line options
51  char *archiveName = 0;
52  unsigned nEvents = 0;
53  char *outFile = 0;
54  char *fuzzyFile = 0;
55  bool exclusive = false;
56  double distCut=1.;
57  unsigned N = 2U;
58  double R = 0.5;
59  unsigned nCycles=100;
60 
61  double scanMin = 0.1;
62  double scanMax = 1.;
63  unsigned scanN = 10;
64 
65  double fixed = 1.;
66 
67  //double qcdCutOff = exp(3.);
68  //double qcdCutOff = 0.01;
69  double qcdCutOff = 0.705796; // 75% quantile of dist for FluctuateQCD2_geom
70  bool doFlatPhaseSpace = true;
71 
72  int c;
73 
74  while (( c = getopt(argc,argv,"a:A:o:r:C:N:m:M:n:f:Ed:q:")) != -1 )
75  switch (c) {
76  case 'a':
77  archiveName = optarg;
78  break;
79  case 'A':
80  nEvents = atoi(optarg);
81  break;
82  case 'N':
83  N = atoi(optarg);
84  break;
85  case 'r':
86  R = atof(optarg);
87  break;
88  case 'o':
89  outFile = optarg;
90  break;
91  case 'C':
92  nCycles = atoi(optarg);
93  break;
94  case 'm':
95  scanMin = atof(optarg);
96  break;
97  case 'M':
98  scanMax = atof(optarg);
99  break;
100  case 'n':
101  scanN = atoi(optarg);
102  break;
103  case 'f':
104  fixed = atof(optarg);
105  break;
106  case 'E':
107  exclusive = true;
108  break;
109  case 'd':
110  distCut = atof(optarg);
111  break;
112  case 'q':
113  fuzzyFile = optarg;
114  break;
115  default:
116  printUsage(argc,argv);
117  return 0;
118  }
119 
120  char * dataDir = 0;
121 
122  for ( int index = optind; index < argc; index++ ) {
123  if ( index == optind ) dataDir = argv[index];
124  }
125 
126  if ( !dataDir ) {
127  std::cerr << "Must provide data directory as non-option argument" << std::endl;
128  return 1;
129  }
130 
131  // -------------- root tree branch variables
132  TFile f("output.root","RECREATE");
133  TTree *tree = new TTree("tree","tree");
134 
135  std::vector<double> *pt=0;
136  std::vector<int> *pid=0;
137 
138  double bpt,bphi,beta,bbarpt,bbarphi,bbareta;
139  int bstatus,bbarstatus;
140  double bdR;
141 
142  tree->Branch("pt",&pt);
143  tree->Branch("pid",&pid);
144  tree->Branch("bpt",&bpt,"bpt/D");
145  tree->Branch("bphi",&bphi,"bphi/D");
146  tree->Branch("beta",&beta,"beta/D");
147  tree->Branch("bstatus",&bstatus,"bstatus/I");
148  tree->Branch("bbarpt",&bbarpt,"bbarpt/D");
149  tree->Branch("bbarphi",&bbarphi,"bbarphi/D");
150  tree->Branch("bbareta",&bbareta,"bbareta/D");
151  tree->Branch("bbarstatus",&bbarstatus,"bbarstatus/I");
152  tree->Branch("bdR",&bdR,"bdR/D");
153 
154 
155  //---------------- the program ----------------------------
156 
157  // stability output stream
158  std::ofstream out;
159  if ( outFile )
160  out.open(outFile,std::ios_base::out);
161 
162  // fuzzyness output stream
163  std::ofstream fout;
164  if ( fuzzyFile )
165  fout.open(fuzzyFile,std::ios_base::out);
166 
167 
168  std::cout << "Opening archive.......";
169  // Open the archive
170  BinaryFileArchive ar(archiveName, "r");
171  if (!ar.isOpen())
172  {
173  std::cerr << ar.error() << std::endl;
174  return 1;
175  }
176  std::cout << "done" << std::endl;
177 
178  // Find all events in the archive with the given category and process them
179  std::cout << "Finding events.......";
180  Reference<SimulatedEvent> ref(ar, Regex(".*"), "dijet");
181  if ( !nEvents ) nEvents = ref.size();
182  std::cout << "done found: " << ref.size() << " events" << std::endl;
183 
184  // fastjet jet definition
185  stab::DiffusionDistFastJet fjDiffDist(R);
186  stab::JetStability jstab((fastjet::JetDefinition*)&fjDiffDist);
187  jstab.setScan(scanN,scanMin,scanMax);
188  jstab.setNCycles(nCycles);
189 
190  // simple smearing agent
191  //stab::SimpleSmear smear(0.087,0.15);
193 
194 
195  SimulatedEvent ev;
196  // loop over so many events
197  for ( unsigned evNum=0; evNum != nEvents; evNum++ ) {
198 
199  pt->clear();
200  pid->clear();
201 
202  stab::Event event;
203  stab::StabilityEventInfo evInfo;
204 
205  // restore event evNum from archive
206  ref.restore(evNum, &ev);
207  SimpleEvent & evs = ev.sim;
208  const unsigned nSimulatedJets = evs.size();
209 
210  PythiaEventInfo & pyInfo = ev.pyev;
211  const int pyN = pyInfo.N();
212  const int bnum = pyInfo.findFirst(5);
213  const int bbarnum = pyInfo.findFirst(-5);
214 
215  bpt = pyInfo.pt(bnum);
216  bphi = atan2(pyInfo.py(bnum),pyInfo.px(bnum));
217  beta = asinh(pyInfo.pz(bnum)/bpt);
218  bstatus = pyInfo.statusCode(bnum);
219 
220  bbarpt = pyInfo.pt(bbarnum);
221  bbarphi = atan2(pyInfo.py(bbarnum),pyInfo.px(bbarnum));
222  bbareta = asinh(pyInfo.pz(bbarnum)/bbarpt);
223  bbarstatus = pyInfo.statusCode(bbarnum);
224 
225  const double dEta = beta - bbareta;
226  double dPhi = bphi - bbarphi;
227  while ( dPhi > M_PI) dPhi -= 2*M_PI;
228  while ( dPhi <= -M_PI ) dPhi += 2*M_PI;
229 
230  bdR = sqrt(dEta*dEta+dPhi*dPhi);
231 
232  for ( unsigned j=0; j != nSimulatedJets; j++ ) {
233  const std::vector<PythiaJetGun::Particle> & constituents = evs[j].jetParticles;
234  const unsigned nConstituents = constituents.size();
235  for ( unsigned c=0; c != nConstituents; c++ ) {
236  const PythiaJetGun::Particle & part = constituents[c];
237  const rk::P4 & p4 = part.p4();
238  const geom3::Vector3 & mom = p4.momentum();
239  stab::PseudoJet pj(mom.x(),mom.y(),mom.z(),p4.e());
240  //stab::StabilityUserInfo *pjInfo = new stab::StabilityUserInfo(part.code(),part.charge());
241  //pj.set_user_info( pjInfo );
242  evInfo.push_back( stab::StabilityUserInfo(part.code(),part.charge()) );
243  event.push_back( pj );
244  pt->push_back(pj.pt());
245  pid->push_back(part.code());
246  }
247  }
248 
249  //jstab.reset(event,evInfo);
250  //jstab.run(smear);
251 
252  tree->Fill();
253  }
254 
255  /*const std::vector<std::vector<double> > & stability = jstab.pairwiseRandStability();
256  const std::vector<std::vector<double> > & fuzziness = jstab.fuzzinessNJet();
257 
258  const double sMax = jstab.scanMax();
259  const double sMin = jstab.scanMin();
260  const unsigned sN = jstab.nScan();
261  const double dScan = (sMax-sMin)/sN;
262 
263  const unsigned nStab = stability.size();
264  assert( nStab == sN );
265  assert( stability.size() == fuzziness.size() );
266  for ( unsigned s=0; s != nStab; s++ ) {
267  const double dCut = sMin + dScan*s;
268  const std::vector<double> & stabPoints = stability[s];
269  const std::vector<double> & fuzzPoints = fuzziness[s];
270  const unsigned nPoints = stabPoints.size();
271  long double avg = 0.L;
272  long double favg = 0.L;
273  for ( unsigned p=0; p != nPoints; p++ ) {
274  avg += stabPoints[p];
275  favg += fuzzPoints[p];
276  }
277  avg /= nPoints;
278  favg /= nPoints;
279  out << dCut << " " << avg << std::endl;
280  fout << dCut << " " << favg << std::endl;
281  }*/
282 
283  tree->Write();
284  f.Close();
285 
286  //delete tree;
287 
288 return 0;
289 }