00001 #include <map>
00002 #include <fstream>
00003 #include "TKey.h"
00004 #include <TROOT.h>
00005 #include <TRint.h>
00006 #include <TKey.h>
00007 #include <TTree.h>
00008 #include <TFile.h>
00009 #include <TSystem.h>
00010 #include <iostream>
00011 #include <TGraphErrors.h>
00012 #include <TF1.h>
00013 #include <TH1F.h>
00014 #include <TCanvas.h>
00015 #include <sstream>
00016 #include "Log.hh"
00017
00018 #include "root/MTRun.hh"
00019 #include "root/MTEvent.hh"
00020 #include "root/MTChannel.hh"
00021 #include "root/MTMicrorocChip.hh"
00022
00023 #include "MicroException.hh"
00024
00025
00026 using namespace std;
00027
00028
00029 int main(int argc, char**argv){
00030
00031 if (argc!=6){
00032
00033 cout<<"/*****************************************************************************************/"<<endl;
00034 cout<<"/* ARGUMENTS TO THE PROGRAM **************************************************************/"<<endl;
00035 cout<<"/* 1 - DIRECTORY OF THE 5 TEXT FILES WITH SCURVE PARAMETERS FOR THE 5 DIFFERENT OFFSETS */"<<endl;
00036 cout<<"/* 2 - ASU NUMBER: 1314, 1516, 1718, 2021, 2223, 2425 etc... */"<<endl;
00037 cout<<"/* 3 - RUN NUMBER */"<<endl;
00038 cout<<"/* 4 - VERBOSE (0/1) */"<<endl;
00039 cout<<"/* 5 - WRITE TO FILE (0/1) */"<<endl;
00040 cout<<"/*****************************************************************************************/"<<endl;}
00041
00042 else{
00043
00044
00045 TString dir = argv[1];
00046 TString asu = argv[2];
00047 TString run = argv[3];
00048 int verbose = atoi(argv[4]);
00049 int writetofile = atoi(argv[5]);
00050
00051 int nsigma = 7;
00052
00053
00054 TString file0 = dir + "scurve_noise_offset0_dac0_run" + run + "_asu" + asu + ".txt";
00055 TString file3 = dir + "scurve_noise_offset3_dac0_run" + run + "_asu" + asu + ".txt";
00056 TString file7 = dir + "scurve_noise_offset7_dac0_run" + run + "_asu" + asu + ".txt";
00057 TString file11 = dir + "scurve_noise_offset11_dac0_run" + run + "_asu" + asu + ".txt";
00058 TString file15 = dir + "scurve_noise_offset15_dac0_run" + run + "_asu" + asu + ".txt";
00059
00060
00061 TString noisy0 = dir + "noisy_noise_offset0_dac0_run" + run + "_asu" + asu + ".txt";
00062 TString noisy3 = dir + "noisy_noise_offset3_dac0_run" + run + "_asu" + asu + ".txt";
00063 TString noisy7 = dir + "noisy_noise_offset7_dac0_run" + run + "_asu" + asu + ".txt";
00064 TString noisy11 = dir + "noisy_noise_offset11_dac0_run" + run + "_asu" + asu + ".txt";
00065 TString noisy15 = dir + "noisy_noise_offset15_dac0_run" + run + "_asu" + asu + ".txt";
00066
00067
00068 TString outfile1 = dir + "ASU" + asu + "_seuils_dac0_endpoint_align_run" + run + ".txt";
00069 TString outfile2 = dir + "ASU" + asu + "_gains_endpoint_align_run" + run + ".txt";
00070 TString outfile3 = dir + "ASU" + asu + "_voies_bruyantes_endpoint_align_run" + run + ".txt";
00071
00072 ofstream out1(outfile1.Data());
00073 ofstream out2(outfile2.Data());
00074 ofstream out3(outfile3.Data());
00075
00076 ifstream in0(file0);
00077 ifstream in3(file3);
00078 ifstream in7(file7);
00079 ifstream in11(file11);
00080 ifstream in15(file15);
00081
00082 ifstream innoise0(noisy0);
00083 ifstream innoise3(noisy3);
00084 ifstream innoise7(noisy7);
00085 ifstream innoise11(noisy11);
00086 ifstream innoise15(noisy15);
00087
00088
00089 TGraphErrors * tg_endpoint[144][64];
00090
00091 bool asu_chip[144];
00092 bool noisy_channel[144][64];
00093
00094 float endpoint_min_15[144];
00095 float endpoint_max_0[144];
00096 float endpoint_target[144];
00097
00098 TH1F * hinflex[144];
00099 TH1F * hwidth[144];
00100 TH1F * hendpoint[144];
00101
00102 for (int i=0;i<144;i++)
00103 {
00104 asu_chip[i] = 0;
00105
00106 endpoint_min_15[i] = 1025;
00107 endpoint_max_0[i] = 0;
00108
00109 endpoint_target[i] = 0;
00110
00111 TString name = Form("hinflex_%i",i+1);
00112 TString title = Form("Inflexion at offset=7 - chip %i;inflexion point (DAC)",i+1);
00113 hinflex[i] = new TH1F(name,title,2000,100,300);
00114
00115 name = Form("hwidth_%i",i+1);
00116 title = Form("Width at offset=7 - chip %i;width (DAC)",i+1);
00117 hwidth[i] = new TH1F(name,title,2000,0,5);
00118
00119 name = Form("hendpoint_%i",i+1);
00120 title = Form("Endpoint (%i sigma) at offset=7 - chip %i;endpoint (DAC)",nsigma,i+1);
00121 hendpoint[i] = new TH1F(name,title,2000,100,300);
00122
00123 for (int j=0;j<64;j++)
00124 {
00125 noisy_channel[i][j] = 0;
00126
00127 tg_endpoint[i][j] = new TGraphErrors();
00128
00129 name = Form("tg_%i_%i",i+1,j);
00130 title = Form("Endpoint versus offset - chip %i - channel %i",i+1,j);
00131
00132 tg_endpoint[i][j]->SetName(name);
00133 tg_endpoint[i][j]->SetTitle(title);
00134
00135 tg_endpoint[i][j]->SetMarkerStyle(20);
00136 tg_endpoint[i][j]->SetMarkerColor(4);
00137 }
00138 }
00139
00140
00141
00142
00143
00144 int N = 0;
00145 int offset = 0;
00146 int chip = 0;
00147 int channel = 0;
00148 float inflex = 0;
00149 float width = 0;
00150 float endpoint = 0;
00151 float inflex_err = 0;
00152 float width_err = 0;
00153 float endpoint_err = 0;
00154
00155
00156
00157
00158
00159 cout<<endl;
00160 cout<<"READ NOISY CHANNELS FILES"<<endl;
00161 cout<<"Read file "<<noisy0<<endl;
00162 while(innoise0>>chip>>channel>>inflex>>inflex_err>>width>>width_err) {noisy_channel[chip-1][channel] = 1;}
00163 cout<<"Read file "<<noisy3<<endl;
00164 while(innoise3>>chip>>channel>>inflex>>inflex_err>>width>>width_err) {noisy_channel[chip-1][channel] = 1;}
00165 cout<<"Read file "<<noisy7<<endl;
00166 while(innoise7>>chip>>channel>>inflex>>inflex_err>>width>>width_err) {noisy_channel[chip-1][channel] = 1;}
00167 cout<<"Read file "<<noisy11<<endl;
00168 while(innoise11>>chip>>channel>>inflex>>inflex_err>>width>>width_err){noisy_channel[chip-1][channel] = 1;}
00169 cout<<"Read file "<<noisy15<<endl;
00170 while(innoise15>>chip>>channel>>inflex>>inflex_err>>width>>width_err){noisy_channel[chip-1][channel] = 1;}
00171
00172
00173
00174 cout<<endl;
00175 cout<<"READ SCURVE PARAMETERS FILES"<<endl;
00176 cout<<"Read file "<<file0<<endl;
00177 offset = 0;
00178 while(in0>>chip>>channel>>inflex>>inflex_err>>width>>width_err)
00179 {
00180 if (!noisy_channel[chip-1][channel])
00181 {
00182 asu_chip[chip-1] = 1;
00183
00184 endpoint = inflex + nsigma*width;
00185 endpoint_err = sqrt(inflex_err + nsigma*nsigma*width_err);
00186
00187 N = tg_endpoint[chip-1][channel]->GetN();
00188
00189 tg_endpoint[chip-1][channel]->SetPoint(N,offset,endpoint);
00190 tg_endpoint[chip-1][channel]->SetPointError(N,0,endpoint_err);
00191
00192 if (endpoint>endpoint_max_0[chip-1]){endpoint_max_0[chip-1] = endpoint;}
00193 }
00194 }
00195
00196
00197
00198 cout<<"Read file "<<file3<<endl;
00199 offset = 3;
00200 while(in3>>chip>>channel>>inflex>>inflex_err>>width>>width_err)
00201 {
00202 if (!noisy_channel[chip-1][channel])
00203 {
00204 endpoint = inflex + nsigma*width;
00205 endpoint_err = sqrt(inflex_err + nsigma*nsigma*width_err);
00206
00207 N = tg_endpoint[chip-1][channel]->GetN();
00208
00209 tg_endpoint[chip-1][channel]->SetPoint(N,offset,endpoint);
00210 tg_endpoint[chip-1][channel]->SetPointError(N,0,endpoint_err);
00211 }
00212 }
00213
00214
00215
00216 cout<<"Read file "<<file7<<endl;
00217 offset = 7;
00218 while(in7>>chip>>channel>>inflex>>inflex_err>>width>>width_err)
00219 {
00220 if (!noisy_channel[chip-1][channel])
00221 {
00222 endpoint = inflex + nsigma*width;
00223 endpoint_err = sqrt(inflex_err + nsigma*nsigma*width_err);
00224
00225 hinflex[chip-1]->Fill(inflex);
00226 hwidth[chip-1]->Fill(width);
00227 hendpoint[chip-1]->Fill(inflex+nsigma*width);
00228
00229 N = tg_endpoint[chip-1][channel]->GetN();
00230
00231 tg_endpoint[chip-1][channel]->SetPoint(N,offset,endpoint);
00232 tg_endpoint[chip-1][channel]->SetPointError(N,0,endpoint_err);
00233 }
00234 }
00235
00236
00237
00238 cout<<"Read file "<<file11<<endl;
00239 offset = 11;
00240 while(in11>>chip>>channel>>inflex>>inflex_err>>width>>width_err)
00241 {
00242 if (!noisy_channel[chip-1][channel])
00243 {
00244 endpoint = inflex + nsigma*width;
00245 endpoint_err = sqrt(inflex_err + nsigma*nsigma*width_err);
00246
00247 N = tg_endpoint[chip-1][channel]->GetN();
00248
00249 tg_endpoint[chip-1][channel]->SetPoint(N,offset,endpoint);
00250 tg_endpoint[chip-1][channel]->SetPointError(N,0,endpoint_err);
00251 }
00252 }
00253
00254
00255
00256 cout<<"Read file "<<file15<<endl;
00257 offset = 15;
00258 while(in15>>chip>>channel>>inflex>>inflex_err>>width>>width_err)
00259 {
00260 if (!noisy_channel[chip-1][channel])
00261 {
00262 endpoint = inflex + nsigma*width;
00263 endpoint_err = sqrt(inflex_err + nsigma*nsigma*width_err);
00264
00265 N = tg_endpoint[chip-1][channel]->GetN();
00266
00267 tg_endpoint[chip-1][channel]->SetPoint(N,offset,endpoint);
00268 tg_endpoint[chip-1][channel]->SetPointError(N,0,endpoint_err);
00269
00270 if (endpoint<endpoint_min_15[chip-1]){endpoint_min_15[chip-1] = endpoint;}
00271 }
00272 }
00273
00274
00275
00276
00277
00278 for (int i=0;i<144;i++)
00279 {
00280 if (asu_chip[i])
00281 {
00282 endpoint_target[i] = 0.5 * (endpoint_max_0[i] + endpoint_min_15[i]);
00283
00284 bool alignment_possible = 0;
00285 if (endpoint_max_0[i]<=endpoint_min_15[i])
00286 {
00287 alignment_possible = 1;
00288 }
00289
00290 if(verbose == 1)
00291 {
00292 cout<<endl;
00293 cout<<"Chip "<<i+1<<endl;
00294 cout<<" Endpoint max at offset 0 = "<<endpoint_max_0[i]<<endl;
00295 cout<<" Endpoint min at offset 15 = "<<endpoint_min_15[i]<<endl;
00296
00297 if (alignment_possible)
00298 {
00299 cout<<" -> Endpoint alignment possible ******************"<<endl;
00300 cout<<" -> Will be aligned on "<<floor(endpoint_target[i]+1)<<endl;
00301 }
00302 else
00303 {
00304 cout<<" -> Endpoint alignment NOT possible"<<endl;
00305 }
00306 }
00307 }
00308 }
00309
00310
00311
00312
00313
00314 float a = 0;
00315 float b = 0;
00316 float offset_ped = 0;
00317 float offset_end = 0;
00318
00319 TF1 * f = new TF1("f","pol1",0,14);
00320
00321 for (int i=0;i<144;i++)
00322 {
00323 if (hinflex[i]->GetEntries()!=0)
00324 {
00325
00326 out1<<i+1<<" "<<floor(endpoint_target[i] + 1)<<endl;
00327
00328
00329 for (int j=0;j<64;j++)
00330 {
00331 if (noisy_channel[i][j])
00332 {
00333 out2<<i+1<<" "<<j<<" "<<0<<endl;
00334 out3<<i+1<<" "<<j<<endl;
00335 }
00336 else
00337 {
00338
00339 tg_endpoint[i][j]->Fit(f,"q");
00340
00341 a = f->GetParameter(1);
00342 b = f->GetParameter(0);
00343
00344 if (a!=0)
00345 {
00346 offset_end = floor((endpoint_target[i] - b)/a + 0.5);
00347 }
00348 else
00349 {
00350 offset_end = 7;
00351 }
00352
00353 if (offset_end<0)
00354 {
00355 offset_end = 0;
00356 }
00357 if (offset_end>15)
00358 {
00359 offset_end = 15;
00360 }
00361
00362 out2<<i+1<<" "<<j<<" "<<offset_end<<endl;
00363 }
00364 }
00365 }
00366 }
00367
00368
00369
00370 if (writetofile == 1)
00371 {
00372 TString rootfilename = dir + "pedestal_alignment.root";
00373
00374 cout<<endl;
00375 cout<<"WRITE CURVES AND HISTOS TO ROOT FILE : "<<rootfilename<<endl;
00376
00377 TFile * tf = new TFile(rootfilename,"recreate");
00378
00379 for (int i=0;i<144;i++)
00380 {
00381 if (hinflex[i]->GetEntries() != 0)
00382 {
00383 hinflex[i]->Write();
00384 hwidth[i]->Write();
00385 hendpoint[i]->Write();
00386
00387 for (int j=0;j<64;j++)
00388 {
00389 tg_endpoint[i][j]->Write();
00390 }
00391 }
00392 }
00393 tf->Close();
00394 }
00395
00396
00397
00398 }
00399 }
00400