/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/Microroc/Calibration/Align_noise_rate_asu_microroc.cpp

Go to the documentation of this file.
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 <TreeClass.hh>
00012 #include <TGraphErrors.h>
00013 #include <TF1.h>
00014 #include <TH1F.h>
00015 #include <TH1I.h>
00016 #include <TH2I.h>
00017 #include <TCanvas.h>
00018 #include <sstream>
00019 #include "Log.hh"
00020 
00021 #include "root/CaloRun.hh"
00022 #include "root/CaloEvent.hh"
00023 #include "root/MTRun.hh"
00024 #include "root/MTEvent.hh"
00025 #include "root/MTChannel.hh"
00026 
00027 
00028 #include "MicroException.hh"
00029 
00030 
00031 using namespace std;
00032 
00033 
00034 int main(int argc, char**argv){
00035 
00036   if (argc!=7)
00037     {
00038       cout<<"/*****************************************************************************************/"<<endl;
00039       cout<<"/* ARGUMENTS TO THE PROGRAM **************************************************************/"<<endl;
00040       cout<<"/* 1 - DIRECTORY CONTAINING THE ROOTFILE *************************************************/"<<endl;
00041       cout<<"/* 2 - NAME OF MERGED ROOTFILE ***********************************************************/"<<endl;
00042       cout<<"/* 3 - M2 PROTOTYPE NUMBER (1 OR 2)*******************************************************/"<<endl;
00043       cout<<"/* 4 - TRIGGER RATE (Hz)******************************************************************/"<<endl;
00044       cout<<"/* 5 - TARGET NOISE RATE (Hz)*************************************************************/"<<endl;
00045       cout<<"/* 6 - ACTIONS: readonly - correctoffset - increaseoffset ********************************/"<<endl;
00046       cout<<"/*****************************************************************************************/"<<endl;
00047     }
00048 
00049   else
00050     {
00051 
00052       TString dir  = argv[1];
00053       TString rootfilename  = argv[2];
00054       TString m2  = atoi(argv[3]);
00055       float trigger_rate = atof(argv[4]);
00056       float target_rate = atof(argv[5]);
00057       TString action = argv[6];
00058 
00059       if (!(m2==1 || m2==2))
00060         {
00061           cout<<endl;
00062           cout<<"M2 NUMBER SHOULD BE 1 OR 2"<<endl;
00063           cout<<endl;
00064         }
00065 
00066       else
00067         {
00068 
00069           TString filename = dir + rootfilename;
00070           int nevent = 0;
00071 
00072           TH1F * hnhit = new TH1F("hnhit",";nhit",1000,0,10000);
00073           TH1F * hc_nhit = new TH1F("hc_nhit",";channel;number of hits",9216,0,9216);
00074           TH1F * hc_rate = new TH1F("hc_rate",";channel;rate (Hz)",9216,0,9216);
00075           TH1F * hrate = new TH1F("hrate",";rate (Hz)",100,0,1);
00076           TH1I * hoffset = new TH1I("hoffset","offset distribution;offset (DAC)",16,0,16);
00077           TH2I * hxy = new TH2I("hxy","hit distribution;y (pad);x (pad)",96,0,96,96,0,96);
00078           
00079           //READ ROOTFILE
00080       
00081           TFile *f = new TFile(filename);
00082           cout<<"reading File : "<<filename<<endl;
00083 
00084           TIter nextkey(f->GetListOfKeys());
00085           TKey *key;
00086 
00087           while (key = (TKey*)nextkey())
00088             {
00089               TTree *tree = (TTree*)key->ReadObj();                
00090               MTRun* run = (MTRun*)tree->GetUserInfo()->FindObject("MTRun");
00091 
00092               nevent = tree->GetEntries();
00093               cout <<"nEvent="<<nevent<<endl;
00094 
00095               MTEvent *evt = new MTEvent();
00096 
00097               TBranch *branch= tree->GetBranch("MTEvent");
00098               branch->SetAddress(&evt);
00099 
00100               for (int i=0;i<nevent;i++)
00101                 { 
00102                   tree->GetEntry(i);
00103                   int nchannel = evt->GetNchannel();
00104                   hnhit->Fill(nchannel);
00105 
00106                   MTChannel* channel = (MTChannel*)evt->GetChannels()->UncheckedAt(0);    
00107 
00108                   for (int j=0;j<nchannel;j++)
00109                     {
00110                       MTChannel* channel = (MTChannel*)evt->GetChannels()->UncheckedAt(j);
00111 
00112                       hxy->Fill(channel->GetY()*1e-4+48,channel->GetX()*1e-4+48,1);
00113 
00114                       int hardid = channel->GetHardId();
00115                       int chipid = channel->GetChipId();
00116                       int boardid = channel->GetBoardId();
00117                 
00118                       if (0)
00119                         {
00120                           if (m2==1)
00121                             {
00122                               if (!(boardid==13 || boardid==14))
00123                                 {
00124                                   if (boardid==15 || boardid==16){chipid += 48;}
00125                                   else{chipid += 96;}
00126                                 }
00127                             }
00128                           
00129                           if (m2==2)
00130                             {
00131                               if (!(boardid==24 || boardid==25))
00132                                 {
00133                                   if (boardid==22 || boardid==23){chipid += 48;}
00134                                   else{chipid += 96;}
00135                                 }
00136                             }
00137                         }
00138 
00139                       hc_nhit->Fill((chipid-1)*64+hardid);
00140                     }
00141                 }
00142             }
00143 
00144 
00145           //CALCULATE RATE FROM NUMBER OF HITS
00146 
00147           for (int i=0;i<9216;i++)
00148             {
00149               int entry = hc_nhit->GetBinContent(i+1);
00150               
00151               if (entry!=0)
00152                 {
00153                   float rate = entry / 1. / nevent * trigger_rate ;
00154                   hc_rate->SetBinContent(i+1,rate);
00155                   hrate->Fill(rate);
00156                   //hrate->Fill(rate);
00157                 }
00158             }
00159 
00160 
00161           
00162           //DECLARE FILES TO WRITE TO AND READ FROM
00163 
00164           TString slab1;
00165           TString slab2;
00166           TString slab3;
00167 
00168           if (m2 == 1)
00169             {
00170               slab1 = "ASU1314";
00171               slab2 = "ASU1516";
00172               slab3 = "ASU1718";
00173             }
00174           
00175           if (m2 == 2)
00176             {
00177               slab1 = "ASU2425";
00178               slab2 = "ASU2223";
00179               slab3 = "ASU2021";
00180             }
00181 
00182 
00183           TString infile11 = dir + slab1 + "_gains_noise_align.txt";
00184           TString infile12 = dir + slab1 + "_offset_set.txt";
00185 
00186           TString infile21 = dir + slab2 + "_gains_noise_align.txt";
00187           TString infile22 = dir + slab2 + "_offset_set.txt";
00188 
00189           TString infile31 = dir + slab3 + "_gains_noise_align.txt";
00190           TString infile32 = dir + slab3 + "_offset_set.txt";
00191 
00192           ifstream in11(infile11.Data());
00193           ifstream in12(infile12.Data());
00194 
00195           ifstream in21(infile21.Data());
00196           ifstream in22(infile22.Data());
00197 
00198           ifstream in31(infile31.Data());
00199           ifstream in32(infile32.Data());
00200 
00201           cout<<endl;
00202           cout<<"Read offset status (set/not-set) from files:"<<endl;
00203           cout<<"  ->  "<<infile12<<endl;
00204           cout<<"  ->  "<<infile22<<endl;
00205           cout<<"  ->  "<<infile32<<endl;
00206           cout<<endl;
00207           cout<<"Write offset map to files"<<endl;
00208           cout<<"  ->  "<<infile11<<endl;
00209           cout<<"  ->  "<<infile21<<endl;
00210           cout<<"  ->  "<<infile31<<endl;
00211           cout<<endl;
00212 
00213 
00214           //READ WHICH CHANNELS HAVE THEIR OFFSET ALREADY SET
00215           
00216           bool set[144][64];
00217           int offset[144][64];
00218 
00219           for (int i=0;i<144;i++)
00220             {
00221               for (int j=0;j<64;j++)
00222                 {
00223                   set[i][j] = 0;
00224                   offset[i][j] = -1;
00225                 }
00226             }
00227 
00228           int i,j,off;
00229 
00230           //Read offset maps
00231           while(in11>>i>>j>>off){offset[i-1][j] = off;}
00232           while(in21>>i>>j>>off){offset[i-1][j] = off;}   
00233           while(in31>>i>>j>>off){offset[i-1][j] = off;}
00234 
00235           //Read which channel have their offset already set
00236           while(in12>>i>>j){set[i-1][j] = 1;}
00237           while(in22>>i>>j){set[i-1][j] = 1;}
00238           while(in32>>i>>j){set[i-1][j] = 1;}
00239 
00240 
00241           //For non-set offsets
00242           //Determine if offset should be raised or lowered
00243           //by comparing measured rate to target rate
00244 
00245           int nthr = 0;
00246 
00247           //loop over m2 channels
00248           for (int i=0;i<144;i++)
00249             {
00250               for (int j=0;j<64;j++)
00251                 {
00252  
00253                   off = offset[i][j];
00254                   float rate = hc_rate->GetBinContent(i*64+j+1);
00255 
00256                   if (rate > target_rate)
00257                     {
00258                       nthr++;
00259                       //cout<<i+1<<" "<<j<<" rate = "<<rate<<" Hz - offset:"<<offset[i][j]<<" -> "<<offset[i][j]-1<<endl;
00260                     }
00261 
00262                   if (action != "readonly")
00263                     {
00264                       //if rate > target, lower offset and define it as set
00265                       if (rate > target_rate)
00266                         {
00267                           offset[i][j]--;
00268                           set[i][j] = 1;
00269                           if (action == "correctoffset"){cout<<i+1<<" "<<j<<" "<<offset[i][j]+1<<" -> "<<offset[i][j]<<endl;}
00270                         }
00271 
00272                       if (action == "increaseoffset")
00273                         {
00274                           if (!set[i][j])
00275                             {
00276                               offset[i][j]++;
00277                               cout<<i+1<<" "<<j<<" "<<offset[i][j]-1<<" -> "<<offset[i][j]<<endl;
00278                             }
00279                         }
00280                     }
00281                 }
00282             }
00283 
00284 
00285 
00286           //Re-write offset map and channel set files
00287 
00288           if (action == "correctoffset" || action == "increaseoffset")
00289             {
00290 
00291               TString outfile11 = infile11;
00292               TString outfile12 = infile12;
00293 
00294               TString outfile21 = infile21;
00295               TString outfile22 = infile22;
00296 
00297               TString outfile31 = infile31;
00298               TString outfile32 = infile32;
00299 
00300               ofstream out11(outfile11.Data());
00301               ofstream out12(outfile12.Data());
00302 
00303               ofstream out21(outfile21.Data());
00304               ofstream out22(outfile22.Data());
00305 
00306               ofstream out31(outfile31.Data());
00307               ofstream out32(outfile32.Data());
00308 
00309 
00310               for (int i=0;i<144;i++)
00311                 {
00312                   for (int j=0;j<64;j++)
00313                     {
00314                       //slab1      
00315                       if (i<48)
00316                         {
00317                           out11<<i+1<<" "<<j<<" "<<offset[i][j]<<endl;
00318                           hoffset->Fill(offset[i][j]);
00319                           if (set[i][j])
00320                             {
00321                               out12<<i+1<<" "<<j<<endl;
00322                             }
00323                         }
00324                       else
00325                         {
00326                           //slab2
00327                           if (i<96)
00328                             {
00329                               out21<<i+1<<" "<<j<<" "<<offset[i][j]<<endl;
00330                               hoffset->Fill(offset[i][j]);
00331                               if (set[i][j])
00332                                 {
00333                                   out22<<i+1<<" "<<j<<endl;
00334                                 }
00335                             }
00336                           //slab3
00337                           else
00338                             {
00339                               out31<<i+1<<" "<<j<<" "<<offset[i][j]<<endl;
00340                               hoffset->Fill(offset[i][j]);
00341                               if (set[i][j])
00342                                 {
00343                                   out32<<i+1<<" "<<j<<endl;
00344                                 }
00345                             }
00346                         }
00347                     }
00348                 }
00349             }
00350 
00351           cout<<endl;
00352           cout<<"Number of channel with noise rate large than "<<target_rate<<" Hz  = "<<nthr<<endl;
00353           cout<<"Average number of hits per readout = "<<hnhit->GetMean()<<endl;
00354           cout<<endl;
00355 
00356 
00357           
00358           TCanvas * c0 = new TCanvas("c0","",10,10,1000,500);
00359 
00360           cout<<endl;
00361           cout<<"Write histos to files: "<<endl;
00362 
00363           hnhit->Draw();
00364           TString picfilename = dir + "hnhit.png";
00365           cout<<picfilename<<endl;
00366           c0->Print(picfilename);
00367 
00368           hoffset->Draw();
00369           picfilename = dir + "hoffset.png";
00370           cout<<picfilename<<endl;
00371           c0->Print(picfilename);
00372 
00373           hxy->Draw("zcol");
00374           picfilename = dir + "hxy.png";
00375           cout<<picfilename<<endl;
00376           c0->Print(picfilename);
00377 
00378           c0->SetLogy();
00379           hc_rate->Draw();
00380           picfilename = dir + "hc_rate.png";
00381           cout<<picfilename<<endl;
00382           c0->Print(picfilename);
00383 
00384           hrate->Draw();
00385           picfilename = dir + "hrate.png";
00386           cout<<picfilename<<endl;
00387           c0->Print(picfilename);
00388 
00389         }
00390 
00391 
00392     }
00393 }

Generated on Mon Jun 11 16:55:46 2012 for MicromegasFramework by  doxygen 1.4.7