From 75f5a994999a08e99aa9c113d76136c2281f0ad9 Mon Sep 17 00:00:00 2001
From: Carlos Yero <cyero002@fiu.edu>
Date: Thu, 22 Jun 2017 13:42:26 -0400
Subject: [PATCH] Add capability to calibrate SHMS calorimeter with part of a
 run, by choosing an event range.

---
 CALIBRATION/shms_cal_calib/THcPShowerCalib.h | 40 +++++++++++++-------
 CALIBRATION/shms_cal_calib/pcal_calib.cpp    | 25 +++++++++---
 2 files changed, 47 insertions(+), 18 deletions(-)

diff --git a/CALIBRATION/shms_cal_calib/THcPShowerCalib.h b/CALIBRATION/shms_cal_calib/THcPShowerCalib.h
index 4200a0c2..9e1cb069 100644
--- a/CALIBRATION/shms_cal_calib/THcPShowerCalib.h
+++ b/CALIBRATION/shms_cal_calib/THcPShowerCalib.h
@@ -53,7 +53,7 @@ class THcPShowerCalib {
 
  public:
 
-  THcPShowerCalib(string);
+  THcPShowerCalib(string, int, int);
   THcPShowerCalib();
   ~THcPShowerCalib();
 
@@ -81,6 +81,8 @@ class THcPShowerCalib {
 
   TTree* fTree;
   UInt_t fNentries;
+  UInt_t fNstart;
+  UInt_t fNstop;
 
   // Declaration of leaves types
 
@@ -150,8 +152,10 @@ THcPShowerCalib::THcPShowerCalib() {};
 
 //------------------------------------------------------------------------------
 
-THcPShowerCalib::THcPShowerCalib(string RunNumber) {
+THcPShowerCalib::THcPShowerCalib(string RunNumber, int nstart, int nstop) {
   fRunNumber = RunNumber;
+  fNstart = nstart;
+  fNstop = nstop;
 };
 
 //------------------------------------------------------------------------------
@@ -173,7 +177,9 @@ void THcPShowerCalib::SaveRawData() {
 
   THcPShTrack trk;
 
-  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  //  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  for (UInt_t ientry=TMath::Max(UInt_t(0),fNstart);
+       ientry<TMath::Min(fNstop,fNentries); ientry++) {
 
     if (ReadShRawTrack(trk, ientry)) {
       trk.SetEs(falphaC);
@@ -281,12 +287,16 @@ void THcPShowerCalib::CalcThresholds() {
   // Histogram uncalibrated energy depositions, get mean and RMS from the
   // histogram, establish +/-3 * RMS thresholds.
 
-  cout << "THcPShowerCalib::CalcThresholds: FNentries = " << fNentries << endl;
+  //cout<< "THcPShowerCalib::CalcThresholds: FNentries = " << fNentries << endl;
+  cout << "THcPShowerCalib::CalcThresholds: fNstart = " << fNstart << " "
+       << "  fNstop = " << fNstop << endl;
 
   Int_t nev = 0;
   THcPShTrack trk;
 
-  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  //  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  for (UInt_t ientry=TMath::Max(UInt_t(0),fNstart);
+       ientry<TMath::Min(fNstop,fNentries); ientry++) {
 
     if ( ReadShRawTrack(trk, ientry)) {
 
@@ -425,7 +435,9 @@ void THcPShowerCalib::ComposeVMs() {
 
   // Loop over the shower track events in the ntuples.
 
-  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  //  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  for (UInt_t ientry=TMath::Max(UInt_t(0),fNstart);
+       ientry<TMath::Min(fNstop,fNentries); ientry++) {
 
     if (ReadShRawTrack(trk, ientry)) {
 
@@ -706,14 +718,16 @@ void THcPShowerCalib::FillHEcal() {
   // Output event by event energy depositions and momenta for debug purposes.
   //
 
-  ofstream output;
-  output.open("calibrated.deb",ios::out);
+  //  ofstream output;
+  //  output.open("calibrated.deb",ios::out);
 
   Int_t nev = 0;
 
   THcPShTrack trk;
 
-  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  //  for (UInt_t ientry=0; ientry<fNentries; ientry++) {
+  for (UInt_t ientry=TMath::Max(UInt_t(0),fNstart);
+       ientry<TMath::Min(fNstop,fNentries); ientry++) {
 
     if (ReadShRawTrack(trk, ientry)) {
       //    trk.Print(cout);
@@ -729,15 +743,15 @@ void THcPShowerCalib::FillHEcal() {
 
       hESHvsEPR->Fill(trk.EPRnorm(), trk.ESHnorm());
 
-      output << Enorm*P/1000. << " " << P/1000. << " " << delta << " "
-	     << trk.GetX() << " " << trk.GetY() << endl;
+      //      output << Enorm*P/1000. << " " << P/1000. << " " << delta << " "
+      //	     << trk.GetX() << " " << trk.GetY() << endl;
 
       nev++;
     }
 
   };
 
-  output.close();
+  //  output.close();
 
   cout << "FillHEcal: " << nev << " events filled" << endl;
 };
@@ -752,7 +766,7 @@ void THcPShowerCalib::SaveAlphas() {
   //
 
   ofstream output;
-  char* fname = Form("pcal.param.%s",fRunNumber.c_str());
+  char* fname = Form("pcal.param.%s_%d-%d",fRunNumber.c_str(),fNstart,fNstop);
   cout << "SaveAlphas: fname=" << fname << endl;
 
   output.open(fname,ios::out);
diff --git a/CALIBRATION/shms_cal_calib/pcal_calib.cpp b/CALIBRATION/shms_cal_calib/pcal_calib.cpp
index 008e7b1e..04114018 100644
--- a/CALIBRATION/shms_cal_calib/pcal_calib.cpp
+++ b/CALIBRATION/shms_cal_calib/pcal_calib.cpp
@@ -8,14 +8,15 @@
 // A steering Root script for the SHMS calorimeter calibration.
 //
 
-void pcal_calib(string RunNumber) {
+void pcal_calib(string RunNumber, int nstart=0, int nstop=999999999) {
 
   // Initialize the analysis clock
   clock_t t = clock();
  
-  cout << "Calibrating run " << RunNumber << endl;
+  cout << "Calibrating run " << RunNumber << ", events "
+       << nstart << " -- " << nstop << endl;
 
-  THcPShowerCalib theShowerCalib(RunNumber);
+  THcPShowerCalib theShowerCalib(RunNumber, nstart, nstop);
 
   theShowerCalib.Init();            // Initialize constants and variables
   theShowerCalib.CalcThresholds();  // Thresholds on the uncalibrated Edep/P
@@ -41,7 +42,7 @@ void pcal_calib(string RunNumber) {
   theShowerCalib.hEuncSel->DrawCopy("same");
 
   Canvas->cd(2);
-  theShowerCalib.hESHvsEPR->Draw("colz");
+  theShowerCalib.hESHvsEPR->Draw();
 
   // Normalized energy deposition after calibration.
 
@@ -56,7 +57,21 @@ void pcal_calib(string RunNumber) {
   // SHMS delta(P) versus the calibrated energy deposition.
 
   Canvas->cd(4);
-  theShowerCalib.hDPvsEcal->Draw("colz");
+  theShowerCalib.hDPvsEcal->Draw();
+
+  // Save canvas in a pdf format.
+  Canvas->Print(Form("%s_%d-%d.pdf",RunNumber.c_str(),nstart,nstop));
+
+  // Save histograms in root file.
+
+  //TFile* froot=new TFile(Form("%s_%d-%d.root",RunNumber.c_str(),nstart,nstop),
+  //			   "RECREATE");
+  //  theShowerCalib.hEunc->Write();
+  //  theShowerCalib.hEuncSel->Write();
+  //  theShowerCalib.hESHvsEPR->Write();
+  //  theShowerCalib.hEcal->Write();
+  //  theShowerCalib.hDPvsEcal->Write();
+  //  froot->Close();
 
   // Calculate the analysis rate
   t = clock() - t;
-- 
GitLab