Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
evDisplay.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file evDisplay.C
1 #include <iomanip>
2 #include <algorithm>
3 #include <numeric>
4 #include <cmath>
5 #include <ctime>
6 
7 #include <iostream>
8 #include <fstream>
9 #include <cstdlib>
10 #include <stdint.h>
11 
12 #include <fileEventiterator.h>
13 #include <Event.h>
14 #include <packet.h>
15 #include <packet_hbd_fpgashort.h>
16 #include <TROOT.h>
17 #include <TF1.h>
18 #include <TH1.h>
19 #include <TH1F.h>
20 #include <TCanvas.h>
21 #include <TGraph.h>
22 #include <TString.h>
23 #include <TApplication.h>
24 #include <TStyle.h>
25 #include <TSystem.h>
26 #include <TGClient.h>
27 #include <TGWindow.h>
28 
29 using namespace std;
30 
31 //TROOT root("xxx","if you want to make a root app you can");
32 
33 // -----------------------------------------------------------------------------------------------
34 
35 //const int NCH = 2*8;
36 const int NCH = 8; // HG channels only
37 //const int NCH = 12;
38 const int NSAMPLES = 24;
39 const int RISETIME = 4;
40 const int FALLTIME = 5;
41 //const int feech[] = { 99, 98, 97, 96, 106, 107, 108, 109, 112, 113, 122, 123, 124, 125 };
42 //const int feech[] = { 115, 114, 113, 112, 119, 118, 117, 116, 123, 122, 121, 120, 127, 126, 125, 124 };
43 //const int feech[] = { 115, 113, 119, 117, 123, 121, 127, 125 }; // HG channels only// ORDERING : RIGHT/LEFT ARE TWO ENDS OF THE SAME FIBER
44 const int feech[] = { 115, 119, 123, 127, 113, 117, 121, 125 }; // HG channels only
45 
46 TGraph * gpulse[NCH];
47 Double_t shapeD[NCH][NSAMPLES];
48 TF1 * shapes[NCH];
49 TCanvas * ev_display, *fitFunc;
51 TH1 ** fitPar[6];
52 int nevents(0); // event counter
53 int channel(0);
54 
55 
56 // -----------------------------------------------------------------------------------------------
57 
58 void usage()
59 {
60  cout << "evDisplay <prdf>" << endl;
61 }
62 // -----------------------------------------------------------------------------------------------
63 // Unipolar pulse (0.3164*pow(x,4)*exp(-x*1.5) - Unity integral, peak ~1/3 of integral)
64 Double_t PEDESTAL = 2048;
65 Double_t par0[] = { 0., 0., 4., 0., 0., 0.};
66 Double_t par0Max[] = { 1000., NSAMPLES-FALLTIME+1., 5., 1., 2150., 0.2};
67 Double_t par0Min[] = { 1., RISETIME-1, 3., 2., 1950.,-0.2};
68 Double_t signal(0.), pedestal(0.);
69 // -----------------------------------------------------------------------------------------------
70 Double_t signalShape(Double_t *x, Double_t * par){
71  // if(x[0]<par[1]) return PEDESTAL;
72  pedestal = par[4]+x[0]*par[5];
73  if(x[0]<par[1]) return pedestal;
74  // signal contribution
75  signal = par[0]*pow((x[0]-par[1]),par[2])*exp(-(x[0]-par[1])*par[3]);
76  // cout<<"EVENT "<<nevents<<" CHANNEL "<<channel<<" X "<<x[0]<<" signal "<<signal<<" pedestal "<<pedestal<<endl;
77  return signal+pedestal;
78 }
79 // -----------------------------------------------------------------------------------------------
80 
81 void init(){
82  fittime = new TH1F("fittime", "Fitted pulse peak time", 100, 0., 24.);
83  fitmax = new TH1F("fitmax", "Fitted pulse amplitude", 500, 0., 5000.);
84  fitwidth = new TH1F("fitwidth", "Fitted pulse width", 100, 0., 5.);
85  pedval = new TH1F("pedval", "Pedestal at t==0", 100, 2000.,2100.);
86  pedslope = new TH1F("pedslope", "Pedestal slope", 100, -1., 1.);
87  fitintegral = new TH1F("fitintegral","Integral of fitted curve (pedestal suppressed)", 100, 0., 10000.);
88  // fit parameters (for templating)
89 
90 
91 }
92 // -----------------------------------------------------------------------------------------------
93 
94 void fitShape(int chan){
95  channel = chan;
96  TString fitName = feech[chan]%2? "HG_" : "LG_";
97  fitName += chan;
98  fitName += "(";
99  fitName += feech[chan];
100  fitName += ")";
101  shapes[chan]= (TF1*) (gROOT->FindObject("fitName"));
102  // zero approximation to fit parameters
103  // use pulse data to find location of maximum
104  if(shapes[chan]) delete shapes[chan];
105  shapes[chan] = new TF1(fitName, &signalShape, 0., 24., 6);
106  shapes[chan]->SetLineColor(2);
107  int peakPos = 0;
108  Double_t peakVal = 0.;
109  for (int iSample=0; iSample<NSAMPLES; iSample++) {
110  if(shapeD[chan][iSample]>peakVal) {
111  peakVal = shapeD[chan][iSample];
112  peakPos = iSample;
113  }
114  }
115  peakVal -= PEDESTAL;
116  if(peakVal<0.) peakVal = 0.;
117  par0[0] = peakVal/3.;
118  par0[1] = peakPos-RISETIME;
119  if(par0[1]<0.) par0[1] = 0.;
120  par0[2] = 4.;
121  par0[3] = 1.5;
122  par0[4] = PEDESTAL; // we do not do pedestal subtrastion at this time
123  par0[5] = 0; // slope of the pedestals is initially set to "0"
124  shapes[chan]->SetParameters(par0);
125  for(int parn=0; parn<6; parn++) shapes[chan]->SetParLimits(parn, par0Min[parn], par0Max[parn]);
126  // fitFunc->cd(chan+1);
127  // shapes[chan]->Draw();
128  // gPad->Modified();
129  gpulse[chan]->Fit(shapes[chan], "", "RQ", 0., (Double_t)NSAMPLES);
130  Double_t fMax = shapes[chan]->GetMaximum(par0Min[1], par0Max[1]);
131  Double_t fPos = shapes[chan]->GetMaximumX(par0Min[1], par0Max[1]);
132 
133 
134  cout<<"EVENT "<<nevents<<" SHAPE "<<(char*)(shapes[chan]->GetName())<<" peakVal "<<peakVal<<" peakPos "<<peakPos<<" fitMax "<<fMax<<" fitPos "<<fPos<<endl;
135  ev_display->cd(chan+1);
136  shapes[channel]->Draw("same");
137  // gPad->Modified();
138 
139 
140 }
141 
142 // -----------------------------------------------------------------------------------------------
143 
144 int main(int argc, char **argv)
145 {
146  if ( argc!=2 )
147  {
148  usage();
149  exit(-1);
150  }
151 
152  TString prdfname = argv[1]; // get prdf file name
153  TString basename = prdfname;
154  basename.ReplaceAll(".prdf","");
155  Ssiz_t lastslash = basename.Last('/');
156  basename.Remove(0,lastslash+1);
157  //cout << basename << endl;
158  basename.ReplaceAll("rc-","");
159  //cout << basename << endl;
160 
161  char *file = argv[1]; // get prdf file name
162  TApplication theApp("theApp",&argc,argv);
163 
164  // gROOT->SetStyle("Plain");
165 /*
166  gStyle->SetLabelSize(0.1,"xyz");
167  gStyle->SetTitleSize(0.08,"xyz");
168  gStyle->SetStatH(0.4);
169 */
170 
171  //TCanvas *ev_display = new TCanvas("sphenix","sphenix display",800,800);
172  // This version of plotting is for High/Low gain preamplifier sending its outputs into two sequential readout channels
173  int nx_c = 2;
174  int ny_c = NCH/2+NCH%2;
175  ev_display = new TCanvas("sphenix","sphenix display",400*nx_c,200*ny_c);
176 
177  // fitFunc = new TCanvas("fitFunc","Fit Functions", 400*nx_c,200*ny_c);
178  ev_display->SetEditable(kTRUE);
179  // ev_display->Divide(nx_c,ny_c);
180  // ev_display->SetBit(TPad::kClearAfterCR);
181  // fitFunc->SetEditable(kTRUE);
182  // fitFunc->Divide(nx_c,ny_c);
183 
184  TString name, title;
185  for (int ich=0; ich<NCH; ich++)
186  {
187  gpulse[ich] = new TGraph(NSAMPLES);
188  name = "gch"; name += ich;
189  gpulse[ich]->SetName(name);
190  gpulse[ich]->SetMarkerStyle(20);
191  gpulse[ich]->SetMarkerSize(0.4);
192  // gpulse[ich]->SetMarkerColor(ich+1);
193  // gpulse[ich]->SetLineColor(ich+1);
194  }
195 
196  // Open up PRDFF
197  int status;
198 
199  Eventiterator *it = new fileEventiterator(file, status);
200  if (status)
201  {
202  cout << "Couldn't open input file " << file << endl;
203  delete(it);
204  exit(1);
205  }
206 
207  Event *evt;
208 
209  // Loop over events
210  while ( (evt = it->getNextEvent()) != 0 )
211  {
212  int eventseq = evt->getEvtSequence();
213 
214  if ( evt->getEvtType() != 1 ) {
215  cout << "eventseq " << eventseq << " event type = " << evt->getEvtType() << endl;
216  continue;
217  }
218  ev_display->Divide(nx_c,ny_c);
219  ev_display->SetBit(TPad::kClearAfterCR);
220 
221  // Get sPHENIX Packet
222  Packet_hbd_fpgashort *spacket = dynamic_cast<Packet_hbd_fpgashort*>( evt->getPacket(21101) );
223  if ( spacket )
224  {
225  spacket->setNumSamples( NSAMPLES );
226  int nmod_per_fem = spacket->iValue(0,"NRMODULES");
227  //cout << "nmod_per_fem " << nmod_per_fem << endl;
228  cout <<"EVENT "<<eventseq << " modules per FEM "<<nmod_per_fem<<endl;
229  nevents++;
230  for (int ich=0; ich<NCH; ich++)
231  {
232  channel = ich;
233  for (int isamp=0; isamp<NSAMPLES; isamp++)
234  {
235  Short_t val = spacket->iValue(feech[ich],isamp);
236  gpulse[ich]->SetPoint(isamp,(Double_t)isamp,(Double_t)val);
237  shapeD[ich][isamp] = (Double_t)val;
238  }
239  ev_display->cd(ich+1);
240  //gpulse[ich]->SetMaximum(4095);
241  //gpulse[ich]->SetMinimum(0);
242  // if(nevents==1) gpulse[ich]->Draw("acp");
243  gpulse[ich]->Draw("acp");
244  // gPad ->Modified();
245  fitShape(ich);
246  // gPad ->Modified();
247  // shapes[ich]->Draw("same");
248  // gPad ->Update();
249  }
250 // for (int ich=0; ich<NCH; ich++)
251 // {
252 // channel = ich;
253 // fitFunc ->cd(ich+1);
254 // shapes[ich]->Draw();
255 // }
256 // gPad ->Modified();
257 
258 
259  cout<<"Updating canvas "<<endl;
260 
261  ev_display->Update();
262  // fitFunc ->Update();
263  // name = basename; name += "_"; name += eventseq; name += ".png";
264  // ev_display->SaveAs(name); for(int loop=0; loop<1e+6; loop++) continue;
265  // TGWindow * mw = (TGWindow *)(gClient->GetRoot());
266  // gClient->WaitFor(mw);
267 // if ( eventseq > 10 )
268 // {
269  // string junk;
270  // cout << "? ";
271  // cin >> junk;
272  // if(junk=="q") goto ALLDONE;
273 // }
274 
275  delete spacket;
276  ev_display->Clear();
277  }
278 
279  delete evt;
280  //if (nevents++==10) break;
281  }
282 
283 
284  ALLDONE:
285  cout<<"ALLDONE"<<endl;
286  delete it;
287 
288  // theApp.Run();
289  exit(0);
290 
291 }
292