Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TPC_Channel_QA.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TPC_Channel_QA.C
1 #include <cstdlib>
2 #include <iostream>
3 #include <map>
4 #include <string>
5 #include <vector>
6 
7 #include "TChain.h"
8 #include "TFile.h"
9 #include "TTree.h"
10 #include "TString.h"
11 #include "TObjString.h"
12 #include "TSystem.h"
13 #include "TROOT.h"
14 #include "TTreeReader.h"
15 #include "TTreeReaderValue.h"
16 #include "TTreeReaderArray.h"
17 
19  gROOT->SetBatch(kTRUE);
20  std::ofstream outdata;
21  outdata.open("noisyChannels.txt");
22  if( !outdata ) { // file couldn't be opened
23  cerr << "Error: file could not be opened" << endl;
24  exit(1);
25  }
26  for (int q = 0; q < 24; q++){
27  string sectorNumber;
28  if (q < 10) sectorNumber = "0"+std::to_string(q);
29  else sectorNumber = std::to_string(q);
30 
31  string runNumber = "beam-00026540";
32 
33  string fileName = "/sphenix/user/jamesj3j3/tpc/sPHENIXProjects/pedestal-run-26540/TPC_ebdc"+sectorNumber+"_"+runNumber+"-0000.prdf_TPCRawDataTree_skip100.root";
34  // string fileName = "/sphenix/user/jamesj3j3/tpc/sPHENIXProjects/pedestal-run-26540/TPC_ebdc02_"+runNumber+"-0000.prdf_TPCRawDataTree_skip100.root";
35 
36 // string mod[26] = {"R2","R2","R1","R1","R1","R3","R3","R3","R3","R3","R3","R2","R2","R1","R2","R2","R1","R1","R2","R2","R3","R3","R3","R3","R3","R3"};
37 // string slot[26] = {"5","6","1","3","2","12","10","11","9","8","7","1","2","4","8","7","6","5","4","3","1","3","2","4","6","5"};
38  int mod_arr[26]={2,2,1,1,1,3,3,3,3,3,3,2,2,1,2,2,1,1,2,2,3,3,3,3,3,3};
39  int slot_arr[26] = {5,6,1,3,2,12,10,11,9,8,7,1,2,4,8,7,6,5,4,3,1,3,2,4,6,5};
40 
41  TFile *truthInput = TFile::Open(fileName.c_str());
42  TTreeReader myReader("SampleTree", truthInput);
43 
44  TTreeReaderValue<Int_t> nSamples_in(myReader, "nSamples");
45  TTreeReaderValue<Int_t> fee_in(myReader, "fee");
46  TTreeReaderArray<UShort_t> adcSamples_in(myReader, "adcSamples");
47  TTreeReaderValue<Int_t> Channel_in(myReader, "Channel");
48 
49 //Adding the output tree
50  TString* outputfilename=new TString("/sphenix/user/jamesj3j3/tpc/sPHENIXProjects/pedestal-run-26540/outputfile_TPC_ebdc"+sectorNumber+"_"+runNumber+"_skip100.root");
51  TFile* outputfile=new TFile(outputfilename->Data(),"recreate");
52  TTree* outputTree=new TTree("outputTree","outputTree");
53  Int_t isAlive=1.0; // 1 if the channel is working properly, 0 if no signal(number of samples is 0, adc value is 0 or nan), pedestal above 200 or below 10
54  Float_t pedMean; // average pedestal value over all samples
55  Float_t pedStdi; // pedestal standard deviation over all samples
56  Int_t chan; // channel number
57  Int_t fee; // fee number
58  Int_t module; // module number (e.g. R1, R2, R3)
59  Int_t slot; // slot number
60  outputTree->Branch("isAlive",&isAlive,"isAlive/I");
61  outputTree->Branch("pedMean",&pedMean,"pedMean/F");
62  outputTree->Branch("pedStdi",&pedStdi,"pedStdi/F");
63  outputTree->Branch("chan",&chan,"chan/I");
64  outputTree->Branch("fee",&fee,"fee/I");
65  outputTree->Branch("module",&module,"module/I");
66  outputTree->Branch("slot",&slot,"slot/I");
67 
68 
69 
70  string dChan = "Dead Channels (Sector "+sectorNumber+", Run "+runNumber+");Channel Number + 256*FEE Number;Instances";
71 
72  //TH1F *ave_adc = new TH1F("ave_adc","Channel Pedestals;Channel Number + 256*FEE Number;Pedestal ADC",256*26,-0.5,255.5+256*25);
73  TH1F *dead_channel = new TH1F("dead_channel",dChan.c_str(),256*26,-0.5,255.5+256*25);
74  //TH1F *rms_adc = new TH1F("rms_adc","Pedestal RMS;Channel Number + 256*FEE Number;Pedestal RMS",256*26,-0.5,255.5+256*25);
75 
76  std::vector<float> channels;
77  std::vector<float> pedestal;
78  std::vector<float> std_data;
79 
80  Float_t ave_adc_fee_channel[26][256];
81  Float_t std_adc_fee_channel[26][256];
82  Float_t counts_adc_fee_channel[26][256];
83  Int_t alive_array_fee_channel[26][256];
84  for(int fee_no=0;fee_no<26;fee_no++){
85  for(int channel_no=0;channel_no<256;channel_no++)
86  {
87  ave_adc_fee_channel[fee_no][channel_no]=0.0;
88  std_adc_fee_channel[fee_no][channel_no]=0.0;
89  counts_adc_fee_channel[fee_no][channel_no]=0.0;
90  alive_array_fee_channel[fee_no][channel_no]=1;
91  }
92  }
93 
94  //Int_t channels[256];
95  while(myReader.Next())
96  {
97  //cout<<"fee="<<*fee<<"Channel="<<*Channel<<endl;
98  if(*nSamples_in==0)
99  {
100  dead_channel->Fill(*Channel_in+ 256*(*fee_in));
101  alive_array_fee_channel[*fee_in][*Channel_in]=0;
102  continue;
103  }
104 
105  bool dead = false;
106  for(int adc_sam_no=0;adc_sam_no<*nSamples_in;adc_sam_no++){
107  //if (*fee_in == 25) channels[*Channel_in] += adcSamples_in[adc_sam_no];
108  if (adcSamples_in[adc_sam_no] == 0 || TMath::IsNaN(float(adcSamples_in[adc_sam_no]))) {
109  dead_channel->Fill(*Channel_in+ 256*(*fee_in));dead=true;break;
110  alive_array_fee_channel[*fee_in][*Channel_in]=0;
111  }
112  }
113  if (dead) {continue;}
114 
115  for(int adc_sam_no=0;adc_sam_no<*nSamples_in;adc_sam_no++){
116  ave_adc_fee_channel[*fee_in][*Channel_in]+=adcSamples_in[adc_sam_no];
117  std_adc_fee_channel[*fee_in][*Channel_in]+=pow(adcSamples_in[adc_sam_no],2);
118  counts_adc_fee_channel[*fee_in][*Channel_in]+=1.0;
119  }
120 
121  }
122 
123  for(int fee_no=0;fee_no<26;fee_no++){
124  for(int channel_no=0;channel_no<256;channel_no++){
125  if(counts_adc_fee_channel[fee_no][channel_no] != 0.0)
126  {
127  float temp1 = ave_adc_fee_channel[fee_no][channel_no]/counts_adc_fee_channel[fee_no][channel_no];
128  float temp2 = std_adc_fee_channel[fee_no][channel_no]/counts_adc_fee_channel[fee_no][channel_no];
129  ave_adc_fee_channel[fee_no][channel_no] = temp1;
130  std_adc_fee_channel[fee_no][channel_no] = temp2;
131  }
132  else
133  {
134  dead_channel->Fill(channel_no+ 256*(fee_no));
135  //std::cout << "FEE: " << fee_no << ", channel: " << channel_no << std::endl;
136  ave_adc_fee_channel[fee_no][channel_no] = 0.0;
137  std_adc_fee_channel[fee_no][channel_no] = 0.0;
138  alive_array_fee_channel[fee_no][channel_no]=0;
139  }
140 
141  if(sqrt(std_adc_fee_channel[fee_no][channel_no] - pow(ave_adc_fee_channel[fee_no][channel_no],2)) > 4)
142  {
143  outdata << "Sector " << q << ", FEE " << fee_no << ", Channel " << channel_no << std::endl;
144  }
145  if(ave_adc_fee_channel[fee_no][channel_no] > 200)
146  {
147  dead_channel->Fill(channel_no+ 256*(fee_no));
148  std::cout << "(Large Pedestal pedestal>200) FEE: " << fee_no << ", channel: " << channel_no << ", " << ave_adc_fee_channel[fee_no][channel_no] << std::endl;
149  alive_array_fee_channel[fee_no][channel_no]=0;
150  }
151  else if(ave_adc_fee_channel[fee_no][channel_no] < 10)
152  {
153  dead_channel->Fill(channel_no+ 256*(fee_no));
154  std::cout << "(Small Pedestal pedestal<10) FEE: " << fee_no << ", channel: " << channel_no << ", " << ave_adc_fee_channel[fee_no][channel_no] << std::endl;
155  alive_array_fee_channel[fee_no][channel_no]=0;
156  }
157  channels.push_back(channel_no+256.*(fee_no));
158  pedestal.push_back(ave_adc_fee_channel[fee_no][channel_no]);
159  std_data.push_back(sqrt(std_adc_fee_channel[fee_no][channel_no] - pow(ave_adc_fee_channel[fee_no][channel_no],2)));
160  //ave_adc->SetBinContent(channel_no*(fee_no+1),ave_adc_fee_channel[fee_no][channel_no]);
161  //std::cout << ave_adc_fee_channel[fee_no][channel_no] << std::endl;
162 
163  pedMean=ave_adc_fee_channel[fee_no][channel_no];
164  pedStdi=sqrt(std_adc_fee_channel[fee_no][channel_no] - pow(ave_adc_fee_channel[fee_no][channel_no],2));
165  isAlive=alive_array_fee_channel[fee_no][channel_no];
166  chan = channel_no;
167  //if(*nSamples_in==0 || pedMean_o>200 || pedMean_o<10){isAlive=false};
168  fee=fee_no;
169  module=mod_arr[fee_no];
170  slot=slot_arr[fee_no];
171  outputTree->Fill();
172 
173 
174  }
175  }
176  //for (int l = 0; l < 256; l++)
177  //{
178  // std::cout << "Channel " << l << ": " << ave_adc_fee_channel[25][l] << std::endl;
179  //}
180 
181  TGraph *ave_adc = new TGraph(256*26,channels.data(),pedestal.data());
182  TGraph *rms_adc = new TGraph(256*26,channels.data(),std_data.data());
183 
184  TCanvas *c1 = new TCanvas("c1","c1",2000,1200);
185  c1->Divide(2,2);
186  c1->cd(1);
187  auto tl = new TLine(); tl->SetLineColor(kRed);
188  dead_channel->Draw();
189  for (int k = 0; k < 26; k++)
190  {
191  tl->DrawLine(256*(k+1),0,256*(k+1),dead_channel->GetMaximum());
192  TLatex *pt = new TLatex(25+k*256,dead_channel->GetMaximum()*5/6,std::to_string(k).c_str());
193  pt->SetTextSize(0.04);
194  pt->Draw();
195  //TLatex *pt2 = new TLatex(25+k*256,dead_channel->GetMaximum()*7.5/10,mod[k].c_str());
196  //pt2->SetTextSize(0.03);
197  //pt2->Draw();
198  //TLatex *pt3 = new TLatex(25+k*256,dead_channel->GetMaximum()*4/6,slot[k].c_str());
199  //pt3->SetTextSize(0.04);
200  //pt3->Draw();
201  }
202  c1->cd(2);
203  ave_adc->SetTitle("Mean Pedestal Value;Channel Number + 256*FEE Number;Mean Pedestal ADC");
204  ave_adc->SetMarkerStyle(20);
205  ave_adc->SetMarkerColor(kBlue);
206  ave_adc->SetMarkerSize(0.25);
207  ave_adc->GetXaxis()->SetRangeUser(0,256*26);
208  ave_adc->GetYaxis()->SetRangeUser(0,150);
209  ave_adc->Draw("AP");
210  for (int k = 0; k < 26; k++)
211  {
212  tl->DrawLine(256*(k+1),0,256*(k+1),150);
213  TLatex *pt = new TLatex(25+k*256,135,std::to_string(k).c_str());
214  pt->SetTextSize(0.04);
215  pt->Draw();
216  /*
217  //TLatex *pt2 = new TLatex(25+k*256,125,mod[k].c_str());
218  //pt2->SetTextSize(0.03);
219  //pt2->Draw();
220  TLatex *pt3 = new TLatex(25+k*256,115,slot[k].c_str());
221  pt3->SetTextSize(0.04);
222  pt3->Draw();
223  */
224  }
225  c1->cd(3);
226  rms_adc->SetTitle("Pedestal Standard Deviation;Channel Number + 256*FEE Number;Pedestal Standard Deviation");
227  rms_adc->SetMarkerStyle(20);
228  rms_adc->SetMarkerColor(kBlue);
229  rms_adc->SetMarkerSize(0.25);
230  rms_adc->GetXaxis()->SetRangeUser(0,256*26);
231  rms_adc->GetYaxis()->SetRangeUser(0,15);
232  rms_adc->Draw("AP");
233  for (int k = 0; k < 26; k++)
234  {
235  tl->DrawLine(256*(k+1),0,256*(k+1),15);
236  TLatex *pt = new TLatex(25+k*256,13.5,std::to_string(k).c_str());
237  pt->SetTextSize(0.04);
238  pt->Draw();
239  //TLatex *pt2 = new TLatex(25+k*256,12.5,mod[k].c_str());
240  //pt2->SetTextSize(0.03);
241  //pt2->Draw();
242  //TLatex *pt3 = new TLatex(25+k*256,11.5,slot[k].c_str());
243  //pt3->SetTextSize(0.04);
244  //pt3->Draw();
245  }
246 
247  string saveName = "images/S"+sectorNumber+"-"+runNumber+".png";
248  c1->SaveAs(saveName.c_str());
249  c1->Close();
250  outputfile->cd();
251  outputTree->Write();
252  outputfile->Close();
253  }
254  outdata.close();
255 }