dump_event.C
Go to the documentation of this file.
1 #include "TBox.h"
2 #include "TCanvas.h"
3 #include "TFile.h"
4 #include "TH2.h"
5 #include "TSystem.h"
6 #include "stdio.h"
7 #include "LEM/func/Library.h"
8 using namespace lem;
9 
10 #include <iostream>
11 
12 int evt_idx=0;
13 FILE* outfile=NULL;
14 
15 void draw_event(TCanvas* theCanvas, TH2F* h[2], const MatchableEvent& sum);
16 void printEventToJson(const MatchableEvent sum, FILE*, int idx=0);
17 
18 void dump_event(TString libPath, int idx = 0, const char* outfile_name=NULL, bool draw=true, bool flipEven = false, bool flipOdd = false, bool idxs = false)
19 {
20  static Library* lib = NULL;
21  static TCanvas* theCanvas = NULL;
22  static int views = 2;
23  static TH2F* h[2] = {NULL,NULL};
24  long long int index=idx;
25  evt_idx=idx;
26 
27  //FILE* outfile=NULL;
28 
29  if(outfile==NULL){
30  if(outfile_name!=NULL){
31  outfile = fopen(outfile_name,"w");
32  }else{
33  outfile = stdout;
34  }
35  }
36 
37  // Map the Library File if we haven't already mapped it
38  if(lib==NULL){
39  // Open the Library via Memory Map
40  lib = Library::FromMMap(libPath.Data(), false);
41  }
42 
43  // Create a Canvase for drawing the events
44  if(theCanvas==NULL){
45  theCanvas = new TCanvas("a", "b", 1200, 600);
46  theCanvas->Divide(2, 1);
47  }
48 
49  // Retrieve the template from the library
50  const MatchableEvent& sum = lib->Event(idx);
51 
52  printEventToJson(sum,outfile,index); // Write the event out in JSon
53 
54  if(draw){
55 
56  // Book the histograms for drawing the event or clear them if they exist
57  if(h[0]==NULL){
58  for(int i=0; i<2; ++i){
59  h[i] = new TH2F("", ";plane;cell", 128, 0, 128, 128, 64, 192);
60  } // endfor
61  }else{
62  for(int i=0; i<2; ++i){
63  // Clear/Reset the Histograms
64  h[i]->Reset();
65  } // endfor
66  } //endif
67 
68  // Fill the histograms (even view first, then the odd view)
69  sum.FillHists(h[0], h[1], flipEven, flipOdd, idxs);
70 
71  draw_event(theCanvas, h, sum); // Draw the Event in question
72  }
73 
74 };
75 
76 void draw_event(TCanvas* theCanvas, TH2F* h[2], const MatchableEvent& sum){
77  for(int v = 0; v < 2; ++v){
78  theCanvas->cd(v+1);
79  h[v]->Draw("colz");
80 
81  TBox* b = new TBox(lem::kVertexPlane, lem::kVertexCell,
83  b->SetLineColor(kRed);
84  b->SetFillStyle(0);
85  b->SetLineWidth(2);
86  b->SetLineStyle(7);
87  b->Draw();
88 
89  const int trueCell = (sum.trueVtxPlane%2 == v) ? sum.trueVtxCellOther : sum.trueVtxCell;
90  TBox* b2 = new TBox(sum.trueVtxPlane, trueCell,
91  sum.trueVtxPlane+1, trueCell+1);
92  b2->SetLineColor(kGreen+2);
93  b2->SetFillStyle(0);
94  b2->SetLineWidth(2);
95  b2->SetLineStyle(7);
96  b2->Draw();
97  }
98 }
99 
100 void printEventToJson(const MatchableEvent sum, FILE* theOutput, int index){
101 
102  // Print out the event's information
103  fprintf(theOutput,"{\"Event\": { \n\"EventID\": %lld,\n",index);
104  fprintf(theOutput," \"Truth\": {\n");
105  fprintf(theOutput," \"PE\": %2.2f ,\n" ,sum.totalPE);
106  fprintf(theOutput," \"VisE\": %2.2f ,\n" ,sum.trueEVis);
107  fprintf(theOutput," \"CC\": %d ,\n" ,sum.ccnc);
108  fprintf(theOutput," \"PDG\": %d ,\n" ,sum.pdg);
109  fprintf(theOutput," \"Mode\": %d ,\n" ,sum.mode);
110  fprintf(theOutput," \"PhotonL\": [ %2.2f, %2.2f ],\n",sum.photL0,sum.photL1);
111  fprintf(theOutput," \"PhotonE\": [ %2.2f, %2.2f ],\n",sum.photE0,sum.photE1);
112 
113  fprintf(theOutput," \"ParticlesPDG\": [ ");
114  for(int i=0; i< sum.nParts; ++i){
115  fprintf(theOutput,"%d,",sum.partPdgs[i]);
116  }
117  fprintf(theOutput," ]\n },\n");
118  fprintf(theOutput," \"NHits\": %d,\n",sum.nhits);
119  fprintf(theOutput," \"HitList\": [\n");
120  for(unsigned int i =0; i<sum.nhits; ++i){
121  if(i!=0){fprintf(theOutput,",\n");}
122  const int plane = sum.hits[i].Plane();
123  const int cell = sum.hits[i].Cell();
124  const int view = plane%2;
125  const float energy = sum.hits[i].pecorr;
126  fprintf(theOutput," [ %3d, %3d, %3d, %7.4f ]", plane, cell, view, energy);
127  }
128  fprintf(theOutput,"\n ]\n");
129  fprintf(theOutput," }\n}\n");
130 };
131 
132 void next_event(){
133  evt_idx++;
134  dump_event("",evt_idx);
135 };
136 
137 
138 void dump_lib(int start=1, int nEvts=1, const char* outfile_name=NULL, bool draw=false){
139 
140  // Close any open output files
141  if(outfile!=NULL){
142  fclose(outfile);
143  outfile==NULL;
144  }
145 
146  // Open a new file for output
147  if(outfile_name!=NULL){
148  outfile = fopen(outfile_name,"w");
149  }else{
150  outfile = stdout;
151  }
152 
153  fprintf(outfile,"{ \"Library\": [");
154  for(int i=start; i<(start+nEvts); ++i){
155  dump_event("",i,outfile_name,false);
156  if(i!=(start+nEvts-1)){
157  fprintf(outfile,",\n");
158  }
159  }
160  fprintf(outfile,"]\n }\n");
161  fflush(outfile);
162  fclose(outfile);
163  outfile=NULL;
164  return;
165 };
unsigned short nhits
Definition: EventSummary.h:53
void draw_event(TCanvas *theCanvas, TH2F *h[2], const MatchableEvent &sum)
Definition: dump_event.C:76
Attach some information used in matching to an EventSummary.
Collection of events for matching.
void dump_event(TString libPath, int idx=0, const char *outfile_name=NULL, bool draw=true, bool flipEven=false, bool flipOdd=false, bool idxs=false)
Definition: dump_event.C:18
Collection of events for matching.
Definition: Library.h:18
const int kVertexPlane
Definition: EventSummary.h:22
unsigned int nEvts
float pecorr
Definition: LiteHit.h:24
int Cell() const
Definition: LiteHit.h:39
Definition: __init__.py:1
PID
Definition: FillPIDs.h:14
fclose(fg1)
int Plane() const
Definition: LiteHit.h:38
void next_event()
Definition: dump_event.C:132
const int kVertexCell
Definition: EventSummary.h:23
double energy
Definition: plottest35.C:25
int evt_idx
Definition: dump_event.C:12
unsigned char nParts
Definition: EventSummary.h:67
const MatchableEvent & Event(int i) const
Definition: Library.h:32
const hit & b
Definition: hits.cxx:21
void dump_lib(int start=1, int nEvts=1, const char *outfile_name=NULL, bool draw=false)
Definition: dump_event.C:138
static Library * FromMMap(const std::string &libPath, bool touchAll)
Definition: Library.cxx:62
Double_t sum
Definition: plot.C:31
void printEventToJson(const MatchableEvent sum, FILE *, int idx=0)
Definition: dump_event.C:100
void FillHists(TH2 *h1, TH2 *h2, bool flipEven=false, bool flipOdd=false, bool idxs=false) const
FILE * outfile
Definition: dump_event.C:13