Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TPCEventDisplay_Clusters.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TPCEventDisplay_Clusters.C
1 #include <cstdlib>
2 #include <iostream>
3 #include <string>
4 
5 #include "TFile.h"
6 #include "TTree.h"
7 #include "TString.h"
8 #include "TObjString.h"
9 #include "TSystem.h"
10 #include "TROOT.h"
11 
12 #include <boost/format.hpp>
13 #include <boost/math/special_functions/sign.hpp>
14 /*************************************************************/
15 /* TPC Event Display */
16 /* Thomas Marshall,Aditya Dash */
17 /* rosstom@ucla.edu,aditya55@physics.ucla.edu */
18 /*************************************************************/
19 
20 void TPCEventDisplay_Clusters(const string &outfile = "./TPCEventDisplay_cosmics25926_clusters",
21  const string &infile = "/sphenix/u/rosstom/macros/TrackingProduction/cosmics25926_ntup.root",
22  const string &runNumber = "25926",
23  int nEventsToRun = 5){
24 
25  TFile *ntpFile = TFile::Open(infile.c_str());
26  TTree *hitTree = (TTree*)ntpFile->Get("ntp_cluster");
27 
28  Float_t event;
29  Float_t x, y, z, adc;
30  hitTree->SetBranchAddress("event",&event);
31  hitTree->SetBranchAddress("x",&x);
32  hitTree->SetBranchAddress("y",&y);
33  hitTree->SetBranchAddress("z",&z);
34  hitTree->SetBranchAddress("adc",&adc);
35 
36  Int_t currentEvent = 0;
37  bool firstHit = true;
38 
39  std::ofstream outdata;
40  outdata.open((outfile+"_event0.json").c_str(),std::ofstream::out | std::ofstream::trunc);
41  if( !outdata ) { // file couldn't be opened
42  cerr << "ERROR: file could not be opened" << endl;
43  exit(1);
44  }
45  outdata << "{\n \"EVENT\": {\n \"runid\":" << runNumber << ", \n \"evtid\": 1, \n \"time\": 0, \n \"timeStr\": \"2023-08-23, 15:23:30 EST\", \n \"type\": \"Cosmics\", \n \"s_nn\": 0, \n \"B\": 0.0,\n \"pv\": [0,0,0],\n \"runstats\": [ \n \"sPHENIX Time Projection Chamber\", \"2023-08-23, Run 25926 - Event 1, Clusters\", \"Cosmics Data\"] \n },\n" << endl;
46 
47  outdata << " \"META\": {\n \"HITS\": {\n \"INNERTRACKER\": {\n \"type\": \"3D\",\n \"options\": {\n \"size\": 2,\n \"color\": 16777215\n } \n },\n" << endl;
48  outdata << " \"TRACKHITS\": {\n \"type\": \"3D\",\n \"options\": {\n \"size\": 2,\n \"transparent\": 0.5,\n \"color\": 16777215\n } \n },\n" << endl;
49  outdata << " \"JETS\": {\n \"type\": \"JET\",\n \"options\": {\n \"rmin\": 0,\n \"rmax\": 78,\n \"emin\": 0,\n \"emax\": 30,\n \"color\": 16777215,\n \"transparent\": 0.5 \n }\n }\n }\n }\n," << endl;
50  outdata << " \"HITS\": {\n \"CEMC\":[{\"eta\": 0, \"phi\": 0, \"e\": 0}\n ],\n \"HCALIN\": [{\"eta\": 0, \"phi\": 0, \"e\": 0}\n ],\n \"HCALOUT\": [{\"eta\": 0, \"phi\": 0, \"e\": 0}\n \n ],\n\n" << endl;
51  outdata << " \"TRACKHITS\": [\n\n ";
52 
53  for(int i = 0; i < hitTree->GetEntries(); i++)
54  {
55  hitTree->GetEntry(i);
56 
57  if (event != currentEvent)
58  {
59  if (event == currentEvent + 1)
60  {
61  outdata << "],\n \"JETS\": [\n ]\n }," << endl;
62  outdata << "\"TRACKS\": {" << endl;
63  outdata <<"\""<<"INNERTRACKER"<<"\": [";
64  outdata << "]" << endl;
65  outdata << "}" << endl;
66  outdata << "}" << endl;
67  outdata.close();
68 
69  currentEvent = event;
70  firstHit = true;
71  if (currentEvent >= nEventsToRun) break;
72 
73  outdata.open((outfile+"_event"+std::to_string(currentEvent)+".json").c_str(),std::ofstream::out | std::ofstream::trunc);
74  if( !outdata )
75  { // file couldn't be opened
76  cerr << "ERROR: file could not be opened" << endl;
77  exit(1);
78  }
79  outdata << "{\n \"EVENT\": {\n \"runid\":" << runNumber << ", \n \"evtid\": 1, \n \"time\": 0, \n \"timeStr\": \"2023-08-23, 15:23:30 EST\", \n \"type\": \"Cosmics\", \n \"s_nn\": 0, \n \"B\": 0.0,\n \"pv\": [0,0,0],\n \"runstats\": [ \n \"sPHENIX Time Projection Chamber\", \"2023-08-23, Run 25926 - Event" << currentEvent+1 << ", Clusters\", \"Cosmics Data\"] \n },\n" << endl;
80 
81  outdata << " \"META\": {\n \"HITS\": {\n \"INNERTRACKER\": {\n \"type\": \"3D\",\n \"options\": {\n \"size\": 2,\n \"color\": 16777215\n } \n },\n" << endl;
82  outdata << " \"TRACKHITS\": {\n \"type\": \"3D\",\n \"options\": {\n \"size\": 2,\n \"transparent\": 0.5,\n \"color\": 16777215\n } \n },\n" << endl;
83  outdata << " \"JETS\": {\n \"type\": \"JET\",\n \"options\": {\n \"rmin\": 0,\n \"rmax\": 78,\n \"emin\": 0,\n \"emax\": 30,\n \"color\": 16777215,\n \"transparent\": 0.5 \n }\n }\n }\n }\n," << endl;
84  outdata << " \"HITS\": {\n \"CEMC\":[{\"eta\": 0, \"phi\": 0, \"e\": 0}\n ],\n \"HCALIN\": [{\"eta\": 0, \"phi\": 0, \"e\": 0}\n ],\n \"HCALOUT\": [{\"eta\": 0, \"phi\": 0, \"e\": 0}\n \n ],\n\n" << endl;
85  outdata << " \"TRACKHITS\": [\n\n ";
86  }
87  else
88  {
89  std::cout << "Error: hit for a previous event placed later in the ntuple" << std::endl;
90  break;
91  }
92  }
93 
94  stringstream spts;
95 
96  if (firstHit) firstHit = false;
97  else spts << ",";
98 
99  spts << "{ \"x\": ";
100  spts << x;
101  spts << ", \"y\": ";
102  spts << y;
103  spts << ", \"z\": ";
104  spts << z;
105  spts << ", \"e\": ";
106  spts << adc;
107  spts << "}";
108 
109  outdata << (boost::format("%1%") % spts.str());
110  spts.clear();
111  spts.str("");
112  }
113  std::cout << "Done" << std::endl;
114 }