File indexing completed on 2025-01-18 09:15:47
0001 #include <TCanvas.h>
0002 #include <TH1F.h>
0003 #include <TF1.h>
0004 #include <TRandom3.h>
0005
0006 const int gMaxChannels = 64;
0007 const int gMaxBoard = 1;
0008
0009 Long64_t gTrID;
0010 Double_t gTRtimeStamp;
0011 Long64_t* gBoard = new Long64_t[gMaxChannels];
0012 Long64_t* gChannel = new Long64_t[gMaxChannels];
0013 Long64_t* gLG = new Long64_t[gMaxChannels];
0014 Long64_t* gHG = new Long64_t[gMaxChannels];
0015 void SetBranchAddressesTree(TTree* inputTree){
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 if (inputTree->GetBranchStatus("t_stamp") ){
0032 inputTree->SetBranchAddress("trgid", &gTrID);
0033 inputTree->SetBranchAddress("t_stamp", &gTRtimeStamp);
0034 inputTree->SetBranchAddress("board", gBoard);
0035 inputTree->SetBranchAddress("channel", gChannel);
0036 inputTree->SetBranchAddress("LG", gLG);
0037 inputTree->SetBranchAddress("HG", gHG);
0038 }
0039 }
0040
0041 void Gauss(const char* noisefile, const char* datafile, int runnumber, TString outputDir = "NoiseSubData/MIP/")
0042 {
0043
0044
0045 outputDir = Form("%s/Run%05d",outputDir.Data(), runnumber );
0046 gSystem->Exec("mkdir -p "+outputDir);
0047
0048 TCanvas *c1 = new TCanvas("c1", "Gaussian Fit Example", 800, 600);
0049
0050
0051
0052 TFile* fnoise = new TFile(noisefile,"READ");
0053
0054 TFile* fdata = new TFile(datafile,"READ");
0055
0056
0057 TH1F* nhistLG[gMaxBoard][gMaxChannels];
0058 TH1F* nhistHG[gMaxBoard][gMaxChannels];
0059
0060 TH1F* histLG[gMaxBoard][gMaxChannels];
0061 TH1F* histHG[gMaxBoard][gMaxChannels];
0062
0063
0064 TH1F* nfithistLG[gMaxBoard][gMaxChannels];
0065 TH1F* nfithistHG[gMaxBoard][gMaxChannels];
0066
0067
0068 TH1F* shistLG[gMaxBoard][gMaxChannels];
0069 TH1F* shistHG[gMaxBoard][gMaxChannels];
0070
0071 const int rows = gMaxBoard;
0072 const int cols = gMaxChannels;
0073 std::vector<std::vector<double>> gauFitSigma(rows, std::vector<double>(cols));
0074 std::vector<std::vector<double>> gauFitMean(rows, std::vector<double>(cols));
0075
0076 std::vector<std::vector<double>> gauFitSigmaLG(rows, std::vector<double>(cols));
0077 std::vector<std::vector<double>> gauFitMeanLG(rows, std::vector<double>(cols));
0078
0079 TF1* gauFit[gMaxBoard][gMaxChannels];
0080
0081
0082
0083 for (Int_t j = 0; j < gMaxBoard; j++)
0084 {
0085 for (Int_t i = 0; i < gMaxChannels; i++)
0086 {
0087
0088 nfithistHG[j][i] = new TH1F(Form("hfit_HG_B%d_C%02d",j,i),"",4001, 0, 4001);
0089 nfithistLG[j][i] = new TH1F(Form("hfit_LG_B%d_C%02d",j,i),"",4001, 0, 4001);
0090
0091 histHG[j][i] = new TH1F(Form("HG_TriggB%d_C%02d",j,i),"",4001,0,4001);
0092 histHG[j][i]->RebinX(2);
0093 histLG[j][i] = new TH1F(Form("LG_B%d_C%02d",j,i),"",4001,0,4001);
0094
0095 shistHG[j][i] = new TH1F(Form("sh_HG_B%d_C%02d",j,i),"",4001,0,4001);
0096 shistHG[j][i]->RebinX(2);
0097 shistLG[j][i] = new TH1F(Form("sh_LG_B%d_C%02d",j,i),"",4001,0,4001);
0098
0099 }
0100 }
0101
0102 TRandom3 randomGenerator;
0103
0104 for (Int_t j = 0; j < gMaxBoard; j++){
0105 for (Int_t i = 0; i < gMaxChannels; i++){
0106
0107 nhistHG[j][i] = (TH1F*)fnoise->Get(Form("h_HGNoise_B%d_C%02d",j,i));
0108 nhistHG[j][i]->RebinX(2);
0109
0110
0111 cout<<nhistHG[j][i]->GetName()<<endl;
0112 cout<<nhistHG[j][i]->GetEntries()<<endl;
0113 cout<<nhistHG[j][i]->GetMaximum()<<endl;
0114
0115 TF1 *gaussianFit = new TF1("gaussianFit", "gaus");
0116
0117
0118 gaussianFit->SetParameter(0, nhistHG[j][i]->GetMaximum());
0119 gaussianFit->SetParameter(1, nhistHG[j][i]->GetMean());
0120 gaussianFit->SetParameter(2, nhistHG[j][i]->GetStdDev());
0121 nhistHG[j][i]->Fit("gaussianFit", "R","",0,100);
0122
0123
0124 double_t amplitude = gaussianFit->GetParameter(0);
0125 double_t mean = gaussianFit->GetParameter(1);
0126 double_t sigma = gaussianFit->GetParameter(2);
0127
0128
0129 gauFitMean[j][i] = mean;
0130 gauFitSigma[j][i] = sigma;
0131 gauFit[j][i]= gaussianFit;
0132
0133
0134 }
0135 }
0136
0137
0138
0139
0140 TTree *tt_event = (TTree*)fdata->Get("tree");
0141 SetBranchAddressesTree(tt_event);
0142 double nEntriesTree = tt_event->GetEntries();
0143 Long64_t startEvent = 0;
0144 Long64_t nEventsProcessed=0;
0145 for (Long64_t i=startEvent; i<nEntriesTree;i++)
0146 {
0147
0148 tt_event->GetEntry(i);
0149 nEventsProcessed++;
0150 for(Int_t ch=0; ch<gMaxChannels; ch++)
0151 {
0152
0153 histHG[gBoard[ch]][gChannel[ch]]->Fill(gHG[ch]);
0154
0155 shistHG[gBoard[ch]][gChannel[ch]]->Fill((gHG[ch]-randomGenerator.Gaus(gauFitMean[gBoard[ch]][gChannel[ch]],gauFitSigma[gBoard[ch]][gChannel[ch]])));
0156
0157
0158
0159 }
0160
0161 }
0162
0163
0164
0165
0166
0167 TLegend *legend_dat = new TLegend(0.60,0.60,0.80,0.90);
0168 legend_dat->AddEntry(nhistHG[0][24],"Noise","l");
0169 legend_dat->AddEntry(shistHG[0][24],"Noise subtracted Triggered","l");
0170 legend_dat->SetBorderSize(0);
0171 nhistHG[0][24]->SetLineColor(kBlue);
0172 nhistHG[0][24]->GetXaxis()->SetTitle("ADC");
0173 nhistHG[0][24]->GetYaxis()->SetTitle("Entries");
0174 nhistHG[0][24]->Draw();
0175 shistHG[0][24]->SetLineColor(kGreen+4);
0176 shistHG[0][24]->Draw("SAME");
0177 legend_dat->Draw("SAME");
0178
0179
0180
0181
0182
0183 TFile* fileOutput = new TFile(Form("%s/%s",outputDir.Data(),datafile),"RECREATE");
0184 for (Int_t j = 0; j < gMaxBoard; j++)
0185 {
0186 for (Int_t i = 0; i < gMaxChannels; i++)
0187 {
0188 shistHG[j][i]->Write();
0189 shistLG[j][i]->Write();
0190
0191 }
0192 }
0193
0194 }
0195
0196
0197