new file: FitHistogramsWithTSpectrum_Sequential_Improved.C

new file:   MatchAndPlotCentroids.C
	new file:   centroids.txt
	new file:   centroids_edited.txt
	new file:   slope_intercept_cathode.txt
	new file:   slope_intercept_results.txt
	new file:   slope_intercept_results_anode.txt
This commit is contained in:
Vignesh Sitaraman 2025-01-27 15:11:27 -05:00
parent a5dfa2ecd3
commit b99ad4e4d7
10 changed files with 600 additions and 23 deletions

View File

@ -102,7 +102,10 @@
"AutoFit.C": "cpp",
"Fitting.C": "cpp",
"PCGainMatch.C": "cpp",
"Analyzer1.C": "cpp"
"Analyzer1.C": "cpp",
"FitHistogramsWithTSpectrum_Sequential_Improved.C": "cpp",
"PlotAndFitCentroids.C": "cpp",
"MatchAndPlotCentroids.C": "cpp"
},
"github-enterprise.uri": "https://fsunuc.physics.fsu.edu"
}

View File

@ -1,23 +1,27 @@
#define Analyzer_cxx
#include "Analyzer.h"
#include "Armory/ClassSX3.h"
#include "Armory/ClassPW.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TMath.h>
#include "TVector3.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <map>
#include <utility>
#include <algorithm>
#include "Armory/ClassSX3.h"
#include "Armory/ClassPW.h"
#include "TVector3.h"
TH2F *hsx3IndexVE;
TH2F *hqqqIndexVE;
TH2F *hpcIndexVE;
TH2F *hpcIndexVE_GM;
TH2F *hsx3Coin;
TH2F *hqqqCoin;
TH2F *hpcCoin;
@ -37,6 +41,8 @@ SX3 sx3_contr;
PW pw_contr;
PW pwinstance;
TVector3 hitPos;
std::map<int, std::pair<double, double>> slopeInterceptMap;
bool HitNonZero;
TH1F *hZProj;
@ -49,8 +55,10 @@ void Analyzer::Begin(TTree * /*tree*/)
hsx3IndexVE->SetNdivisions(-612, "x");
hqqqIndexVE = new TH2F("hqqqIndexVE", "QQQ index vs Energy; QQQ index ; Energy", 4 * 2 * 16, 0, 4 * 2 * 16, 400, 0, 5000);
hqqqIndexVE->SetNdivisions(-1204, "x");
hpcIndexVE = new TH2F("hpcIndexVE", "PC index vs Energy; PC index ; Energy", 2 * 24, 0, 2 * 24, 400, 0, 4000);
hpcIndexVE = new TH2F("hpcIndexVE", "PC index vs Energy; PC index ; Energy", 2 * 24, 0, 2 * 24, 400, 0, 16000);
hpcIndexVE->SetNdivisions(-1204, "x");
hpcIndexVE_GM = new TH2F("hpcIndexVE_GM", "PC index vs Energy; PC index ; Energy", 2 * 24, 0, 2 * 24, 400, 0, 16000);
hpcIndexVE_GM->SetNdivisions(-1204, "x");
hsx3Coin = new TH2F("hsx3Coin", "SX3 Coincident", 24 * 12, 0, 24 * 12, 24 * 12, 0, 24 * 12);
hqqqCoin = new TH2F("hqqqCoin", "QQQ Coincident", 4 * 2 * 16, 0, 4 * 2 * 16, 4 * 2 * 16, 0, 4 * 2 * 16);
@ -77,7 +85,6 @@ void Analyzer::Begin(TTree * /*tree*/)
hanVScatsum = new TH2F("hanVScatsum", "Anode vs Cathode Sum; Anode E; Cathode E", 400, 0, 10000, 400, 0, 16000);
hAnodeMultiplicity = new TH1F("hAnodeMultiplicity", "Number of Anodes/Event", 40, 0, 40);
hanVScatsum = new TH2F("hanVScatsum", "Anode vs Cathode Sum; Anode E; Cathode E", 400, 0, 10000, 800, 0, 16000);
for (int i = 0; i < 24; i++)
{
TString histName = Form("hAnodeVsCathode_%d", i);
@ -86,6 +93,29 @@ void Analyzer::Begin(TTree * /*tree*/)
}
sx3_contr.ConstructGeo();
pw_contr.ConstructGeo();
std::ifstream inputFile("slope_intercept_results.txt");
if (inputFile.is_open())
{
std::string line;
int index;
double slope, intercept;
while (std::getline(inputFile, line))
{
std::stringstream ss(line);
ss >> index >> slope >> intercept;
if (index >= 0 && index <= 47)
{
slopeInterceptMap[index] = std::make_pair(slope, intercept);
}
}
inputFile.close();
}
else
{
std::cerr << "Error opening slope_intercept.txt" << std::endl;
}
}
Bool_t Analyzer::Process(Long64_t entry)
@ -308,6 +338,23 @@ Bool_t Analyzer::Process(Long64_t entry)
{
hpcCoin->Fill(pc.index[i], pc.index[j]);
}
// Gain Matching of PC wires
if (pc.index[i] >= 0 && pc.index[i] < 48)
{
// printf("index: %d, Old cathode energy: %d \n", pc.index[i],pc.e[i]);
auto it = slopeInterceptMap.find(pc.index[i]);
if (it != slopeInterceptMap.end())
{
double slope = it->second.first;
double intercept = it->second.second;
// printf("slope: %f, intercept:%f\n" ,slope, intercept);
pc.e[i] = slope * pc.e[i] + intercept;
// printf("index: %d, New cathode energy: %d \n",pc.index[i], pc.e[i]);
}
hpcIndexVE_GM->Fill(pc.index[i], pc.e[i]);
}
}
// Calculate the crossover points and put them into an array
@ -345,22 +392,22 @@ Bool_t Analyzer::Process(Long64_t entry)
//-so that it can be used to sort "good" hits later
Crossover[i][j][1].x = alpha;
if (i == 16)
{
for(int k=0;k<5;k++){
if ((i+24+k)%24==j)
{
// if (alpha < 0 && alpha >= -1)
// {
printf("Anode and cathode indices and coord : %d %d %f %f %f %f\n", i, j, pwinstance.Ca[j].first.X(), pwinstance.Ca[j].first.Y(), pwinstance.Ca[j].first.Z(), alpha);
printf("Crossover wires, points and alpha are : %f %f %f %f \n", Crossover[i][j][1].x, Crossover[i][j][1].y, Crossover[i][j][1].z, Crossover[i][j][2].x /*this is alpha*/);
// }
}
}
}
// if (i == 16)
// {
// for (int k = 0; k < 5; k++)
// {
// if ((i + 24 + k) % 24 == j)
// {
// // if (alpha < 0 && alpha >= -1)
// // {
// printf("Anode and cathode indices and coord : %d %d %f %f %f %f\n", i, j, pwinstance.Ca[j].first.X(), pwinstance.Ca[j].first.Y(), pwinstance.Ca[j].first.Z(), alpha);
// printf("Crossover wires, points and alpha are : %f %f %f %f \n", Crossover[i][j][1].x, Crossover[i][j][1].y, Crossover[i][j][1].z, Crossover[i][j][2].x /*this is alpha*/);
// // }
// }
// }
// }
}
}
std::vector<std::pair<int, double>> anodeHits = {};
std::vector<std::pair<int, double>> cathodeHits = {};

View File

@ -0,0 +1,124 @@
#include <TFile.h>
#include <TH1.h>
#include <TSpectrum.h>
#include <TF1.h>
#include <TCanvas.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <TText.h>
void FitHistogramsWithTSpectrum_Sequential_Improved() {
TFile *inputFile = new TFile("../Histograms_anodes.root", "READ");
if (!inputFile || inputFile->IsZombie()) {
std::cerr << "Error opening the input file!" << std::endl;
return;
}
TCanvas *c1 = new TCanvas("c1", "Histogram Viewer", 800, 600);
// Open the output ASCII file to save the centroids
std::ofstream outFile("centroids.txt");
if (!outFile.is_open()) {
std::cerr << "Error opening output file!" << std::endl;
return;
}
outFile << "HistogramIndex\tPeakNumber\tCentroid\tAmplitude\tSigma" << std::endl;
for (int i = 0; i < 24; ++i) {
TH1 *histogram = dynamic_cast<TH1*>(inputFile->Get(Form("hCathode_%d", i)));
if (!histogram) {
std::cerr << "Failed to retrieve histogram_" << i << " from the file." << std::endl;
continue;
}
// Set range for peak search
double minX = 700;
double maxX = 25000;
histogram->GetXaxis()->SetRangeUser(minX, maxX);
// Draw the histogram
c1->cd();
histogram->Draw();
// Peak search using TSpectrum
const int maxPeaks = 5;
TSpectrum spectrumFinder(maxPeaks);
int nFound = spectrumFinder.Search(histogram, 2, "", 0.01);
if (nFound <= 0) {
std::cerr << "No peaks found for histogram " << i << std::endl;
continue;
}
Double_t *xPositions = spectrumFinder.GetPositionX();
Double_t *yPositions = spectrumFinder.GetPositionY();
std::vector<std::pair<Double_t, Double_t>> peaks;
// Collect and sort peaks by X position
for (int j = 0; j < nFound; ++j) {
peaks.emplace_back(xPositions[j], yPositions[j]);
}
std::sort(peaks.begin(), peaks.end());
// Fit each peak with a Gaussian
for (int j = 0; j < peaks.size(); ++j) {
Double_t peakX = peaks[j].first;
Double_t peakY = peaks[j].second;
Double_t initialAmplitude = peakY; // Better initial guess
Double_t initialCentroid = peakX; // Centroid based on peak position
Double_t initialSigma = 60.0;
// Define Gaussian with initial parameters
TF1 *gaussFit = new TF1(Form("gauss_%d", j), "gaus", peakX - 200, peakX + 200);
//gaussFit->SetParameters(peakY, peakX, 25.0); // Initial guesses for amplitude, mean, sigma
gaussFit->SetParameters(initialAmplitude, initialCentroid, initialSigma);
// Perform fit
int fitStatus = histogram->Fit(gaussFit, "RQ+");
if (fitStatus != 0) {
std::cerr << "Fit failed for peak " << j + 1 << " in histogram " << i << std::endl;
delete gaussFit;
continue;
}
// Retrieve fit parameters
double amplitude = gaussFit->GetParameter(0);
double centroid = gaussFit->GetParameter(1);
double sigma = gaussFit->GetParameter(2);
double amplitudeError = gaussFit->GetParError(0);
double centroidError = gaussFit->GetParError(1);
double sigmaError = gaussFit->GetParError(2);
// Chi-squared value
double chi2 = gaussFit->GetChisquare();
int ndf = gaussFit->GetNDF();
outFile << i << "\t" << j + 1 << "\t" << centroid << std::endl;
gaussFit->SetLineColor(kRed);
gaussFit->Draw("SAME");
TText *text = new TText();
text->SetNDC();
text->SetTextSize(0.03);
text->SetTextColor(kRed);
//text->DrawText(0.15, 0.8 - j * 0.05, Form("Peak %d: Amp=%.2f, Mean=%.2f, Sigma=%.2f", j + 1, amplitude, centroid, sigma));
text->DrawText(0.15, 0.8 - j * 0.05,
Form("Peak %d: Amp=%.2f±%.2f, Mean=%.2f±%.2f, Sigma=%.2f±%.2f, Chi2/NDF=%.2f",
j + 1, amplitude, amplitudeError, centroid, centroidError, sigma, sigmaError, chi2 / ndf));
// Save results
// Clean up
delete gaussFit;
}
// Update canvas for visualization
c1->Update();
std::cout << "Press Enter to view the next histogram..." << std::endl;
c1->WaitPrimitive(); // Wait until Enter is pressed in the ROOT console
}
// Close resources
inputFile->Close();
outFile.close();
delete c1;
}

132
MatchAndPlotCentroids.C Normal file
View File

@ -0,0 +1,132 @@
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <iostream>
#include <TGraph.h>
#include <TF1.h>
#include <TCanvas.h>
#include <TH1.h>
void MatchAndPlotCentroids() {
// Open the centroid data file
std::ifstream inputFile("centroids.txt");
if (!inputFile.is_open()) {
std::cerr << "Error: Could not open Centroids.txt" << std::endl;
return;
}
// Data structure to store centroids by histogram and peak number
std::map<int, std::map<int, double>> centroidData;
// Read data from the file
std::string line;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
int histogramIndex, peakNumber;
double centroid;
if (iss >> histogramIndex >> peakNumber >> centroid) {
centroidData[histogramIndex][peakNumber] = centroid;
}
}
inputFile.close();
// Ensure histogram 24 exists and has data
if (centroidData.find(1) == centroidData.end()) {
std::cerr << "Error: Histogram 0 not found in the data!" << std::endl;
return;
}
// Reference centroids from histogram 24
const auto& referenceCentroids = centroidData[1];
std::ofstream outputFile("slope_intercept_results.txt");
if (!outputFile.is_open()) {
std::cerr << "Error: Could not open the output file for writing!" << std::endl;
return;
}
outputFile << "Histogram Number\tSlope\tIntercept\n";
// Loop through histograms 25 to 47
for (int targetHist = 0; targetHist <= 23; ++targetHist) {
// Ensure the target histogram exists and matches in peak numbers
if (centroidData.find(targetHist) == centroidData.end() || centroidData[targetHist].size() != referenceCentroids.size()) {
std::cout << "Skipping Histogram " << targetHist << " due to mismatched or missing data." << std::endl;
continue;
}
// Prepare x and y values for TGraph
std::vector<double> xValues, yValues;
for (const auto& [peakNumber, refCentroid] : referenceCentroids) {
if (centroidData[targetHist].find(peakNumber) != centroidData[targetHist].end()) {
yValues.push_back(refCentroid);
xValues.push_back(centroidData[targetHist][peakNumber]);
} else {
std::cerr << "Warning: Peak " << peakNumber << " missing in histogram " << targetHist << std::endl;
}
}
if (xValues.size() < 4) {
std::cout << "Skipping Histogram " << targetHist << " as it has less than 4 matching centroids." << std::endl;
continue;
}
// Create a TGraph
TCanvas *c1 = new TCanvas(Form("c_centroid_1_vs_%d", targetHist), Form("Centroid 1 vs %d", targetHist), 800, 600);
TGraph *graph = new TGraph(xValues.size(), &xValues[0], &yValues[0]);
graph->SetTitle(Form("Centroid of Histogram %d vs 1", targetHist));
graph->GetYaxis()->SetTitle("Centroid of Histogram 1");
graph->GetXaxis()->SetTitle(Form("Centroid of Histogram %d", targetHist));
graph->SetMarkerStyle(20); // Full circle marker
graph->SetMarkerSize(1.0);
graph->SetMarkerColor(kBlue);
// Draw the graph
graph->Draw("AP");
double minX = *std::min_element(xValues.begin(), xValues.end());
double maxX = *std::max_element(xValues.begin(), xValues.end());
// Fit the data with a linear function
TF1 *fitLine = new TF1("fitLine", "pol1", minX, maxX); // Adjust range as needed
fitLine->SetLineColor(kRed); // Set the line color to distinguish it
fitLine->SetLineWidth(2); // Thicker line for visibility
graph->Fit(fitLine, "M");
fitLine->Draw("same");
fitLine->SetParLimits(0, -10, 10); // Limit intercept between -10 and 10
fitLine->SetParLimits(1, 0, 2);
// Extract slope and intercept
double slope = fitLine->GetParameter(1);
double intercept = fitLine->GetParameter(0);
outputFile << targetHist << "\t" << slope << "\t" << intercept << "\n";
std::cout << "Histogram 24 vs " << targetHist << ": Slope = " << slope << ", Intercept = " << intercept << std::endl;
std::vector<double> residuals;
for (size_t i = 0; i < xValues.size(); ++i) {
double fittedY = fitLine->Eval(xValues[i]); // Evaluate fitted function at x
double residual = yValues[i] - fittedY; // Residual = observed - fitted
residuals.push_back(residual);
}
// Create a graph for the residuals
/*TGraph *residualGraph = new TGraph(residuals.size(), &xValues[0], &residuals[0]);
residualGraph->SetTitle(Form("Residuals for Histogram 24 vs %d", targetHist));
residualGraph->GetYaxis()->SetTitle("Residuals");
residualGraph->GetXaxis()->SetTitle(Form("Centroid of Histogram %d", targetHist));
residualGraph->SetMarkerStyle(20);
residualGraph->SetMarkerSize(1.0);
residualGraph->SetMarkerColor(kGreen);
// Draw the residuals plot below the original plot (can be on a new canvas if preferred)
TCanvas *c2 = new TCanvas(Form("c_residuals_24_vs_%d", targetHist), Form("Residuals for Centroid 24 vs %d", targetHist), 800, 400);
residualGraph->Draw("AP");*/
c1->Update();
//c2->Update();
std::cout << "Press Enter to continue..." << std::endl;
//std::cin.get();
c1->WaitPrimitive();
//c2->WaitPrimitive();
//std::cin.get();
//std::cin.get();
}
outputFile.close();
std::cout << "Results written to slope_intercept_results.txt" << std::endl;
}

View File

@ -30,4 +30,4 @@ if [ $option -eq 0 ]; then
./Mapper ${rootFolder}/*${runID}*${timeWindow}.root
fi
root "processRun.C(\"${rootFolder}/Run_${runID}_mapped.root\")"
root "processRun.C(\"${rootFolder}/PulserRun_${runID}_mapped.root\")"

89
centroids.txt Normal file
View File

@ -0,0 +1,89 @@
HistogramIndex PeakNumber Centroid Amplitude Sigma
1 1 922.213
1 2 1885.55
1 3 2845.53
1 4 3810.32
2 1 955.591
2 2 1953.17
2 3 2949.37
2 4 3950.79
3 1 995.787
3 2 2036.58
3 3 3076.91
3 4 4112.05
4 1 1017.48
4 2 2080.19
4 3 3142.24
4 4 4206.1
5 1 1022.78
5 2 2091.21
5 3 3158.28
5 4 4226.97
6 1 1076.22
6 2 2203.37
6 3 3329.53
6 4 4457.69
7 1 977.46
7 2 1998.02
7 3 3017.36
7 4 4040.47
8 1 1049.74
8 2 2144.38
8 3 3238.2
8 4 4335.25
9 1 1000.59
9 2 2046.42
9 3 3090.29
9 4 4129.63
10 1 1014.92
10 2 2076.16
10 3 3134.59
10 4 4213.42
11 1 1004.85
11 2 2052.88
11 3 3100.3
11 4 4164.75
12 1 945.861
12 2 1932.49
12 3 2917.95
12 4 3955.15
13 1 998.307
13 2 2040.38
13 3 3078.76
13 4 4135.51
14 1 966.429
14 2 1972.15
14 3 2974.84
14 4 4056.41
15 1 958.352
15 2 1958.64
15 3 2957.7
15 4 3970.41
16 1 970.732
16 2 1977.63
16 3 2984.97
16 4 4002.56
17 1 1013.65
17 2 2064.9
17 3 3114.19
17 4 4190.98
18 1 975.538
18 2 1990.64
18 3 3005.46
18 4 4048.99
20 1 912.778
20 2 1866.83
20 3 2819.21
20 4 3781.63
21 1 1002.36
21 2 1989.95
21 3 2975.53
21 4 3986.71
22 1 1075.38
22 2 2144.25
22 3 3210.17
22 4 4312.84
23 1 988.828
23 2 2016.35
23 3 3044.19
23 4 4082.41

89
centroids_edited.txt Normal file
View File

@ -0,0 +1,89 @@
HistogramIndex PeakNumber Centroid Amplitude Sigma
1 1 922.213
1 2 1885.55
1 3 2845.53
1 4 3810.32
2 1 955.591
2 2 1953.17
2 3 2949.37
2 4 3950.79
3 1 995.787
3 2 2036.58
3 3 3076.91
3 4 4112.05
4 1 1017.48
4 2 2080.19
4 3 3142.24
4 4 4206.1
5 1 1022.78
5 2 2091.21
5 3 3158.28
5 4 4226.97
6 1 1076.22
6 2 2203.37
6 3 3329.53
6 4 4457.69
7 1 977.46
7 2 1998.02
7 3 3017.36
7 4 4040.47
8 1 1049.74
8 2 2144.38
8 3 3238.2
8 4 4335.25
9 1 1000.59
9 2 2046.42
9 3 3090.29
9 4 4129.63
10 1 1014.92
10 2 2076.16
10 3 3134.59
10 4 4213.42
11 1 1004.85
11 2 2052.88
11 3 3100.3
11 4 4164.75
12 1 945.861
12 2 1932.49
12 3 2917.95
12 4 3955.15
13 1 998.307
13 2 2040.38
13 3 3078.76
13 4 4135.51
14 1 966.429
14 2 1972.15
14 3 2974.84
14 4 4056.41
15 1 958.352
15 2 1958.64
15 3 2957.7
15 4 3970.41
16 1 970.732
16 2 1977.63
16 3 2984.97
16 4 4002.56
17 1 1013.65
17 2 2064.9
17 3 3114.19
17 4 4190.98
18 1 975.538
18 2 1990.64
18 3 3005.46
18 4 4048.99
20 1 912.778
20 2 1866.83
20 3 2819.21
20 4 3781.63
21 1 1002.36
21 2 1989.95
21 3 2975.53
21 4 3986.71
22 1 1075.38
22 2 2144.25
22 3 3210.17
22 4 4312.84
23 1 988.828
23 2 2016.35
23 3 3044.19
23 4 4082.41

View File

@ -0,0 +1,23 @@
Histogram Number Slope Intercept
24 1 -2.89219e-10
25 0.942098 -0.105169
26 0.980862 -0.732032
27 0.982975 -2.22704
28 0.978815 -1.51477
29 0.965245 -2.19515
30 0.945384 -0.892599
31 0.977408 -0.908592
32 0.919546 3.25464
33 0.972194 2.44956
34 0.92852 5.44745
35 0.947098 1.40531
36 0.875491 -1.13145
37 1.95496 -1735.58
38 0.970862 2.86019
40 0.91793 -3.80615
41 0.913897 -2.12964
42 0.954014 -0.760604
43 0.993616 -1.40278
45 0.926169 -21.2016
46 1.00577 -2.14281
47 0.943312 -1.26464

View File

@ -0,0 +1,49 @@
Histogram Number Slope Intercept
0 1 0
1 1 -1.87356e-10
2 0.964185 1.49989
3 0.92638 -1.30621
4 0.905569 1.00834
5 0.901182 0.470903
6 0.853932 3.32687
7 0.942785 1.08887
8 0.878904 -0.0107433
9 0.922662 -2.32259
10 0.903343 8.38332
11 0.914227 6.56108
12 0.961008 23.0982
13 0.920976 5.22104
14 0.936584 31.5073
15 0.959044 5.43267
16 0.95263 -0.404053
17 0.90953 4.82833
18 0.940277 10.3629
19 1 0
20 1.00683 4.76371
21 0.968342 -43.9496
22 0.892882 -32.0742
23 0.933615 1.10704
24 1 -2.89219e-10
25 0.942098 -0.105169
26 0.980862 -0.732032
27 0.982975 -2.22704
28 0.978815 -1.51477
29 0.965245 -2.19515
30 0.945384 -0.892599
31 0.977408 -0.908592
32 0.919546 3.25464
33 0.972194 2.44956
34 0.92852 5.44745
35 0.947098 1.40531
36 0.875491 -1.13145
37 1 0
38 0.970862 2.86019
39 1 0
40 0.91793 -3.80615
41 0.913897 -2.12964
42 0.954014 -0.760604
43 0.993616 -1.40278
44 1 0
45 0.926169 -21.2016
46 1.00577 -2.14281
47 0.943312 -1.26464

View File

@ -0,0 +1,21 @@
Histogram Number Slope Intercept
1 1 -1.87356e-10
2 0.964185 1.49989
3 0.92638 -1.30621
4 0.905569 1.00834
5 0.901182 0.470903
7 0.942785 1.08887
8 0.878904 -0.0107433
10 0.903343 8.38332
11 0.914227 6.56108
12 0.961008 23.0982
13 0.920976 5.22104
14 0.936584 31.5073
15 0.959044 5.43267
16 0.95263 -0.404053
17 0.90953 4.82833
18 0.940277 10.3629
20 1.00683 4.76371
21 0.968342 -43.9496
22 0.892882 -32.0742
23 0.933615 1.10704