Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:21

0001 // Convert calibration data (10 vcal points) to a ntuple
0002 // Reading ascii files in CINT can be slow.
0003 // Try to precompile.
0004 // .x make_ntuple_data.C+
0005 //
0006 
0007 #include <iostream>
0008 #include <fstream>
0009 #include <cmath>
0010 #include <fcntl.h>
0011 
0012 #if !defined(__CINT__)
0013 #include <TH2.h>
0014 #include <TF1.h>
0015 #include <TNtuple.h>
0016 #endif
0017   
0018 //#define PRINT 
0019 
0020 TNtuple *  ntuple;  // to see outside the macro.
0021 
0022 int make_ntuple_data() {   // run with .x
0023 //int main() {   // run with .L & main()
0024 
0025 // INput file 
0026   ifstream in_file;  // data file pointer
0027   char filename[80] = "phCalibration_C0.dat";  
0028   in_file.open(filename, ios::in ); // in C++
0029 
0030   cout << in_file.eof() << " " << in_file.bad() << " " 
0031        << in_file.fail() << " " << in_file.good()<<endl; 
0032   
0033   if (in_file.fail()) {
0034     cout << " File not found " << endl;
0035     return(1); // signal error
0036   }
0037   cout << " file opened : " << filename << endl;
0038 
0039   char line[500];
0040   for (int i = 0; i < 3; i++) {
0041     in_file.getline(line, 500,'\n');
0042     cout<<line<<endl;
0043   }
0044   // Create NTuple 
0045   const int nvar=13;
0046   char *chvar = "d0:d1:d2:d3:d4:d5:d6:d7:d8:d9:roc:col:row";
0047   tfile = new TFile ( "phCalibration_C0.root" , "RECREATE" );
0048   ntuple = new TNtuple("ntuple","NTUPLE",chvar);
0049   float p[nvar];
0050   
0051   float count[10] = {0.,0.,0.,0.,0.,0.,0.,0.,0.,0.};
0052   float sum[10] = {0.,0.,0.,0.,0.,0.,0.,0.,0.,0.};
0053   float par[10];
0054   int rocid=0,colid,rowid;
0055   string name;
0056   string str[10];
0057   //char[100] str;
0058 
0059   // Read data for all pixels
0060   for(int i=0;i<(52*80);i++)  { // loop over pixels
0061    //for(int i=0;i<10;i++)  { // loop over pixels
0062    
0063 
0064    //for(int i0=0;i0<10;i0++) fscanf(in_file, "%s", str);
0065      
0066    //in_file >> par[0] >> par[1] >> par[2] >> par[3] >> par[4]
0067    // >> par[5] >> par[6] >> par[7] >> par[8] >> par[9]
0068    in_file >> str[0] >> str[1] >> str[2] >> str[3] >> str[4]
0069        >> str[5] >> str[6] >> str[7] >> str[8] >> str[9]
0070        >> name >> colid >> rowid;
0071 
0072    if (in_file.fail()) { // check for errors
0073      cout << "Cannot read data file" << endl;
0074      return(1);
0075    }
0076    if ( in_file.eof() != 0 ) {
0077      cout<< " end of file " << endl;
0078      break;;
0079    }
0080    
0081 #ifdef PRINT
0082    cout << " line " << i <<" ";
0083    for(int i1 =0;i1<10;i1++) { cout<<str[i1]<<" "; }
0084    cout<<colid<<" "<<rowid<<endl;
0085 #endif
0086    //cout <<i<<" "<<colid<<" "<<rowid<<endl;
0087    
0088    for(int i2 =0;i2<10;i2++) { 
0089      if( str[i2] == "N/A" ) {
0090        //cout<<" skip N/A"<<endl;
0091        p[i2]=-9999.;
0092      } else {
0093        p[i2]=atof(str[i2].c_str());
0094        count[i2]++;
0095        sum[i2] += p[i2];
0096      }
0097    }
0098    
0099    p[10]=float(rocid);
0100    p[11]=float(colid);
0101    p[12]=float(rowid);
0102    ntuple->Fill(p); 
0103    
0104    //cout << " line " << i <<" ";
0105    //for(int i1 =0;i1<10;i1++) { cout<<p[i1]<<" "; }
0106    //cout<<colid<<" "<<rowid<<endl;
0107    
0108  }   
0109  
0110  /* Visualization */
0111  //ntuple->Draw("adc1:delay"); 
0112  
0113  in_file.close();
0114  cout << " Close input file " << endl;
0115  tfile->Write();
0116  tfile->Close();
0117 
0118  for(int i2 =0;i2<10;i2++) { 
0119    par[i2] = sum[i2]/count[i2];
0120    cout<<par[i2]<<" ";
0121  }
0122  cout<<endl;
0123  
0124  return(0);
0125 }
0126 
0127 
0128 
0129