Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-04-05 08:15:44

0001 #include <iostream>
0002 #include <iomanip>
0003 #include <fstream>
0004 
0005 #include "caen_correction.h"
0006 #include "packet.h"
0007 
0008 using namespace std;
0009 
0010 caen_correction::caen_correction ( const char *calibdata) 
0011 { 
0012 
0013   int nsample[9];
0014 
0015   _broken = 0;
0016   _samples = 1024;
0017   
0018   int index;
0019   ifstream IN;
0020 
0021   int adccorr[9];
0022   float timecorr;
0023 
0024   IN.open(calibdata,ios_base::in);
0025 
0026   int chip;
0027   int i;
0028   if ( ! IN.is_open())
0029     {
0030       _broken = 1;
0031     }
0032   else 
0033     {
0034       for  ( chip = 0; chip < 4; chip++)
0035     {
0036       for ( i = 0; i < 1024; i++)
0037         {
0038           IN >> index
0039             >> adccorr[0]
0040             >> adccorr[1]
0041             >> adccorr[2]
0042             >> adccorr[3]
0043             >> adccorr[4]
0044             >> adccorr[5]
0045             >> adccorr[6]
0046             >> adccorr[7]
0047             >> adccorr[8]
0048             >> timecorr
0049             >> nsample[0]
0050             >> nsample[1]
0051             >> nsample[2]
0052             >> nsample[3]
0053             >> nsample[4]
0054             >> nsample[5]
0055             >> nsample[6]
0056             >> nsample[7]
0057           >> nsample[8];
0058           
0059           base [index][chip*9 + 0] = adccorr[0];
0060           base [index][chip*9 + 1]= adccorr[1];
0061           base [index][chip*9 + 2]= adccorr[2];
0062           base [index][chip*9 + 3]= adccorr[3];
0063           base [index][chip*9 + 4]= adccorr[4];
0064           base [index][chip*9 + 5]= adccorr[5];
0065           base [index][chip*9 + 6]= adccorr[6];
0066           base [index][chip*9 + 7]= adccorr[7];
0067           base [index][chip*9 + 8]= adccorr[8];
0068           timevec[index][chip] = timecorr;
0069           //std::cout << __FILE__ << " " << __LINE__ << setw(3) << chip << setw(5) << index << " " << setw(5) << timecorr << endl;
0070         }
0071     }
0072       IN.close();
0073     }
0074 }
0075 
0076 int caen_correction::init (Packet *p)
0077 {
0078   int chip,c,i, idx;
0079 
0080   for ( chip = 0; chip < 4; chip++)
0081     {
0082 
0083       _samples = p->iValue(chip,"SAMPLES");
0084       int cell = p->iValue(chip,"INDEXCELL");
0085       idx = cell;
0086       
0087       //correct time for each chip
0088       double tstart = timevec[cell][chip];
0089       double t_end = timevec[1023][chip];
0090       
0091       current_time[0][chip] = 0;
0092       
0093       for ( i = 0; i < _samples; i++)
0094     {
0095       double tc;
0096       if ( i+cell < 1024)
0097         {
0098           int j = i+cell;
0099           tc = timevec[j][chip];
0100           current_time[i][chip] = tc - tstart;
0101         }
0102       else
0103         {
0104           int j = i+cell -1023;
0105           tc = timevec[j][chip];
0106           current_time[i][chip] = tc + t_end - tstart;  
0107         }
0108       // if ( chip == 0) cout << setw(4) << i << "  "
0109       //               << setw(6) << current_time[i][chip] << " "
0110       //               << setw(6)  << tc <<  "  " << endl; 
0111 
0112 
0113     }
0114 
0115       
0116       // the adc samples
0117       for ( c = 0; c < 8; c++)  
0118     {
0119       idx = cell;
0120       for ( i = 0; i < _samples; i++)
0121         {
0122           current_wave[i][chip*8+c] = p->iValue(i,chip*8+c) - base[idx][chip*9+c];
0123           idx++;
0124           if (idx >=1024) idx=0;
0125         }
0126     }
0127 
0128       // Trigger cells;
0129       {
0130     idx = cell;
0131     for ( i = 0; i < _samples; i++)
0132       {
0133         current_wave[i][32+chip] = p->iValue(i,32+chip) - base[idx][chip*9+8];
0134         idx++;
0135         if (idx >=1024) idx=0;
0136       }
0137       }
0138     }
0139   return 0;
0140 }
0141       
0142 float caen_correction::caen_corrected(const int sample, const int channel) const
0143 {
0144   if ( sample < 0 || sample >=_samples || channel < 0 || channel > 35) return 0;
0145   return current_wave[sample][channel];
0146 }
0147 
0148 float caen_correction::caen_time(const int sample, const int channel) const
0149 {
0150   //if ( sample < 0 || sample >1023 || channel < 0 || channel > 31) return 0;
0151   //return current_time[sample][channel/8];
0152   if ( sample < 0 || sample >1023 || channel < 0 || channel > 35) return 0;
0153   return current_time[sample][channel < 32 ? channel/8 : channel-32];
0154 }