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 #include "MTEvent.hh"
00018 #include "MTRun.hh"
00019 #include "MTChannel.hh"
00020
00021 #include "MicroException.hh"
00022
00023
00024 using namespace std;
00025
00026
00027 int main(int argc, char**argv){
00028
00029 if (argc!=7)
00030 {
00031 cout<<"/*****************************************************************************************/"<<endl;
00032 cout<<"/* ARGUMENTS TO THE PROGRAM **************************************************************/"<<endl;
00033 cout<<"/* 1 - DIRECTORY CONTAINING THE ROOTFILE *************************************************/"<<endl;
00034 cout<<"/* 2 - NAME OF MERGED ROOTFILE ***********************************************************/"<<endl;
00035 cout<<"/* 3 - ASU NUMBER (13-14...)**************************************************************/"<<endl;
00036 cout<<"/* 4 - TRIGGER RATE (Hz)******************************************************************/"<<endl;
00037 cout<<"/* 5 - TARGET NOISE RATE (Hz)*************************************************************/"<<endl;
00038 cout<<"/* 6 - ACTIONS: R (readonly) - C (correctoffset) - I (increaseoffset) ********************/"<<endl;
00039 cout<<"/*****************************************************************************************/"<<endl;
00040 }
00041
00042 else
00043 {
00044
00045 TString dir = argv[1];
00046 TString rootfilename = argv[2];
00047 TString asu = argv[3];
00048 float trigger_rate = atof(argv[4]);
00049 float target_rate = atof(argv[5]);
00050 TString action = argv[6];
00051
00052
00053
00054
00055 if (!(asu>="13" && asu<="37"))
00056 {
00057 cout<<endl;
00058 cout<<"ASU NUMBER SHOULD BE BETWEEN 13 AND 37"<<endl;
00059 cout<<endl;
00060 }
00061
00062 else
00063 {
00064
00065 TString filename = dir + rootfilename;
00066 int nevent = 0;
00067
00068 TH1F * hnhit = new TH1F("hnhit",";nhit",1000,0,10000);
00069 TH1F * hc_nhit = new TH1F("hc_nhit",";channel;number of hits",9216,0,9216);
00070 TH1F * hc_rate = new TH1F("hc_rate",";channel;rate (Hz)",9216,0,9216);
00071
00072
00073
00074
00075
00076 TFile *f = new TFile(filename);
00077 cout<<"reading File : "<<filename<<endl;
00078
00079 TIter nextkey(f->GetListOfKeys());
00080 TKey *key;
00081
00082 while (key = (TKey*)nextkey())
00083 {
00084 TTree *tree = (TTree*)key->ReadObj();
00085 MTRun* run = (MTRun*)tree->GetUserInfo()->FindObject("MTRun");
00086
00087 nevent = tree->GetEntries();
00088 cout <<"nEvent="<<nevent<<endl;
00089
00090 MTEvent *evt = new MTEvent();
00091
00092 TBranch *branch= tree->GetBranch("MTEvent");
00093 branch->SetAddress(&evt);
00094
00095 for (int i=0;i<nevent;i++)
00096 {
00097 tree->GetEntry(i);
00098 int nchannel = evt->GetNchannel();
00099 hnhit->Fill(nchannel);
00100
00101 MTChannel* channel = (MTChannel*)evt->GetChannels()->UncheckedAt(0);
00102
00103 for (int j=0;j<nchannel;j++)
00104 {
00105 MTChannel* channel = (MTChannel*)evt->GetChannels()->UncheckedAt(j);
00106
00107 int hardid = channel->GetHardId();
00108 int chipid = channel->GetChipId();
00109
00110 hc_nhit->Fill((chipid-1)*64+hardid);
00111 }
00112 }
00113 }
00114
00115
00116
00117
00118 for (int i=0;i<9216;i++)
00119 {
00120 int entry = hc_nhit->GetBinContent(i+1);
00121
00122 if (entry!=0)
00123 {
00124 float rate = entry / 1. / nevent * trigger_rate ;
00125 hc_rate->SetBinContent(i+1,rate);
00126
00127 }
00128 }
00129
00130
00131
00132
00133
00134 TString infile11 = dir + "ASU00" + asu + "_gains_noise_align.txt";
00135 TString infile12 = dir + "ASU00" + asu + "_offset_set.txt";
00136
00137 ifstream in11(infile11.Data());
00138 ifstream in12(infile12.Data());
00139
00140 cout<<endl;
00141 cout<<"Read offset status (set/not-set) from files:"<<endl;
00142 cout<<" -> "<<infile12<<endl;
00143 cout<<endl;
00144 cout<<"Write offset map to files"<<endl;
00145 cout<<" -> "<<infile11<<endl;
00146 cout<<endl;
00147
00148
00149
00150
00151 bool set[144][64];
00152 int offset[144][64];
00153 bool chan[144][64];
00154
00155 for (int i=0;i<144;i++)
00156 {
00157 for (int j=0;j<64;j++)
00158 {
00159 chan[i][j] = 0;
00160 set[i][j] = 0;
00161 offset[i][j] = -1;
00162 }
00163 }
00164
00165 int i,j,off;
00166
00167
00168 while(in11>>i>>j>>off){offset[i-1][j] = off;chan[i-1][j] = 1;}
00169
00170
00171 while(in12>>i>>j){set[i-1][j] = 1;}
00172
00173
00174
00175
00176
00177
00178 int nthr = 0;
00179
00180
00181 for (int i=0;i<144;i++)
00182 {
00183 for (int j=0;j<64;j++)
00184 {
00185 if (chan[i][j])
00186 {
00187
00188 off = offset[i][j];
00189 float rate = hc_rate->GetBinContent(i*64+j+1);
00190
00191 if (rate > target_rate)
00192 {
00193 nthr++;
00194 cout<<i+1<<" "<<j<<" rate = "<<rate<<" Hz - offset:"<<offset[i][j]<<" -> "<<offset[i][j]-1<<endl;
00195 }
00196
00197 if (action != "R")
00198 {
00199
00200 if (rate > target_rate)
00201 {
00202 offset[i][j]--;
00203 set[i][j] = 1;
00204 if (action == "C"){cout<<i+1<<" "<<j<<" "<<offset[i][j]+1<<" -> "<<offset[i][j]<<endl;}
00205 }
00206
00207
00208 else
00209 {
00210 if (!set[i][j])
00211 {
00212 if (action == "I")
00213 {
00214 offset[i][j]++;
00215 cout<<i+1<<" "<<j<<" "<<offset[i][j]-1<<" -> "<<offset[i][j]<<endl;
00216 }
00217 }
00218 }
00219 }
00220 }
00221 }
00222 }
00223
00224
00225
00226
00227
00228 if (action != "R")
00229 {
00230
00231 TString outfile11 = infile11;
00232 TString outfile12 = infile12;
00233
00234 ofstream out11(outfile11.Data());
00235 ofstream out12(outfile12.Data());
00236
00237 for (int i=0;i<144;i++)
00238 {
00239 for (int j=0;j<64;j++)
00240 {
00241 if (chan[i][j])
00242 {
00243 {
00244 out11<<i+1<<" "<<j<<" "<<offset[i][j]<<endl;
00245 if (set[i][j])
00246 {
00247 out12<<i+1<<" "<<j<<endl;
00248 }
00249 }
00250 }
00251 }
00252 }
00253 }
00254
00255 cout<<endl;
00256 cout<<"Number of channel with noise rate large than "<<target_rate<<" Hz = "<<nthr<<endl;
00257 cout<<"Average number of hits per readout = "<<hnhit->GetMean()<<endl;
00258 cout<<endl;
00259
00260
00261
00262 TCanvas * c0 = new TCanvas("c0","",10,10,1000,500);
00263
00264 hnhit->Draw();
00265 TString picfilename = dir + "hnhit.png";
00266 c0->Print(picfilename);
00267
00268 c0->SetLogy();
00269 hc_rate->Draw();
00270 picfilename = dir + "hc_rate.png";
00271 c0->Print(picfilename);
00272
00273 }
00274
00275
00276 }
00277 }