StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fps_make_ped_files.C
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <time>
5 #include <cstdlib>
6 #include <math>
7 
8 
9 using namespace std;
10 
11 static int startday=80;//Starting day of using physics pedestals
12 static int endday=365;//Last day of run( default end of year )
13 
14 static int NQ = 4;
15 static int NL = 3;
16 
17 static const int NRUN=10000;//some large number of runs for declaring arrays
18 static int nrun=0;//Acutal number of runs
19 static int ngoodrunP=0;//good run peaks point counter for the TGraph gP. Also number of good standby runs
20 static int ngoodrunS=0;//good run sigma point counter for the TGraph gS. Also number of good physics runs
21 static int NGOODS[NRUN];//Array that holds the run number index for the values array that corresponds to a good Standby runs
22 static int NGOODP[NRUN];//Array that holds the run number index for the values array that corresponds to a good Physics runs
23 
24 static const int NID=252;//Maximum IDs FPS
25 static const int NVAL=8; //0=peak,1=sigma,2=rms,3=p95,4=p98,5=mip error,6=sigma error,7=sigma/peak
26 static int run[NRUN];//holds run number from sorted run list for that particular run
27 static int flag[NRUN];//flag for a given run (seems to only equal 1 since pedestal)
28 static Long64_t frun[NRUN];//run number from the file name for a given run
29 static Long64_t idx[NRUN];//Array that will hold sorted run numbers from 'frun'
30 static double values[NID][NVAL][NRUN];//Holds all the values for a given id, index 0=peak,1=sigma,2=rms,3=p95,4=p98, and run
31 
32 //Function to get correct ID for FPS
33 int getID(int q, int l, int s)
34 {
35  return (q-1)*3*21 + (l-1)*21 + (s-1);
36 }
37 
38 //Function that replaces one character with another
39 //Mostly used to replace ',' and '_' with spaces so stringstream can correctly assign values
40 string ReplaceChar( const string &orig, const char char1, const char char2 )
41 {
42  string copy = orig;
43  for( unsigned int i = 0; i < copy.length(); i++ )
44  {
45  if( copy.at(i) == char1 )
46  {
47  copy.at(i) = char2;
48  }
49  }
50  return copy;
51 }//ReplaceChar( const string &orig, const char char1, const char char2 )
52 
53 //Function to read the data from a particular runnumber. Gets called in readall
54 void read(int irun, int runnum){
55  //cout << "in read" << endl;
56  char file[100];
57  int yearday=runnum/1000;
58  sprintf(file,"www/fps/%d/%d.mip.txt",yearday,runnum);
59  ifstream iFile(file);
60  if(!iFile.is_open()) { printf("Failed to open %s\n",file); return; }
61  printf("irun=%d Reading %s\n",irun,file);
62  run[irun]=runnum;
63  flag[irun]=1;
64 
65  int nzero=0, nrms1=0;
66  for(int id=0; id<NID; id++){
67  int i,q,l,s;
68  float peak,sigma,rms,p95,p98,miperr,sigerr;
69  iFile >> i >> q >> l >> s >> peak >> sigma >> rms >> p95 >> p98 >> miperr >> sigerr;
70  //printf("%d %d %d %d %d %f %f %f %f %f\n",irun,i,q,l,s,peak,sigma,rms,p95,p98);
71  values[i][0][irun]=peak;
72  values[i][1][irun]=sigma;
73  values[i][2][irun]=rms;
74  values[i][3][irun]=p95;
75  values[i][4][irun]=p98;
76  values[i][5][irun]=miperr;
77  values[i][6][irun]=sigerr;
78  values[i][7][irun]=sigma/peak;
79  if(peak<20.0) nzero++;
80  if(rms<0.7) nrms1++;
81  }
82  iFile.close();
83  printf("nzero=%4d nrms1=%4d\n",nzero,nrms1);
84  if(nzero<20){
85  for(int id=0; id<NID; id++){
86  for(int v=0; v<NVAL; v++){
87  if(nrms1>100){
88  //cout << "Setpoint yes" << endl;
89  //gS[id][v]->SetPoint(ngoodrunS,double(ngoodrunS),values[id][v][irun]);
90  NGOODS[ngoodrunS] = irun;
91  //cout << "finished setting point" << endl;
92  }else{
93  //cout << "Setpoint no" << endl;
94  //gP[id][v]->SetPoint(ngoodrunP,double(ngoodrunP),values[id][v][irun]);
95  NGOODP[ngoodrunP] = irun;
96  }
97  }
98  }
99  if(nrms1>100){
100  ngoodrunS++;
101  }else{
102  ngoodrunP++;
103  }
104  }
105 }
106 
107 //Function to read all the data from a particular yearday
108 void readall(int day){
109  //cout << "in readall" << endl;
110  Long64_t i=0;
111  char tmp[100];
112  Long64_t RunNum;
113  int year=day/1000;
114  int yearday=day%1000;
115  //printf("%d %d %d\n",day,year,yearday);
116  if(yearday!=0) {startday=yearday; endday=yearday;}
117  for(int d=startday; d<=endday; d++){
118  int yday=year*1000+d;
119  //cout << "Yday: " << yday << endl;
120  char dirname[100]; sprintf(dirname,"../www/fps/%5d/",yday);
121  //printf("Directory name: %s\n",dirname);
122  TSystemDirectory dir(dirname,dirname);
123  TList *files = dir.GetListOfFiles();
124  //cout << "in d for loop" << endl;
125  if(files){
126  TSystemFile *file;
127  TString fname;
128  TIter next(files);
129  //cout << "in if(files)" << endl;
130  while ((file=(TSystemFile*)next())) {
131  fname = file->GetName();
132  if (!file->IsDirectory() && fname.EndsWith(".mip.txt")) {
133  string filename = fname.Data();
134  string s_yearday = filename.substr(0,8);
135  stringstream s_yday(s_yearday);
136  s_yday >> RunNum;
137  s_yday.clear();
138  frun[i]=RunNum;
139  //cout << filename << " " << frun[i] << endl;
140  i++;
141  //cout << "in while loop" << endl;
142  }
143  }
144  }
145  }
146  nrun=i;
147  TMath::Sort(i,frun,idx,0);
148  //cout << "sorted run numbers" << endl;
149  cout << "Number of runs: " << nrun << endl;
150  for(int j=0; j<nrun; j++)
151  {
152  //cout << "Reading each file" << endl;
153  //cout << "J:"<<j << "|IDX:"<<idx[j] << "|frun:"<<frun[idx[j]] << endl;
154  read(j,(int)frun[idx[j]]);
155  }
156 }
157 
158 void make_ped_text_files()
159 {
160 
161  for( int k = 0; k < ngoodrunP; k++ )
162  {
163  stringstream FileName;
164  FileName << "fps_good_physics_ped/" << frun[idx[NGOODP[k]]] << ".txt";
165  ofstream out_file(FileName.str().c_str());
166 
167  for( int id=0; id < NID; id++ )
168  {
169  out_file << id << " " << values[id][0][NGOODP[k]] << " " << values[id][1][NGOODP[k]] << endl;
170  }
171  out_file.close();
172  }
173 
174  return;
175 }
176 
177 //The quad and layer argements are so that I can run it only once or for all 0 for both means all
178 void fps_make_ped_files( int day=18000 )
179 {
180 
181  memset(values,0,sizeof(values));
182 
183 
184  cout << "Reading all the files" << endl;
185  readall(day);
186  cout << "Finshed Reading" << endl;
187  make_ped_text_files();
188 
189  return;
190 
191 }
192