int _tmain(int argc, _TCHAR* argv[])
{
   std::string scripts_filename;
   std::string constants_filename;
   std::string gexscripts_filename;
   std::string gexconstants_filename;
   std::string line;

   if (argc <= 4)
   {
      std::cout << "Usage: GEXCreator scripts.gml constants.gml gexscripts.txt gexconstants.txt" << std::endl;
      return -1;
   }

   if (argc >= 1)
      scripts_filename = argv[1];
   if (argc >= 2)
      constants_filename = argv[2];
   if (argc >= 3)
      gexscripts_filename = argv[3];
   if (argc >= 4)
      gexconstants_filename = argv[4];

   std::ifstream scripts_file(scripts_filename.c_str());
   std::ifstream constants_file(constants_filename.c_str());
   std::ofstream gexscripts_file(gexscripts_filename.c_str());
   std::ofstream gexconstants_file(gexconstants_filename.c_str());

   if (!scripts_file.is_open())
   {
      std::cout << "Error: Cannot find the specified GML scripts file." << std::endl;
      return -1;
   }

   if (!constants_file.is_open())
   {
      std::cout << "Error: Cannot find the specified GML constants file." << std::endl;
      return -1;
   }

   if (!gexscripts_file.is_open())
   {
      std::cout << "Error: Cannot create the specified GEX scripts import file." << std::endl;
      return -1;
   }

   if (!gexconstants_file.is_open())
   {
      std::cout << "Error: Cannot create the specified GEX constants import file." << std::endl;
      return -1;
   }

   while(std::getline(scripts_file, line))
   {
      if (line.find("#define ") == 0)
      {
         // Parse out our function name
         std::string function = line.replace(0, 8, "");

         gexscripts_file << "kind dll-cdecl" << std::endl;
         gexscripts_file << "function " << function;

         int count = 0;
         bool found = false;
         while(std::getline(scripts_file, line))
         {
            // See if this is a value funtion definition
            if (line.find("(") != std::string::npos && line.find(")") != std::string::npos)
            {
               found = true;
               break;
            }

            // If we parsed more than 10 lines without finding our function definition, then
            // we just ain't gonna find it.
            if (count > 10)
               break;

            count++;
         }

         if (found)
         {
            std::vector<std::string> tokens;

            // Remove function parens
            line.replace(0, line.find("(") + 1, "");
            line.replace(line.find(")"), 1, "");

            // Tokenize our parameters
            Tokenize(line, tokens);
            std::string var_type = "unknown";
            count = 0;

            gexscripts_file << "(";
            for (int x = 0; x < tokens.size(); x++)
            {
               // Remove any commas
               if (tokens[x].find(",") != std::string::npos)
                  tokens[x] = tokens[x].replace(tokens[x].find(","), 1, "");
               
               // Either save type or...
               if (tokens[x] == "real" || tokens[x] == "string")
                  var_type = tokens[x];
               else
               {
                  // Write parameter and type
                  if (count > 0)
                     gexscripts_file << ",";

                  gexscripts_file << tokens[x] << ":" << var_type;
                  var_type = "unknown";
                  count++;
               }
            }
            gexscripts_file << ")" << std::endl;
         }
         else
         {
            gexscripts_file << "()" << std::endl;
         }
      }
   }

   scripts_file.close();
   gexscripts_file.close();

   while(std::getline(constants_file, line))
   {
      if (line.find("global.") != std::string::npos)
      {
         if (line.find("=") != std::string::npos)
         {
            std::string constant = line.replace(0, line.find("global.") + 7, "");
            constant = constant.substr(0, constant.find("="));

            std::string value = line.replace(0, line.find("=") + 1, "");

            // Replace double quotes with single quots
            while (value.find("\"") != std::string::npos)
               value = value.replace(value.find("\""), 1, "'");

            // Remove ;
            value = value.replace(value.find(";"), 1, "");

            gexconstants_file << "const " << constant << " = ";
            gexconstants_file << value << std::endl;
         }
      }
   }

   gexconstants_file.close();

	return 0;
}
예제 #2
0
// filname : output file name 
// pmode : pedestal mode 
// mode :  
void SPEFit_UXC_Ana_V2(int run=254743, TString filname="test", bool pmode=false, int mode=1)
{

    /*mode 1: TH1F * HistoSum[2][72][13];             // integral of TS 2-5
      mode 2: TH1F * HistoFullSum[2][72][13];         // integral of TS 0-9
      mode 3: TH1F * HistoEachTS[2][72][13];          // no sum but each TS 
      mode 4: TH1F * HistoEachTSsub[2][72][13];       // no sum but ecah TS(2-5)
      mode5-15 TH1F * HistoSeparateTS[2][72][13][mode-5] // draw only TS = mode-5
     */
    TString fLEDname = Form("Run_%i.root",run);
    TString fPEDname = fLEDname;
    TString rootname = filname+"_mode_";
    rootname+=mode;
    rootname+=".root";
    pedmode=pmode;
    int LED_amp=40;
    _hstFile = new TFile(rootname,"recreate");
    BookHistograms();
    GetHFPMap();
    //set plotting styles
    gStyle->SetCanvasColor(0);
    gStyle->SetPadColor(0);
    gStyle->SetCanvasBorderMode(0);
    gStyle->SetFrameBorderMode(0);
    gStyle->SetStatColor(0);
    gStyle->SetPadTickX(1);
    gStyle->SetPadTickY(1);


    //set file names
    stringstream out_fname;
    stringstream out_fname1;
    out_fname<<"SPEconstants_Run_"<<run<<"_"<<filname<<"_mode_"<<mode<<".txt";
    out_fname1<<"SPEspec_Run_"<<run<<"_"<<filname<<".txt";

    ofstream  constants_file(out_fname.str().c_str(),ios_base::trunc); 
    //ofstream  constants_file1(out_fname1.str().c_str(),ios_base::trunc); 
    constants_file<<"#Run "<<run<<endl;
    constants_file<<"#type SPE"<<endl;
    constants_file<<"#LED_amplitude "<<LED_amp<<endl;
    constants_file<<"#Robox Column iDepth iPhi iEta Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err"<<endl;

    out_fname.str("");
    out_fname<<"SPEdistributions_Run_"<<run<<".txt";

    out_fname.str("");
    out_fname<<"SPEextra_Run_"<<run<<".txt";
    //ofstream  extra_file(out_fname.str().c_str(),ios_base::trunc); 


    double scale = 1.0;
    scale = 2.6; //Need to scale up HF charge
    double fC2electrons = 6240.; //convert fC to #electrons

    char spename[128], pedname[128], spehistname[128], numname[128];

    TFile *tfLED = new TFile(fLEDname);
    TFile *tfPED = new TFile(fPEDname);


    //const int NnewBins = 106;
    //double binsX[NnewBins] = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,180,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};

    //const int NnewBins = 80;//75
    //double binsX[NnewBins] = {0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};	  

    const int NnewBins = 101;//75
    double binsX[NnewBins] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100};
    
    const int NnewBins_single = 57;
    double binsX_single[NnewBins] = {0,3,6,9,12,15,18,21,24,27,30,33,36,39,45,51,57,63,69,75,81,87,93,99,105,111,117,123,129,135,141,147,153,159,165,171,177,183,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};	  

    //default: 
    const int NnewBins_ped = 46;//75
    double binsX_ped[NnewBins] = {0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,120,138,156,174,200,230,266,316,378,456,500};
    double binsX_ped_single[NnewBins] = {0,3,6,9,12,15,18,21,24,27,30,33,36,39,45,51,57,63,69,75,81,87,93,99,120,138,156,174,200,230,266,316,378,456,500};
    const int NnewBins_ped_single = 31;
    //var1
    //  const int NnewBins_ped = 37;//75
    // double binsX_ped[NnewBins_ped] = {0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,200,350,500};

    //double binsX[NnewBins] = {0,6,12,18,24,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};	 
    TH1F *hspe[8], *resid[8];
    if(!pedmode) {
        if(mode<3){ 
            for(int i=0; i<8; i++) hspe[i] = new TH1F(Form("hspe_pmt%i",i+1),Form("hspe_pmt%i",i+1),NnewBins-1,binsX);
            for(int i=0; i<8; i++) resid[i] = new TH1F(Form("resid_pmt%i",i+1),Form("resid_pmt%i",i+1),NnewBins-1,binsX);
        }
        else{
            for(int i=0; i<8; i++) hspe[i] = new TH1F(Form("hspe_pmt%i",i+1),Form("hspe_pmt%i",i+1),NnewBins_single-1,binsX_single);
            for(int i=0; i<8; i++) resid[i] = new TH1F(Form("resid_pmt%i",i+1),Form("resid_pmt%i",i+1),NnewBins_single-1,binsX_single);
        }
    }
    else if(pedmode) {
        if(mode<3){ 
            for(int i=0; i<8; i++) hspe[i] = new TH1F(Form("hspe_pmt%i",i+1),Form("hspe_pmt%i",i+1),NnewBins_ped-1,binsX);
            for(int i=0; i<8; i++) resid[i] = new TH1F(Form("resid_pmt%i",i+1),Form("resid_pmt%i",i+1),NnewBins_ped-1,binsX);
        }
        else{
            for(int i=0; i<8; i++) hspe[i] = new TH1F(Form("hspe_pmt%i",i+1),Form("hspe_pmt%i",i+1),NnewBins_ped_single-1,binsX_single);
            for(int i=0; i<8; i++) resid[i] = new TH1F(Form("resid_pmt%i",i+1),Form("resid_pmt%i",i+1),NnewBins_ped_single-1,binsX_single);
        }
    }
    for(int i=0; i<8; i++) hspe[i]->Sumw2();
    for(int i=0; i<8; i++) resid[i]->Sumw2();




    /* // int NDepth = 2; //number of depths
       int MinDepth = 1;
       int MaxDepth = 2;
       int MinEta = 29; 
       int MaxEta = 33; //41
    //int MinPhi = 41;
    //int MaxPhi = 53;
    int MinPhi = 1;
    int MaxPhi = 5; //71
    int NEta = 1+MaxEta-MinEta;
    int NPhi = 1+MaxPhi-MinPhi;*/

    //TCanvas *Carray[NDepth+1][MaxPhi+1];
    TCanvas *Carray[37][4];
    bool drawflag[37][4];  
    bool badfit[37][4][9];
    TH1F *LED[NDepth+1][MaxEta+1][MaxPhi+1];
    TH1F *PED[NDepth+1][MaxEta+1][MaxPhi+1];
//    TH1F *NumAbove[NDepth+1][MaxEta+1][MaxPhi+1];




    for(int iDepth = MinDepth; iDepth <= MaxDepth; iDepth++){
        for(int iPhi = MinPhi; iPhi <= MaxPhi; iPhi++){

            bool nonNull = false;

            for(int iEta = MinEta; iEta <= MaxEta; iEta++){

                if(mode==1) sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
                if(mode==2) sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d_FullSum",iDepth,iEta,iPhi);
                if(mode==3) sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d_eachTS",iDepth,iEta,iPhi);
                if(mode==4) sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d_eachTSsub",iDepth,iEta,iPhi);
                if(mode>4) sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d_TS_%i",iDepth,iEta,iPhi,mode-5);


                LED[iDepth][iEta][iPhi]=(TH1F *)tfLED->Get(spename);
                if(LED[iDepth][iEta][iPhi]) nonNull = true;
                //else cout<<"failed reg"<<endl;

                //sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
                PED[iDepth][iEta][iPhi]=(TH1F *)tfPED->Get(spename);

                sprintf(numname,"Analyzer/CommonDir/ResPlotDir/Num_Above_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
                //	cout<<"Getting numAbove"<<endl;
//                NumAbove[iDepth][iEta][iPhi]=(TH1F *)tfLED->Get(numname);
                //	if(!NumAbove[iDepth][iEta][iPhi]) cout<<"Failed"<<endl;

            }



            if(nonNull){ //only create canvas if distributions exist

            }

        }
    }



    int HV=0;
    int iEta, iPhi,iDepth;
    /*for(int iDepth = MinDepth; iDepth <= MaxDepth; iDepth++){
      for(int iPhi = MinPhi; iPhi <= MaxPhi; iPhi++){
      for(int iEta = MinEta; iEta <= MaxEta; iEta++){*/
    for(int irbx = 1; irbx<37; irbx++){
        for(int ibb= 1; ibb<4;ibb++){
            
            //if(irbx!=9 || ibb!=3) continue; // FIXME
            
            drawflag[irbx][ibb] = false;
            char canvname[16];
            sprintf(canvname, "c_%d_%d", irbx,ibb);
            Carray[irbx][ibb] = new TCanvas(canvname,canvname,1800,800);
            Carray[irbx][ibb]->Divide(4,2);
            for(int ipmt=1; ipmt<9;ipmt++){
                badfit[irbx][ibb][ipmt] = false;
                iEta = eta[irbx][ibb][ipmt];
                iPhi = phi[irbx][ibb][ipmt];
                iDepth = dep[irbx][ibb][ipmt];
                //cout<<iDepth<<" "<<iPhi<<" "<<iEta<<endl;
                // if(iEta!=38 || iPhi !=33 || iDepth !=1) continue;
                if(!LED[iDepth][iEta][iPhi]) {cout<<"Fail depth eta phi "<<iDepth<<" "<<iEta<<" "<<iPhi<<endl; continue;}

                sprintf(spehistname,"led %d %d %d",iDepth,iEta,iPhi);
                TH1F *hspe_temp = (TH1F *)LED[iDepth][iEta][iPhi]->Clone(spehistname);
                //	cout<<"post clone depth eta phi pmt int "<<iDepth<<" "<<iEta<<" "<<iPhi<<" "<<ipmt<<" "<<hspe_temp->Integral()<<endl;
                sprintf(spehistname,"ped %d %d %d",iDepth,iEta,iPhi);
                TH1F *hped = (TH1F *)PED[iDepth][iEta][iPhi]->Clone(spehistname);
                hspe[ipmt-1]->Reset();
                resid[ipmt-1]->Reset();
                sprintf (spehistname, "SumLED_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
                hspe[ipmt-1]->SetTitle(spehistname);
                sprintf(spehistname,"cp_Num_Above_Depth_%i_Eta_%i_Phi_%i",iDepth,iEta,iPhi);
//                TH1F *chg_share = new TH1F(spehistname,spehistname,10,0,10);

//                for(int ix=2;ix<=10;ix++){
//                    chg_share->SetBinContent(ix,NumAbove[iDepth][iEta][iPhi]->GetBinContent(ix));
//                } 
//                chg_share->SetBinContent(1,0);
//                float chg_frac = chg_share->Integral(3,10)/chg_share->Integral();

                //cout<<"preloop check"<<ipmt<<" integral "<<hspe_temp->Integral()<<endl;
                //combine bins of original SPE histogram
                for(int ib=1; ib<=hspe_temp->GetNbinsX(); ib++) {
                    double bin_center = hspe_temp->GetBinCenter(ib);

                    if(bin_center>hspe[ipmt-1]->GetXaxis()->GetXmax()) continue;
                    int newbin = hspe[ipmt-1]->FindBin(bin_center);
                    double new_content = hspe[ipmt-1]->GetBinContent(newbin) + hspe_temp->GetBinContent(ib);
                    double new_error = sqrt(pow(hspe[ipmt-1]->GetBinError(newbin),2)+pow(hspe_temp->GetBinError(ib),2));
                    hspe[ipmt-1]->SetBinContent(newbin,new_content);
                    hspe[ipmt-1]->SetBinError(newbin,new_error);
                }
                TH1F* hspe_unscaled = (TH1F*)hspe[ipmt-1]->Clone("hspe_unscaled");
                //renormalize bins of new SPE histogram
                for(int ib=1; ib<=hspe[ipmt-1]->GetNbinsX(); ib++) {
                    double new_content = hspe[ipmt-1]->GetBinContent(ib)/hspe[ipmt-1]->GetXaxis()->GetBinWidth(ib)*hspe_temp->GetXaxis()->GetBinWidth(1);
                    double new_error = hspe[ipmt-1]->GetBinError(ib)/hspe[ipmt-1]->GetXaxis()->GetBinWidth(ib)*hspe_temp->GetXaxis()->GetBinWidth(1);
                    hspe[ipmt-1]->SetBinContent(ib,new_content);
                    hspe[ipmt-1]->SetBinError(ib,new_error);
                }
                //	cout<<"pre temp check "<<ipmu<<" integral "<<hspe_temp->Integral()<<endl; 

                // ------------------------------
                // FIT 
                // ------------------------------
                if(hspe_temp->Integral()==0) continue;
                else drawflag[irbx][ibb] = true;	  
                Nev = hspe_temp->Integral()*hspe_temp->GetXaxis()->GetBinWidth(1);  

                //
                // Pedestal fit 
                //
                //TF1 *fped = new TF1("fped","gaus",0, 80);
                TF1 *fped;
                if(mode<3) fped= new TF1("fped","gaus",0, 20);
                else fped= new TF1("fped","gaus",0,15);
                hped->Fit(fped,"NQR");
                double pploc = fped->GetParameter(1), ppwidth = fped->GetParameter(2);
                // DEBUG	
                //cout<<"depth "<<iDepth<<" ieta "<<iEta<<" iphi "<<iPhi <<" pploc "<<pploc<<" ppwidth "<<ppwidth<<endl;
                //hspe->Fit(fped, "NQ", "", pploc - 3*ppwidth, pploc + ppwidth);  
                hspe[ipmt-1]->Fit(fped, "NQ", "", pploc - 2*ppwidth, pploc + 2*ppwidth);  

                // 
                //estimate SPE peak location
                // 
                int max_SPE_bin, maxbin, Nbins;
                double max_SPE_height=0, minheight, max_SPE_location;
                bool minflag = false;
                maxbin=hspe[ipmt-1]->FindBin(fped->GetParameter(1)); //location of pedestal peak
                minheight=hspe[ipmt-1]->GetBinContent(maxbin); //initialize minheight
                Nbins = hspe[ipmt-1]->GetNbinsX();
                for(int j=maxbin+1; j<Nbins-1; j++) { //start from pedestal peak and loop through bins
                    if(hspe[ipmt-1]->GetBinContent(j) > minheight && !minflag) minflag=true; //only look for SPE peak when minflag=true
                    if(hspe[ipmt-1]->GetBinContent(j) < minheight )  minheight = hspe[ipmt-1]->GetBinContent(j);
                    if(minflag && hspe[ipmt-1]->GetBinContent(j) > max_SPE_height){
                        max_SPE_bin = j;
                        max_SPE_location = hspe[ipmt-1]->GetBinCenter(max_SPE_bin);
                        max_SPE_height = hspe[ipmt-1]->GetBinContent(j);
                    }
                } //start from pedestal peak and loop through bins
                //find minimum bin between pedestal and SPE peaks
                hspe[ipmt-1]->GetXaxis()->SetRange(maxbin,max_SPE_bin);
                int minbin = hspe[ipmt-1]->GetMinimumBin(); 
                double minbin_location = hspe[ipmt-1]->GetBinCenter(minbin);
                hspe[ipmt-1]->GetXaxis()->SetRange(1,Nbins);	    

                TF1 *fit = new TF1("fit", FitFun, 0, 50, 5);

                double mu = - log(fped->Integral(0,100)/Nev); // f(0)  = exp(-par[0])
                if(mu<0 && pedmode ) mu=0.00001;
                if( mu<0 && !pedmode) mu=0.01;
                double gain_est = max_SPE_location-1.0*fped->GetParameter(1);
                //if(max_SPE_bin > (minbin+1)) fit->SetParameters(mu, 20, 1, gain_est, gain_est*0.5);
                if(max_SPE_bin > (minbin+1)) fit->SetParameters(mu, fped->GetParameter(1), fped->GetParameter(2), gain_est, gain_est*0.5);
                else fit->SetParameters(mu, fped->GetParameter(1), fped->GetParameter(2), 2.1*fped->GetParameter(2), 10); //case of no clear minimum; start looking for SPE peak at 2sigma away from pedestal peak
                if(pedmode && (mode==1 || mode==4)) fit->SetParLimits(0, 0, 0.000316);
                if(pedmode && mode!=1 && mode!=4) fit->SetParLimits(0, 0, 0.00005);
                else fit->SetParLimits(0, 0, 10);
                //fit->FixParameter(1, fped->GetParameter(1)); // FIXME
                //fit->FixParameter(2, fped->GetParameter(2)); // FIXME
                fit->SetParLimits(3, fped->GetParameter(2)*2, 350);
                fit->SetParLimits(4, fped->GetParameter(2)*1.01, 250);

                fit->SetParLimits(5, 1.0, 4.0);

                double maxfitrange = 100.;    
                double minfitrange = 0.;
                hspe[ipmt-1]->Fit(fit, "MNQLB", "", minfitrange, maxfitrange);
                //cout<<"initial p: "<<fped->GetParameter(1)<<" "<<fped->GetParError(1)<<" "<<fped->GetParameter(2)<<" "<<fped->GetParError(2)<<" "<<fit->GetParameter(4)<<" "<<fit->GetParError(4)<<" "<<fit->GetParameter(3)<<" "<<fit->GetParError(3)<<endl;
                maxfitrange = fped->GetParameter(1)+4*fit->GetParameter(3)+fit->GetParameter(4)+30;
                //if(maxfitrange<100) maxfitrange=100;
                // cout<<"new min ="<<minfitrange<<endl;
                // cout<<"new max ="<<maxfitrange<<endl;
                if(100<maxfitrange) maxfitrange = 100;
                hspe[ipmt-1]->Fit(fit, "MNQLB", "", minfitrange, maxfitrange);
                //cout<<"chi2 = "<<fit->GetChisquare()<<endl;
                //calculate NDOF of fit excluding bins with 0 entries
                int myNDOF=-5; //three free parameters // FIXME
                for(int j=hspe[ipmt-1]->FindBin(minfitrange); j<=hspe[ipmt-1]->FindBin(maxfitrange); j++) { //loop through fitted spe bins
                    if(hspe[ipmt-1]->GetBinContent(j)) myNDOF++;
                } //loop through fitted spe bins

                //cout<<"ndf= "<<myNDOF<<endl;
                //calculate means and integrals of the fit and data
                double fint, fint_error, hint, favg, havg;
                int temp_lowbin, temp_highbin;
                temp_lowbin = hspe[ipmt-1]->FindBin(minfitrange);
                temp_highbin = hspe[ipmt-1]->FindBin(maxfitrange);
                hspe_unscaled->GetXaxis()->SetRangeUser(minfitrange, maxfitrange);
                havg = hspe_unscaled->GetMean();
                hint = hspe[ipmt-1]->Integral(temp_lowbin,temp_highbin,"width");
                double min_frange = hspe[ipmt-1]->GetBinLowEdge(temp_lowbin);
                favg = fit->Mean(min_frange, maxfitrange);
                fint = fit->Integral(min_frange, maxfitrange);
                //fint_error = fit->IntegralError(min_frange, maxfitrange);

                double PE5int = 0; //integral of events with >=5 PE
                double PE5loc =  fped->GetParameter(1)+ 5*fit->GetParameter(3);
                if(PE5loc>500) PE5int = 0;
                else {
                    int PE5bin =  hspe_temp->FindBin(PE5loc);
                    temp_highbin = hspe_temp->FindBin(maxfitrange)-1;
                    PE5int =  hspe_temp->Integral(PE5bin,temp_highbin,"width");
                }
                int PE5flag = 0;
                if(PE5int/hint>0.05) PE5flag = 1; //set flag if more than 5% of events in the fit correspond to >=5PE
                //=========================================    
                //for(int i1=1;i1<hspe->GetNbinsX();i1++){
                //constants_file1<<HV<<"\t"<<iDepth<<"\t"<<iEta<<"\t"<<iPhi<<"\t"<<2.6*hspe->GetBinCenter(i1)<<"\t"<<hspe->GetBinContent(i1)<<"\t"<<fit->Eval(hspe->GetBinCenter(i1))<<"\n";
                //}
                //=========================================    

                //printf("%d\n",myNDOF);
                //output calibrations constants
                //constants_file<<endl<<"LED_amplitude HV Spigot Channel Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag"<<endl;
                //constants_file<<LED_amp<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParError(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fped->GetParError(2)<<" "<<scale*fit->GetParameter(4)<<" "<<scale*fit->GetParError(4)<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<scale*fit->GetParError(3)*fC2electrons<<" "<<fit->GetChisquare()/myNDOF/*fit->GetNDF()*/<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<endl;


                /* Ped_mean[iDepth]->Fill(iEta,iPhi,fped->GetParameter(1));
                   Ped_mean_err[iDepth]->Fill(iEta,iPhi,fped->GetParError(1));
                   Ped_RMS[iDepth]->Fill(iEta,iPhi,fped->GetParameter(2));
                   Ped_RMS_err[iDepth]->Fill(iEta,iPhi,fped->GetParError(2));
                   Gain[iDepth]->Fill(iEta,iPhi,fit->GetParameter(3));
                   Gain_err[iDepth]->Fill(iEta,iPhi,fit->GetParError(3));
                   SPEPeak_RMS[iDepth]->Fill(iEta,iPhi,fit->GetParameter(4));
                   SPEPeak_RMS_err[iDepth]->Fill(iEta,iPhi,fit->GetParError(4));
                   Normalized_Chi2[iDepth]->Fill(iEta,iPhi,fit->GetChisquare()/myNDOF);
                   MeanPE_fit[iDepth]->Fill(iEta,iPhi,fit->GetParameter(0));
                   MeanPE_fit_err[iDepth]->Fill(iEta,iPhi,fit->GetParError(0));
                   MeanPE_estimate[iDepth]->Fill(iEta,iPhi,mu);
                //constants_file<<fit->GetParameter(3)<<" "<<fit->GetParError(3)<<endl;
                 */
                /*
                   if(iDepth==2 && iPhi==53 && iEta==36){
                   cout<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<gain_est<<" "<<fit->GetParameter(3)<<endl;
                   cout<<LED_amp<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParError(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fped->GetParError(2)<<" "<<scale*fit->GetParameter(4)<<" "<<scale*fit->GetParError(4)<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<scale*fit->GetParError(3)*fC2electrons<<" "<<fit->GetChisquare()/myNDOF<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<endl;
                   }
                 */
                int col = column[iPhi][iEta][iDepth];
                int rbx = robox[iPhi][iEta][iDepth];


                //orig	constants_file<<LED_amp<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<fped->GetParameter(1)<<" "<<fped->GetParError(1)<<" "<<fped->GetParameter(2)<<" "<<fped->GetParError(2)<<" "<<fit->GetParameter(4)<<" "<<fit->GetParError(4)<<" "<<fit->GetParameter(3)<<" "<<fit->GetParError(3)<<" "<<fit->GetChisquare()/myNDOF/*fit->GetNDF()*/<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<endl;
                //orig from RH : using fped for pedestal peak info
                //constants_file<<rbx<<" "<<col<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<fped->GetParameter(1)<<" "<<fped->GetParError(1)<<" "<<fped->GetParameter(2)<<" "<<fped->GetParError(2)<<" "<<fit->GetParameter(4)<<" "<<fit->GetParError(4)<<" "<<fit->GetParameter(3)<<" "<<fit->GetParError(3)<<" "<<fit->GetChisquare()/myNDOF/*fit->GetNDF()*/<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<endl;
                constants_file<<rbx<<" "<<col<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<fit->GetParameter(1)<<" "<<fit->GetParError(1)<<" "<<fit->GetParameter(2)<<" "<<fit->GetParError(2)<<" "<<fit->GetParameter(4)<<" "<<fit->GetParError(4)<<" "<<fit->GetParameter(3)<<" "<<fit->GetParError(3)<<" "<<fit->GetChisquare()/myNDOF/*fit->GetNDF()*/<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<endl;

                //cout<<"irbx ibb ipmt"<<irbx<<" "<<ibb<<" "<<ipmt<<endl;
                //cout<<"iPhi iEta iDepth "<<iPhi<<" "<<iEta<<" "<<iDepth<<" col RBX "<<col<<" "<<rbx<<endl;
                Ped_mean[0]->Fill(col,rbx,fit->GetParameter(1));
                Ped_mean_err[0]->Fill(col,rbx,fit->GetParError(1));
                Ped_RMS[0]->Fill(col,rbx,fit->GetParameter(2));
                Ped_RMS_err[0]->Fill(col,rbx,fit->GetParError(2));
                Gain[0]->Fill(col,rbx,fit->GetParameter(3));
                Gain_err[0]->Fill(col,rbx,fit->GetParError(3));
                SPEPeak_RMS[0]->Fill(col,rbx,fit->GetParameter(4));
                SPEPeak_RMS_err[0]->Fill(col,rbx,fit->GetParError(4));
                Normalized_Chi2[0]->Fill(col,rbx,fit->GetChisquare()/myNDOF);
                MeanPE_fit[0]->Fill(col,rbx,fit->GetParameter(0));
                MeanPE_fit_err[0]->Fill(col,rbx,fit->GetParError(0));
                MeanPE_estimate[0]->Fill(col,rbx,mu);
                //ShareMap[0]->Fill(col,rbx,chg_frac);


                proj_Ped_mean[0]->Fill(fit->GetParameter(1));
                proj_Ped_mean_err[0]->Fill(fit->GetParError(1));
                proj_Ped_RMS[0]->Fill(fit->GetParameter(2));
                proj_Ped_RMS_err[0]->Fill(fit->GetParError(2));
                proj_Gain[0]->Fill(fit->GetParameter(3));
                proj_Gain_err[0]->Fill(fit->GetParError(3));
                proj_SPEPeak_RMS[0]->Fill(fit->GetParameter(4));
                proj_SPEPeak_RMS_err[0]->Fill(fit->GetParError(4));
                proj_Normalized_Chi2[0]->Fill(TMath::Log10(fit->GetChisquare()/myNDOF));
                proj_MeanPE_fit[0]->Fill(TMath::Log10(fit->GetParameter(0)));
                proj_MeanPE_fit_err[0]->Fill(TMath::Log10(fit->GetParError(0)));
                proj_MeanPE_estimate[0]->Fill(TMath::Log10(mu));
                //proj_ShareMap[0]->Fill(chg_frac);
                /*float frac = ROOT::Math::gaussian_cdf_c(100.,sqrt(pow(fit->GetParameter(4),2)+pow(fped->GetParameter(2),2)) , fit->GetParameter(3)+fped->GetParameter(1));
                  float trig = frac*fit->GetParameter(0)*pow(10,9)/(25.);*/
                float frac = hspe[ipmt-1]->Integral(hspe[ipmt-1]->FindBin(100),hspe[ipmt-1]->FindBin(499),"width")/ hspe[ipmt-1]->Integral(hspe[ipmt-1]->FindBin(0),hspe[ipmt-1]->FindBin(499),"width");
                float trig = frac*pow(10,9)/(100.);
                Selftrigger_estimate[0]->Fill(col,rbx,trig);
                proj_Selftrigger_estimate[0]->Fill(trig);

                frac = fit->Integral(100,499)/fit->Integral(0,499);
                trig = frac*pow(10,9)/(100.);
                Selftrigger_integral[0]->Fill(col,rbx,trig);
                proj_Selftrigger_integral[0]->Fill(trig);



                if(!pedmode){
                    if(fit->GetChisquare()/myNDOF > 20 || fit->GetParError(3) > 0.6){
                        badfit[irbx][ibb][ipmt] = true;
                    }
                }
                else if(pedmode){
                    if(fit->GetChisquare()/myNDOF > 400 || fit->GetParError(3) > 20){
                        badfit[irbx][ibb][ipmt] = true;
                    }
                }
                //cout<<"eta phi depth pmt "<<iEta<<" "<<iPhi<<" "<<iDepth<<" "<<ipmt<<endl;
                Carray[irbx][ibb]->cd(ipmt);
                // TCanvas *c1 = new TCanvas("c1","c1",800,800);
                TPad *pad1 = new TPad("pad1","pad1",0,0.33,1,1);
                TPad *pad2 = new TPad("pad2","pad2",0,0.05,1,0.33);
                pad1->SetBottomMargin(0.00001);
                pad1->SetBorderMode(0);
                pad1->SetLogy();
                pad2->SetTopMargin(0.00001);
                pad2->SetBottomMargin(0.3);
                pad2->SetBorderMode(0);
                pad1->Draw();
                pad2->Draw();
                pad1->cd();
                /*gPad->SetBorderMode(0);
                  gPad->SetBorderSize(0);
                  gPad->SetRightMargin(0.01);
                  gPad->SetBottomMargin(0.1);
                  gPad->SetLogy(true);*/
                hspe[ipmt-1]->GetXaxis()->SetRangeUser(0, 100 /*200*/ /*300*//*508*/);
                hspe[ipmt-1]->SetLineColor(kBlue);
                //hspe[ipmt-1]->SetLineColor(kBlack);
                hspe[ipmt-1]->SetMarkerColor(kBlack);
                hspe[ipmt-1]->SetMarkerSize(1);
                hspe[ipmt-1]->SetMarkerStyle(20);
                hspe[ipmt-1]->SetStats(false);

                hspe[ipmt-1]->Draw("HIST");

                fit->SetLineWidth(1);
                fit->SetLineColor(kRed);
                fit->SetRange(minfitrange,maxfitrange);
                fit->Draw("same");
                myText(0.5,0.83,Form("Gain = %.2f +/- %.2f",fit->GetParameter(3),fit->GetParError(3)),1,0.05);
                myText(0.5,0.76,Form("SPE RMS = %.2f +/- %.2f",fit->GetParameter(4),fit->GetParError(4)),1,0.05);
                if(pedmode) myText(0.5,0.69,Form("Log(Mean PE) = %.2f",TMath::Log10(fit->GetParameter(0))),1,0.05);
                else myText(0.5,0.69,Form("Mean PE = %.2f",fit->GetParameter(0)),1,0.06);
                myText(0.5,0.62,Form("Chi^{2}/ndf =  %.2f",fit->GetChisquare()/myNDOF/*fit->GetNDF()*/),1,0.05);
                myText(0.5,0.55,Form("Ped = %.2f +/- %.2f",fit->GetParameter(1),fit->GetParError(1)),1,0.05);
                myText(0.5,0.48,Form("Ped RMS = %.2f +/- %.2f",fit->GetParameter(2),fit->GetParError(2)),1,0.05);
                /*	TArrow first = TArrow(maxfitrange,2,maxfitrange,0.1,0.04,"");
                    first.DrawLine(maxfitrange,2,maxfitrange,0.1);*/
                
                // Draw SPE peak 
                TF1 *spefunc = new TF1("spefunc",SPEFunc,0,50,5); 
                spefunc->SetParameters(fit->GetParameter(0),fit->GetParameter(1),fit->GetParameter(2),fit->GetParameter(3),fit->GetParameter(4));
                spefunc->SetLineColor(kGreen);
                spefunc->SetLineWidth(1);
                spefunc->Draw("same");
                
                // Draw PED peak 
                TF1 *pedfunc = new TF1("pedfunc",PEDFunc,0,50,3); 
                pedfunc->SetParameters(fit->GetParameter(0),fit->GetParameter(1),fit->GetParameter(2));
                pedfunc->SetLineColor(kBlack);
                pedfunc->SetLineWidth(1);
                pedfunc->Draw("same");
                
                pad2->cd();

                for(int k=0;k<=hspe[ipmt-1]->FindBin(maxfitrange);k++){
                    Double_t diff = hspe[ipmt-1]->GetBinContent(k)/fit->Eval(hspe[ipmt-1]->GetBinCenter(k));
                    Double_t error = hspe[ipmt-1]->GetBinError(k)/fit->Eval(hspe[ipmt-1]->GetBinCenter(k));
                    resid[ipmt-1]->SetBinContent(k,diff);
                    resid[ipmt-1]->SetBinError(k,error);
                    //if(hspe[ipmt-1]->GetBinContent(k)) resid[ipmt-1]->SetBinContent(k,(hspe[ipmt-1]->GetBinContent(k)-fit->Eval(hspe[ipmt-1]->GetBinCenter(k)))/hspe[ipmt-1]->GetBinError(k));
                }

                resid[ipmt-1]->GetXaxis()->SetRangeUser(0, 100 /*200*/);
                resid[ipmt-1]->SetMaximum(3);
                //resid[ipmt-1]->SetMinimum(-2);
                resid[ipmt-1]->SetLineColor(kBlack);
                resid[ipmt-1]->SetMarkerStyle(20);
                resid[ipmt-1]->SetMarkerSize(1);
                resid[ipmt-1]->SetLabelSize(0.15);
                resid[ipmt-1]->SetLabelSize(0.15,"Y");
                resid[ipmt-1]->SetStats(false);
                resid[ipmt-1]->SetTitle("");
                resid[ipmt-1]->SetXTitle("LADC counts");
                resid[ipmt-1]->GetXaxis()->SetTitleSize(0.15);
                resid[ipmt-1]->Draw("PE");
                //resid[ipmt-1]->Delete();
            }

            if(drawflag[irbx][ibb]) { //draw plots of fit if data for the HV is present
                stringstream plot_name;
                plot_name<<"Plots/SPEFits_Run_"<<run<<"_mode_"<<mode<<"_Robox"<<irbx<<"_BB"<<ibb<<"_"<<filname<<".pdf";
                Carray[irbx][ibb]->SaveAs(plot_name.str().c_str());
                plot_name.str( std::string() );
            }

        }


    }

/*
    for(int irbx = 1; irbx<37; irbx++){
        for(int ibb= 1; ibb<4;ibb++){
            for(int ipmt=1; ipmt<9;ipmt++){
            }
        }
    }
*/
    _hstFile->Write();
    _hstFile->Close();
    constants_file.close();
    //constants_file1.close();

}
예제 #3
0
void SPEFit(char * fLEDname, char * fPEDname, int run, int LED_amp, double cutmax = 250.0)
{

  //set plotting styles
  gStyle->SetCanvasColor(0);
  gStyle->SetPadColor(0);
  gStyle->SetCanvasBorderMode(0);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);

    //set file names
    stringstream out_fname;
    stringstream out_fname1;
    out_fname<<"SPEconstants_Run_"<<run<<".txt";
    out_fname1<<"SPEspec_Run_"<<run<<".txt";

    ofstream  constants_file(out_fname.str().c_str(),ios_base::trunc); 
    //ofstream  constants_file1(out_fname1.str().c_str(),ios_base::trunc); 
    constants_file<<"Run "<<run<<endl;
    constants_file<<"type SPE"<<endl;
    constants_file<<"LED_amplitude "<<LED_amp<<endl<<endl;

    constants_file<<endl<<"LED_amplitude Depth Phi Eta Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag"<<endl;

    out_fname.str("");
    out_fname<<"SPEdistributions_Run_"<<run<<".txt";

    out_fname.str("");
    out_fname<<"SPEextra_Run_"<<run<<".txt";
    //ofstream  extra_file(out_fname.str().c_str(),ios_base::trunc); 


    double scale = 1.0;
    scale = 2.6; //Need to scale up HF charge
    double fC2electrons = 6240.; //convert fC to #electrons

    char spename[128], pedname[128], spehistname[128];
 
    TFile *tfLED = new TFile(fLEDname);
    TFile *tfPED = new TFile(fPEDname);
    


    //const int NnewBins = 106;
    //double binsX[NnewBins] = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,180,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};

    const int NnewBins = 80;
    double binsX[NnewBins] = {0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};	  
    TH1F* hspe = new TH1F("hspe","hspe",NnewBins-1,binsX);


    int NDepth = 2; //number of depths
    int MinDepth = 1;
    int MaxDepth = 2;
    int MinEta = 29; 
    int MaxEta = 41;
    int MinPhi = 41;
    int MaxPhi = 53;
   
 
    TCanvas *Carray[NDepth+1][MaxPhi+1];
    bool drawflag[NDepth+1][MaxPhi+1];  
    TH1F *LED[NDepth+1][MaxEta+1][MaxPhi+1];
    TH1F *PED[NDepth+1][MaxEta+1][MaxPhi+1];

    for(int iDepth = MinDepth; iDepth <= MaxDepth; iDepth++){
      for(int iPhi = MinPhi; iPhi <= MaxPhi; iPhi++){

	bool nonNull = false;

	for(int iEta = MinEta; iEta <= MaxEta; iEta++){

	  sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
	  LED[iDepth][iEta][iPhi]=(TH1F *)tfLED->Get(spename);
	  if(LED[iDepth][iEta][iPhi]) nonNull = true;
      
	  sprintf(spename,"Analyzer/CommonDir/ResPlotDir/Histo_for_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
	  PED[iDepth][iEta][iPhi]=(TH1F *)tfPED->Get(spename);
	}

	drawflag[iDepth][iPhi] = false;
	char canvname[16];
	sprintf(canvname, "c_%d_%d", iDepth,iPhi);
	if(nonNull){ //only create canvas if distributions exist
	  Carray[iDepth][iPhi] = new TCanvas(canvname,canvname,1200,700);
	  Carray[iDepth][iPhi]->Divide(5,3);
	}

      }
    }



    int HV=0;

    for(int iDepth = MinDepth; iDepth <= MaxDepth; iDepth++){
      for(int iPhi = MinPhi; iPhi <= MaxPhi; iPhi++){
	for(int iEta = MinEta; iEta <= MaxEta; iEta++){

	  //cout<<iDepth<<" "<<iPhi<<" "<<iEta<<endl;

	  if(!LED[iDepth][iEta][iPhi]) continue;

	  sprintf(spehistname,"led %d %d %d",iDepth,iEta,iPhi);
	  TH1F *hspe_temp = (TH1F *)LED[iDepth][iEta][iPhi]->Clone(spehistname);
	  sprintf(spehistname,"ped %d %d %d",iDepth,iEta,iPhi);
	  TH1F *hped = (TH1F *)PED[iDepth][iEta][iPhi]->Clone(spehistname);
	  hspe->Reset();
	  sprintf (spehistname, "SumLED_Depth_%d_Eta_%d_Phi_%d",iDepth,iEta,iPhi);
	  hspe->SetTitle(spehistname);

	  //combine bins of original SPE histogram
	  for(int ib=1; ib<=hspe_temp->GetNbinsX(); ib++) {
	    double bin_center = hspe_temp->GetBinCenter(ib);
	    if(bin_center>hspe->GetXaxis()->GetXmax()) continue;
	    int newbin = hspe->FindBin(bin_center);
	    double new_content = hspe->GetBinContent(newbin) + hspe_temp->GetBinContent(ib);
	    double new_error = sqrt(pow(hspe->GetBinError(newbin),2)+pow(hspe_temp->GetBinError(ib),2));
	    hspe->SetBinContent(newbin,new_content);
	    hspe->SetBinError(newbin,new_error);
	  }
	  TH1F* hspe_unscaled = (TH1F*)hspe->Clone("hspe_unscaled");
	  //renormalize bins of new SPE histogram
	  for(int ib=1; ib<=hspe->GetNbinsX(); ib++) {
	    double new_content = hspe->GetBinContent(ib)/hspe->GetXaxis()->GetBinWidth(ib)*hspe_temp->GetXaxis()->GetBinWidth(1);
	    double new_error = hspe->GetBinError(ib)/hspe->GetXaxis()->GetBinWidth(ib)*hspe_temp->GetXaxis()->GetBinWidth(1);
	    hspe->SetBinContent(ib,new_content);
	    hspe->SetBinError(ib,new_error);
	  }
	  
	  if(hspe_temp->Integral()==0) continue;
	  else drawflag[iDepth][iPhi] = true;	  

	  Nev = hspe_temp->Integral()*hspe_temp->GetXaxis()->GetBinWidth(1); 
	  TF1 *fped = new TF1("fped","gaus",0, 80);
	  hped->Fit(fped,"NQR");
	  double pploc = fped->GetParameter(1), ppwidth = fped->GetParameter(2);
	  hspe->Fit(fped, "NQ", "", pploc - 3*ppwidth, pploc + ppwidth);  
	  
	  //estimate SPE peak location
	  int max_SPE_bin, maxbin, Nbins;
	  double max_SPE_height=0, minheight, max_SPE_location;
	  bool minflag = false;
	  maxbin=hspe->FindBin(fped->GetParameter(1)); //location of pedestal peak
	  minheight=hspe->GetBinContent(maxbin); //initialize minheight
	  Nbins = hspe->GetNbinsX();
	  for(int j=maxbin+1; j<Nbins-1; j++) { //start from pedestal peak and loop through bins
	    if(hspe->GetBinContent(j) > minheight && !minflag) minflag=true; //only look for SPE peak when minflag=true
	    if(hspe->GetBinContent(j) < minheight )  minheight = hspe->GetBinContent(j);
	    if(minflag && hspe->GetBinContent(j) > max_SPE_height){
	      max_SPE_bin = j;
	      max_SPE_location = hspe->GetBinCenter(max_SPE_bin);
	      max_SPE_height = hspe->GetBinContent(j);
	    }
	  } //start from pedestal peak and loop through bins
	  //find minimum bin between pedestal and SPE peaks
	  hspe->GetXaxis()->SetRange(maxbin,max_SPE_bin);
	  int minbin = hspe->GetMinimumBin(); 
	  double minbin_location = hspe->GetBinCenter(minbin);
	  hspe->GetXaxis()->SetRange(1,Nbins);	    
	  
	  TF1 *fit = new TF1("fit", FitFun, 0, 500, 5);
	    
	  double mu = - log(fped->Integral(0,100)/Nev);
	  if(mu<0) mu=0.01;
	  double gain_est = max_SPE_location-1.0*fped->GetParameter(1);
	  if(max_SPE_bin > (minbin+1)) fit->SetParameters(mu, 20, 1, gain_est, gain_est*0.5);
	  else fit->SetParameters(mu, 20, 1, 2.1*fped->GetParameter(2), 10); //case of no clear minimum; start looking for SPE peak at 2sigma away from pedestal peak
	  fit->SetParLimits(0, 0, 10);
	  fit->FixParameter(1, fped->GetParameter(1));
	  fit->FixParameter(2, fped->GetParameter(2));
	  fit->SetParLimits(3, fped->GetParameter(2)*2, 350);
	  fit->SetParLimits(4, fped->GetParameter(2)*1.01, 250);
	  

	  double maxfitrange = 500.;    
	  double minfitrange = 0.;
	  hspe->Fit(fit, "MNQL", "", minfitrange, maxfitrange);
	  maxfitrange = fped->GetParameter(1)+4*fit->GetParameter(3)+fit->GetParameter(4);
	  if(500<maxfitrange) maxfitrange = 500;
	  hspe->Fit(fit, "MNQL", "", minfitrange, maxfitrange);

	  //calculate NDOF of fit excluding bins with 0 entries
	  int myNDOF=-3; //three free parameters
	  for(int j=hspe->FindBin(minfitrange); j<=hspe->FindBin(maxfitrange); j++) { //loop through fitted spe bins
	    if(hspe->GetBinContent(j)) myNDOF++;
	  } //loop through fitted spe bins


	  //calculate means and integrals of the fit and data
	  double fint, fint_error, hint, favg, havg;
	  int temp_lowbin, temp_highbin;
	  temp_lowbin = hspe->FindBin(minfitrange);
	  temp_highbin = hspe->FindBin(maxfitrange);
	  hspe_unscaled->GetXaxis()->SetRangeUser(minfitrange, maxfitrange);
	  havg = hspe_unscaled->GetMean();
	  hint = hspe->Integral(temp_lowbin,temp_highbin,"width");
	  double min_frange = hspe->GetBinLowEdge(temp_lowbin);
	  favg = fit->Mean(min_frange, maxfitrange);
	  fint = fit->Integral(min_frange, maxfitrange);
	  //fint_error = fit->IntegralError(min_frange, maxfitrange);
	  
	  double PE5int = 0; //integral of events with >=5 PE
	  double PE5loc =  fped->GetParameter(1)+ 5*fit->GetParameter(3);
	  if(PE5loc>500) PE5int = 0;
	  else {
	    int PE5bin =  hspe_temp->FindBin(PE5loc);
	    temp_highbin = hspe_temp->FindBin(maxfitrange)-1;
	    PE5int =  hspe_temp->Integral(PE5bin,temp_highbin,"width");
	  }
	  int PE5flag = 0;
	  if(PE5int/hint>0.05) PE5flag = 1; //set flag if more than 5% of events in the fit correspond to >=5PE
	//=========================================    
	  //for(int i1=1;i1<hspe->GetNbinsX();i1++){
	    //constants_file1<<HV<<"\t"<<iDepth<<"\t"<<iEta<<"\t"<<iPhi<<"\t"<<2.6*hspe->GetBinCenter(i1)<<"\t"<<hspe->GetBinContent(i1)<<"\t"<<fit->Eval(hspe->GetBinCenter(i1))<<"\n";
          //}
        //=========================================    

	  //printf("%d\n",myNDOF);
	  //output calibrations constants
	  //constants_file<<endl<<"LED_amplitude HV Spigot Channel Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag"<<endl;
	  constants_file<<LED_amp<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParError(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fped->GetParError(2)<<" "<<scale*fit->GetParameter(4)<<" "<<scale*fit->GetParError(4)<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<scale*fit->GetParError(3)*fC2electrons<<" "<<fit->GetChisquare()/myNDOF/*fit->GetNDF()*/<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<endl;
	    

	  /*
	  if(iDepth==2 && iPhi==53 && iEta==36){
	    cout<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<gain_est<<" "<<fit->GetParameter(3)<<endl;
	    cout<<LED_amp<<" "<<iDepth<<" "<<iPhi<<" "<<iEta<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParError(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fped->GetParError(2)<<" "<<scale*fit->GetParameter(4)<<" "<<scale*fit->GetParError(4)<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<scale*fit->GetParError(3)*fC2electrons<<" "<<fit->GetChisquare()/myNDOF<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<endl;
	  }
	  */

	  Carray[iDepth][iPhi]->cd(iEta-MinEta+1);
	  gPad->SetBorderMode(0);
	  gPad->SetBorderSize(0);
	  gPad->SetRightMargin(0.01);
	  gPad->SetBottomMargin(0.1);
	  gPad->SetLogy(true);
	  hspe->GetXaxis()->SetRangeUser(0, 200 /*300*//*508*/);
	  hspe->SetLineColor(kBlue);
	  hspe->DrawClone("hist");
	  fit->SetLineWidth(2);
	  fit->Draw("same");

	}
    
	if(drawflag[iDepth][iPhi]) { //draw plots of fit if data for the HV is present
	  stringstream plot_name;
	  plot_name<<"Plots/SPEFits_Run_"<<run<<"_Depth"<<iDepth<<"_Phi"<<iPhi<<".pdf";
	  Carray[iDepth][iPhi]->SaveAs(plot_name.str().c_str());
	  plot_name.str( std::string() );
	}

      }
    }

    constants_file.close();
    //constants_file1.close();
}
예제 #4
0
void SPEFit(char * fname, int run, int LED_amp, double cutmax = 250.0)
{

  //set plotting styles
  gStyle->SetCanvasColor(0);
  gStyle->SetPadColor(0);
  gStyle->SetCanvasBorderMode(0);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);

    //set file names
    stringstream out_fname;
    stringstream out_fname1;
    out_fname<<"SPEconstants_Run_"<<run<<".txt";
    out_fname1<<"SPEspec_Run_"<<run<<".txt";

    ofstream  constants_file(out_fname.str().c_str(),ios_base::trunc); 
    ofstream  constants_file1(out_fname1.str().c_str(),ios_base::trunc);
    constants_file<<"Run "<<run<<endl;
    constants_file<<"type SPE"<<endl;
    constants_file<<"LED_amplitude "<<LED_amp<<endl<<endl;

    //constants_file<<endl<<"LED_amplitude HV Spigot Channel Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag"<<endl;
    constants_file<<endl<<"LED_amplitude HV Spigot Channel Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag Polya_shape Polya_shape_err Polya_mode"<<endl;

    out_fname.str("");
    out_fname<<"SPEdistributions_Run_"<<run<<".txt";


    out_fname.str("");
    out_fname<<"SPEextra_Run_"<<run<<".txt";
    //ofstream  extra_file(out_fname.str().c_str(),ios_base::trunc); 

    //extra_file<<endl<<"LED_amplitude HV Spigot Channel PedSubtracted_mean Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag"<<endl;


    double scale = 1.0;
    scale = 2.6; //Need to scale up HF charge
    double fC2electrons = 6240.; //convert fC to #electrons

    char spename[128], pedname[128], spehistname[128];
    bool drawflag;   
 
    TFile *tf = new TFile(fname);
    
    TCanvas *c1 = new TCanvas("c1","c1",1200,700);
    c1->Divide(6,4);
    c1->SetBorderMode(0);
    c1->SetBorderSize(0);
    TCanvas *c2 = new TCanvas("c2","c2",1200,700);
    c2->Divide(6,4);
    c2->SetBorderMode(0);
    c2->SetBorderSize(0);  
    TCanvas *c3 = new TCanvas("c3","c3",1200,700);
    c3->Divide(6,4);
    c3->SetBorderMode(0);
    c3->SetBorderSize(0);

    const int NnewBins = 106;
    double binsX[NnewBins] = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,180,190,200,210,220,230,240,250,266,282,298,316,336,356,378,404,430,456,482,500};	  
    TH1F* hspe = new TH1F("hspe","hspe",NnewBins-1,binsX);

    int Npoints;
    TH2D *tmp;
    for(Npoints=0;Npoints<10;Npoints++){
      sprintf(spename,"spetest/spigot_%d/bb_%d/LED_HVset_%d_sp_%d_BB_%d",0,1,Npoints,0,1);
      tmp=(TH2D *)tf->Get(spename);
      if(tmp==0) break;
    }
    TH2D *LED[3][3][20];
    TH2D *PED[3][3];
    for(int iSpig = 0; iSpig < 3; iSpig++)for(int bb = 1; bb < 4; bb++){
        for(int ii=0; ii<Npoints; ii++){
           sprintf(spename,"spetest/spigot_%d/bb_%d/LED_HVset_%d_sp_%d_BB_%d",iSpig,bb,ii,iSpig,bb);
           LED[iSpig][bb-1][ii]=(TH2D *)tf->Get(spename);
        }
        sprintf(spename,"spetest/spigot_%d/bb_%d/PED_sp_%d_BB_%d",iSpig,bb,iSpig,bb);
        PED[iSpig][bb-1]=(TH2D *)tf->Get(spename);
    }


    for(int ii=0; ii<Npoints; ii++) {
      drawflag=false;
      int HV=0;
      for (int iSpig = 0; iSpig < 3; iSpig++) {
	for(int i = 0; i < 24; i++) {
            int bb=BB_MAP[i];
            int pmt=PMT_MAP[i];
            sprintf(spehistname,"led %d %d %d",ii,iSpig,i);
            TH1D *hspe_temp = (TH1D *)LED[iSpig][bb-1][ii]->ProjectionX(spehistname,pmt,pmt,"")->Clone();
            sprintf(spehistname,"ped %d %d %d",ii,iSpig,i);
            TH1D *hped = (TH1D *)PED[iSpig][bb-1]->ProjectionX(spehistname,pmt,pmt,"")->Clone();
            sscanf(&hspe_temp->GetTitle()[7],"%d",&HV);
	    hspe->Reset();
            sprintf (spehistname, "SumLED%d_sp_%d_ch_%d", HV, iSpig, i);
	    hspe->SetTitle(spehistname);

	    //combine bins of original SPE histogram
	    for(int ib=1; ib<=hspe_temp->GetNbinsX(); ib++) {
	      double bin_center = hspe_temp->GetBinCenter(ib);
	      if(bin_center>hspe->GetXaxis()->GetXmax()) continue;
	      int newbin = hspe->FindBin(bin_center);
	      double new_content = hspe->GetBinContent(newbin) + hspe_temp->GetBinContent(ib);
	      double new_error = sqrt(pow(hspe->GetBinError(newbin),2)+pow(hspe_temp->GetBinError(ib),2));
	      hspe->SetBinContent(newbin,new_content);
	      hspe->SetBinError(newbin,new_error);
	    }
	    TH1F* hspe_unscaled = (TH1F*)hspe->Clone("hspe_unscaled");

	    //renormalize bins of new SPE histogram
	    for(int ib=1; ib<=hspe->GetNbinsX(); ib++) {
	      double new_content = hspe->GetBinContent(ib)/hspe->GetXaxis()->GetBinWidth(ib)*hspe_temp->GetXaxis()->GetBinWidth(1);
	      double new_error = hspe->GetBinError(ib)/hspe->GetXaxis()->GetBinWidth(ib)*hspe_temp->GetXaxis()->GetBinWidth(1);
	      hspe->SetBinContent(ib,new_content);
	      hspe->SetBinError(ib,new_error);
	    }
	    
	    if(hspe_temp->Integral()==0) continue;
	    else drawflag=true;	  
    
            Nev = hspe_temp->Integral()*hspe_temp->GetXaxis()->GetBinWidth(1); 
	    
	    TF1 *fped = new TF1("fped","gaus",0, 80);
	    hped->Fit(fped,"NQR");
	    double pploc = fped->GetParameter(1), ppwidth = fped->GetParameter(2);
	    //cout<<"Ped only: ped mean "<<fped->GetParameter(1)<<", ped width "<<fped->GetParameter(2)<<" normalization "<<fped->GetParameter(0)<<endl;
	    hspe->Fit(fped, "NQ", "", pploc - 3*ppwidth, pploc + ppwidth);
	    //cout<<"SPE distribution: ped mean "<<fped->GetParameter(1)<<", ped width "<<fped->GetParameter(2)<<" normalization "<<fped->GetParameter(0)<<endl;
	    
	    
	    //estimate SPE peak location
	    int max_SPE_bin, maxbin, Nbins;
	    double max_SPE_height=0, minheight, max_SPE_location;
	    bool minflag = false;
	    maxbin=hspe->FindBin(fped->GetParameter(1)); //location of pedestal peak
	    minheight=hspe->GetBinContent(maxbin); //initialize minheight
	    /*
	    int maxped_bin = hspe->GetMaximumBin();
	    int maxped_binheight = hspe->GetBinContent(maxped_bin);
	    minheight = maxped_binheight;
	    */
	    Nbins = hspe->GetNbinsX();
	    for(int j=/*maxped_bin*/maxbin+1; j<Nbins-1; j++) { //start from pedestal peak and loop through bins
	      if(hspe->GetBinContent(j) > minheight && !minflag) minflag=true; //only look for SPE peak when minflag=true
	      if(hspe->GetBinContent(j) < minheight )  minheight = hspe->GetBinContent(j);
	      if(minflag && hspe->GetBinContent(j) > max_SPE_height){
		max_SPE_bin = j;
		max_SPE_location = hspe->GetBinCenter(max_SPE_bin);
		max_SPE_height = hspe->GetBinContent(j);
	      }
	    } //start from pedestal peak and loop through bins
	    //find minimum bin between pedestal and SPE peaks
	    hspe->GetXaxis()->SetRange(maxbin,max_SPE_bin);
	    int minbin = hspe->GetMinimumBin(); 
	    double minbin_location = hspe->GetBinCenter(minbin);
	    hspe->GetXaxis()->SetRange(1,Nbins);
	    
	    
	    TF1 *fit = new TF1("fit", FitFun, 0, 500, 5);
	    
	    double mu = - log(fped->Integral(0,100)/Nev);
	    if(mu<0) mu=0.01;
	    double gain_est = max_SPE_location-1.0*fped->GetParameter(1);
	    if(max_SPE_bin > (minbin+1)) fit->SetParameters(mu, 20, 1, gain_est, 3.0);
	    else fit->SetParameters(mu, 20, 1, 2.1*fped->GetParameter(2), 3.0); //case of no clear minimum; start looking for SPE peak at 2sigma away from pedestal peak
	    fit->SetParLimits(0, 0, 10);
	    fit->FixParameter(1, fped->GetParameter(1));
	    fit->FixParameter(2, fped->GetParameter(2));
	    fit->SetParLimits(3, fped->GetParameter(2)*2, 350);
	    fit->SetParLimits(4, 1.01, 100.);


	    double maxfitrange = 500.;    
	    double minfitrange = 0.;
	    hspe->Fit(fit, "MNQL", "", minfitrange, maxfitrange);
	    double rms_estimate = fit->GetParameter(3)/sqrt(fit->GetParameter(4));
            maxfitrange = fped->GetParameter(1)+4*fit->GetParameter(3)+rms_estimate;

 //  cout<<"estimate of gain "<<gain_est<<", fit "<<fit->GetParameter(3)<<endl;
 //  cout<<"Shape Parameter "<<fit->GetParameter(4)<<endl;
 //  cout<<"SPE width "<<rms_estimate<<endl;
 //  cout<<"maxfitrange "<<maxfitrange<<endl;

            if(500<maxfitrange) maxfitrange = 500;
	    hspe->Fit(fit, "MNQL", "", minfitrange, maxfitrange);

	    //calculate NDOF of fit excluding bins with 0 entries
	    int myNDOF=-3; //three free parameters
	    for(int j=hspe->FindBin(minfitrange); j<=hspe->FindBin(maxfitrange); j++) { //loop through fitted spe bins
	      if(hspe->GetBinContent(j)) myNDOF++;
            } //loop through fitted spe bins


//	    cout<<"estimate of gain "<<gain_est<<", fit "<<fit->GetParameter(3)<<endl;
//	    cout<<"Shape Parameter "<<fit->GetParameter(4)<<endl;
//	    double SPE_rms = fit->GetParameter(3)/sqrt(fit->GetParameter(4));
//	    cout<<"SPE width "<<SPE_rms<<endl;

	    //cout<<"SPE width "<<fit->GetParameter(4)<<endl;
	    //cout<<"Fit normalization constant: estimate "<<mu<<" fit "<<fit->GetParameter(0)<<endl;
	    //cout<<spename<<endl;


	    //calculate means and integrals of the fit and data
	    double fint, fint_error, hint, favg, havg;
	    int temp_lowbin, temp_highbin;
	    temp_lowbin = hspe->FindBin(minfitrange);
	    temp_highbin = hspe->FindBin(maxfitrange);
	    hspe_unscaled->GetXaxis()->SetRangeUser(minfitrange, maxfitrange);
	    havg = hspe_unscaled->GetMean();
	    hint = hspe->Integral(temp_lowbin,temp_highbin,"width");
	    double min_frange = hspe->GetBinLowEdge(temp_lowbin);
	    favg = fit->Mean(min_frange, maxfitrange);
	    fint = fit->Integral(min_frange, maxfitrange);
	    //fint_error = fit->IntegralError(min_frange, maxfitrange);
	    
	    double PE5int = 0; //integral of events with >=5 PE
	    double PE5loc =  fped->GetParameter(1)+ 5*fit->GetParameter(3);
	    if(PE5loc>500) PE5int = 0;
	    else {
	      int PE5bin =  hspe_temp->FindBin(PE5loc);
	      temp_highbin = hspe_temp->FindBin(maxfitrange)-1;
	      PE5int =  hspe_temp->Integral(PE5bin,temp_highbin,"width");
	    }
	    int PE5flag = 0;
	    if(PE5int/hint>0.05) PE5flag = 1; //set flag if more than 5% of events in the fit correspond to >=5PE
        //=========================================    
            for(int i1=1;i1<hspe->GetNbinsX();i1++){
constants_file1<<HV<<"\t"<<iSpig<<"\t"<<i<<"\t"<<2.6*hspe->GetBinCenter(i1)<<"\t"<<hspe->GetBinContent(i1)<<"\t"<<fit->Eval(hspe->GetBinCenter(i1))<<"\n";
            }
        //=========================================    


	    //output calibrations constants
	    //constants_file<<endl<<"LED_amplitude HV Spigot Channel Ped_mean Ped_mean_err Ped_RMS  Ped_RMS_err SPEPeak_RMS SPEPeak_RMS_err Gain Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag Polya_shape Polya_shape_err Polya_mode"<<endl;
	    constants_file<<LED_amp<<" "<<HV<<" "<<iSpig<<" "<<(bb-1)*8+pmt<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParError(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fped->GetParError(2)<<" "<<scale*fit->GetParameter(3)/sqrt(fit->GetParameter(4))<<" "<<0<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<scale*fit->GetParError(3)*fC2electrons<<" "<<fit->GetChisquare()/myNDOF/*fit->GetNDF()*/<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<" "<<fit->GetParameter(4)<<" "<<fit->GetParError(4)<<" "<<scale*(fit->GetParameter(4)-1.0)/fit->GetParameter(4)*fit->GetParameter(3)*fC2electrons<<endl;



//	    cout<<LED_amp<<" "<<HV<<" "<<iSpig<<" "<<QIECh[i]<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParError(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fped->GetParError(2)<<" "<<scale*fit->GetParameter(4)<<" "<<scale*fit->GetParError(4)<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<scale*fit->GetParError(3)*fC2electrons<<" "<<fit->GetChisquare()/fit->GetNDF()<<" "<<fit->GetChisquare()<<" "<<fit->GetNDF()<<" "<<myNDOF<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<endl;
	   
	    //extra_file<<endl<<"LED_amplitude HV Spigot Channel SignalAvg_inFitRange FitAvg_inFitRange SignalInt_inFitRange FitInt_inFitRange PEge5Int Gain(fC) Gain_err Normalized_Chi2 MeanPE_fit MeanPE_fit_err MeanPE_estimate PE5flag"<<endl;
	    //extra_file<<LED_amp<<" "<<HV<<" "<<iSpig<<" "<<QIECh[i]<<" "<<scale*havg<<" "<<scale*favg<<" "<<hint<<" "<<fint<<" "<<PE5int<<" "<<scale*fit->GetParameter(3)<<" "<<scale*fit->GetParError(3)<<" "<<fit->GetChisquare()/myNDOF<<" "<<fit->GetParameter(0)<<" "<<fit->GetParError(0)<<" "<<mu<<" "<<PE5flag<<endl;


	    //cout<<"# Spigot Channel Ped_mean Ped_RMS SPE_PeakRMS Gain Normalized_Chi2 Avg_PE"<<endl;
	    //cout<<iSpig<<" "<<i<<" "<<scale*fped->GetParameter(1)<<" "<<scale*fped->GetParameter(2)<<" "<<scale*fit->GetParameter(4)<<" "<<scale*fit->GetParameter(3)*fC2electrons<<" "<<fit->GetChisquare()/fit->GetNDF()<<" "<<fit->GetParameter(0)<<endl<<endl;
	    
	    
	  
	    if(iSpig==0) c1->cd(i+1);
	    else if(iSpig==1) c2->cd(i+1);
            else if(iSpig==2) c3->cd(i+1);
	    gPad->SetBorderMode(0);
	    gPad->SetBorderSize(0);
	    gPad->SetRightMargin(0.01);
	    gPad->SetBottomMargin(0.1);
	    gPad->SetLogy(true);
	    hspe->GetXaxis()->SetRangeUser(0, /*300*/508);
	    hspe->SetLineColor(kBlue);
	    hspe->DrawClone("hist");
	    fit->SetLineWidth(2);
	    fit->Draw("same");
	   
	  }
      }

      if(drawflag) { //draw plots of fit if data for the HV is present
        stringstream plot_name;
        //plot_name<<"Plots/SPEFits_Spigot0_Run_"<<run<<"_HV"<<HV<<".pdf";
        plot_name<<"Plots/SPEFits_Spigot0_Run_"<<run<<"_HV"<<HV<<"_config"<<ii<<".pdf";
        c1->SaveAs(plot_name.str().c_str());
        plot_name.str( std::string() );
        //plot_name<<"Plots/SPEFits_Spigot1_Run_"<<run<<"_HV"<<HV<<".pdf";
        plot_name<<"Plots/SPEFits_Spigot1_Run_"<<run<<"_HV"<<HV<<"_config"<<ii<<".pdf";
        c2->SaveAs(plot_name.str().c_str());
        plot_name.str( std::string() );
        //plot_name<<"Plots/SPEFits_Spigot2_Run_"<<run<<"_HV"<<HV<<".pdf";
        plot_name<<"Plots/SPEFits_Spigot2_Run_"<<run<<"_HV"<<HV<<"_config"<<ii<<".pdf";
        c3->SaveAs(plot_name.str().c_str());
      }

    } //HV loop

    constants_file.close();
    constants_file1.close();
}