/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/Renaud/test_microroc.cpp

Go to the documentation of this file.
00001 /* @version $Revision: 1585 $ * @modifiedby $Author: jacquem $ * @lastmodified $Date: 2012-03-13 11:03:35 +0100 (Tue, 13 Mar 2012) $ */
00002 #include <map>
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 <TF1.h>
00012 #include <TH2.h>
00013 #include <TGraph.h>
00014 #include <sstream>
00015 #include "Log.hh"
00016 #include <fstream>
00017 #include "TCanvas.h"
00018 #include "TApplication.h"
00019 #include "TColor.h"
00020 #include "TStyle.h"
00021 #include "TAxis.h"
00022 #include "TString.h"
00023 #include "TLegend.h"
00024 #include "Bytes.h"
00025 
00026 
00027 
00028 #include "root/MTRun.hh"
00029 #include "root/MTEvent.hh"
00030 #include "root/MTChannel.hh"
00031 #include "root/MTMicrorocChip.hh"
00032 
00033 #include "MicroException.hh"
00034 
00035 //#define DISPLAY
00036 #undef DISPLAY
00037 //#define R_DEBUG
00038 #undef R_DEBUG
00039 //#define PLOT
00040 #undef PLOT
00041 #define WRITE
00042 
00043 using namespace std;
00044 void usage(char *progname) ;
00045 #ifdef PLOT
00046         void set_style_myplain() ;
00047         TStyle *style_myplain=new TStyle("myplain","classic style");
00048 #endif
00049 ////////////////////////////////////////////////////////////////////////////////////////////
00050 
00051 int main(int argc, char**argv){
00052 
00053   /*****************************************************************/
00054   /* ARGUMENTS TO THE PROGRAM **************************************/
00055   /* 1 - DIRECTORY OF THE ROOT FILE PRODUCED BY THE RECONSTRUCTION */
00056   /* 2 - NAME OF THE ROOT FILE *************************************/
00057   /* 3 - THRESHOLD THAT IS MOVED DURING THE CALIBRATION ************/
00058   /*    0 -> DAC_0 (LOW THRESHOLD) *********************************/
00059   /*    1 -> DAC_1 (MEDIUM THRESHOLD) ******************************/
00060   /*    2 -> DAC_2 (HIGH THRESHOLD) ********************************/
00061   /*****************************************************************/
00062         if(argc != 4)
00063         {       usage(argv[0]) ;
00064                 return 1 ;
00065         }
00066         TString root_directory = argv[1];
00067         TString root_filename = argv[2];
00068         int dac = atoi(argv[3]);
00069 
00070         //bool display = 0;
00071         #ifdef PLOT
00072                 set_style_myplain() ;
00073         #endif
00074         TCanvas *c1 = new TCanvas("c1","Test MICROROC",0,0,1000,1000);
00075         TString root_file = root_directory + root_filename;
00076         TString scurve_file = root_directory + "scurve_" + root_filename;
00077 
00078 //  cout<<"Wish to write "<<scurve_file<<endl;
00079 
00080         int nbChannel = 0;
00081         float nbEvt = 0;
00082         int threshold = 0;
00083         //int chipid = 0;
00084         int hardid = 0;
00085         int value = 0;
00086         //int content = 0;
00087         Double_t content = 0;
00088         UInt_t chipId ;
00089  
00090         TH1I * hvalue = new TH1I("hvalue","",5,0,5);
00091         TH1I * hnpulse_threshold = new TH1I("hnpulse_threshold","",1024,0,1024);
00092 
00093         TH1I * hscurve[64];
00094         TString name,title;
00095  
00096         for (int j=0;j<64;j++)
00097         {       name  = Form("hscurve_%i",j);
00098                 title = Form("SCurve from channel %i",j);
00099                 hscurve[j] = new TH1I(name,title,1024,0,1024);
00100         }
00101   
00102         TFile f(root_file);
00103         TIter nextkey(f.GetListOfKeys());
00104         TKey *key;
00105         while (key = (TKey*)nextkey())
00106         {       TTree *tree = (TTree*)key->ReadObj();                      
00107                 MTRun* run = (MTRun*)tree->GetUserInfo()->FindObject("MTRun");
00108                 const MTChip&  chip = run->GetOneChip();
00109                 //    cout << "------------ Chip[" << chip.GetId() << "], softId: [" << chip.GetSoftId() <<"] configuration ---------------" << endl ;
00110 
00111                 try
00112                 {       const MTMicrorocChip &microChip = dynamic_cast<const MTMicrorocChip &> (chip);
00113                         //const MTHardroc2Chip &microChip = dynamic_cast<const MTHardroc2Chip &> (chip);
00114                         //cout << "threshold dac 0:[" << microChip.GetThresholdDac_0()  << endl;
00115                         //cout << "threshold dac 1:[" << microChip.GetThresholdDac_1()  << endl;
00116                         //cout << "threshold dac 2:[" << microChip.GetThresholdDac_2()  << endl;
00117                         //chipId = chip.GetId() ;
00118                         //cout << "Chip identifier: " << chipId << endl ;
00119                         if (dac==0){threshold = microChip.GetThresholdDac_0();}
00120                         if (dac==1){threshold = microChip.GetThresholdDac_1();}
00121                         if (dac==2){threshold = microChip.GetThresholdDac_2();}
00122                 }
00123                 catch (...) {}
00124 
00125                 MTEvent *evt =  new MTEvent();
00126                 TBranch *branch= tree->GetBranch("MTEvent");
00127                 branch->SetAddress(&evt);
00128                 MTChannel* channel = NULL;
00129                 nbEvt = tree->GetEntries();
00130 //              cout << "Number of events in the tree: " << nbEvt << endl ;
00131 
00132                 hnpulse_threshold->SetBinContent(threshold+1,nbEvt);
00133 
00134                 for( int evtNum = 0; evtNum < nbEvt ; evtNum++)
00135                 {       //if (evtNum%1000 == 0) {cout<<evtNum/nbEvt*100.<<" %"<<"\r"<<flush;}
00136                         tree->GetEntry(evtNum);
00137                         nbChannel = evt->GetNchannel();
00138                         for(int i=0;i<nbChannel ;i++)
00139                         {       channel = (MTChannel*)evt->GetChannels()->UncheckedAt(i);
00140                                 chipId = channel->GetChipId();
00141                                 hardid = channel->GetHardId();
00142                                 value = channel->GetDigitalValue();
00143                                 hvalue->Fill(value);
00144                                 if (value==(dac+1))
00145                                 {       hscurve[hardid]->Fill(threshold);
00146                                         #ifdef R_DEBUG
00147                                                 cout << "---- digital value:" <<  value <<endl;
00148                                                 cout << "---- hard id:" << hardid <<endl;
00149                                                 //cout << "---- x:"<< channel->GetX() <<endl;
00150                                                 //cout << "---- y:" << channel->GetY() <<endl;
00151                                                 cout << "---- chip id:" << chipId <<endl;
00152                                         #endif
00153           
00154                                 }
00155                         }
00156                 }
00157         }
00158         
00159         
00160 
00161 
00162         //===========================================================================*
00163         // Now analyze scurves  
00164 
00165         TString t_scurve_file = root_directory + root_filename ;//+ ".root";
00166         //cout << t_scurve_file<< endl;
00167 
00168         int tmin = 0;
00169         int tmed = 0;
00170         int tmax = 0;
00171         int delta = 0;
00172 
00173 //      TH1I * hscurve[64];
00174 
00175         TH1F * hinflex = new TH1F("hinflex","",1024,0,1024);
00176         TH1F * hsigma = new TH1F("hsigma","",50,-2,2);
00177 
00178         TH1F * hinflex_err = new TH1F("hinflex_err","",100,0,10);
00179         TH1F * hsigma_err = new TH1F("hsigma_err","",100,0,2);
00180 
00181         TH1F * hc_inflex = new TH1F("hc_inflex","",64,0,64);
00182         TH1F * hc_sigma = new TH1F("hc_sigma","",64,0,64);
00183 
00184         TH1F * hc_inflex_err = new TH1F("hc_inflex_err","",64,0,64);
00185         TH1F * hc_sigma_err = new TH1F("hc_sigma_err","",64,0,64);
00186 
00187         TH1F * hc_inflex_sigma = new TH1F("hc_inflex_sigma","",64,0,64);
00188 
00189 //      TString name;
00190 
00191 /*      for (int j=0;j<64;j++)
00192         {       name  = Form("hscurve_%i",j);
00193                 TH1I * h = (TH1I*)tf.Get(name);
00194                 hscurve[j] = h;
00195         }
00196 */
00197         TString func = "[0]/(1+exp((x-[1])/[2]))";
00198         TF1 * fermi[64];
00199         float scurve_max = 0;
00200         float scurve_inflex = 0;
00201         float inflex = 0;
00202         float sigma = 0;
00203         float inflex_err = 0;
00204         float sigma_err = 0;
00205         float content0,content1,content2;
00206         float chi2 = 0;
00207         float ndf = 0;
00208         
00209         for (int j=0;j<64;j++)
00210         {       name  = Form("fermi_%i",j);
00211                 fermi[j] = new TF1(name,func,0,1024);
00212                 //fermi[j]->SetLineColor(j+1);
00213                 fermi[j]->SetLineColor(kRed);
00214                 fermi[j]->SetLineWidth(2);
00215                 /*If SCurve exist*/
00216                 if (hscurve[j]->GetEntries()!=0)
00217                 {       /*Find threshold min tmin*/
00218                         for (int b=0;b<1023;b++)
00219                         {       content = hscurve[j]->GetBinContent(b+1);
00220                                 if (content != 0)
00221                                 {       tmin = b ;
00222                                         b = 1023 ;
00223                                         scurve_max = content;
00224                                 }
00225                         }
00226                         /*Find threshold at which scurve = 0 tmax*/
00227                         for (int b=1024;b>tmin;b--)
00228                         {       if (hscurve[j]->GetBinContent(b+1) != 0)
00229                                 {       tmax = b+1 ;
00230                                         b = tmin ;
00231                                 }
00232                         }
00233                         /*Find threshold at which scurve reaches the max*/
00234                         for (int b=tmax;b>=0;b--)
00235                         {       if (hscurve[j]->GetBinContent(b+1) == scurve_max)
00236                                 {       tmed = b+1 ;
00237                                         b = -1 ;
00238                                 }
00239                         }
00240                         scurve_inflex = 0.5*(tmax+tmed);
00241                         delta = tmax - tmed;
00242   
00243                         /*If SCurve is a step function, do not fit parameters*/
00244                         if (delta==0)
00245                         {       inflex = scurve_inflex;
00246                                 sigma = 0.;
00247                                 inflex_err = 0.;
00248                                 sigma_err = 0.;
00249                         }
00250                         else
00251                         {       /*Set parameters*/
00252                                 fermi[j]->FixParameter(0,scurve_max);
00253                                 fermi[j]->SetParameter(1,scurve_inflex);
00254                                 fermi[j]->SetParameter(2,delta/10.);
00255                                 /*Perform fit*/
00256                                 hscurve[j]->Fit(fermi[j],"q","",0,tmax+50);
00257                                 /*Get parameters*/
00258                                 inflex = fermi[j]->GetParameter(1);
00259                                 sigma = sqrt(pow(fermi[j]->GetParameter(2),2));
00260                                 inflex_err = sqrt(pow(fermi[j]->GetParError(1),2));
00261                                 sigma_err = sqrt(pow(fermi[j]->GetParError(2),2));
00262                                 chi2 = fermi[j]->GetChisquare();
00263                                 ndf = fermi[j]->GetNDF();
00264                         }
00265 
00266                         hc_inflex_sigma->SetBinContent(j+1,inflex);
00267                         hc_inflex_sigma->SetBinError(j+1,sigma);
00268 
00269                         hc_inflex->SetBinContent(j+1,inflex);
00270                         hc_sigma->SetBinContent(j+1,sigma);
00271 
00272                         hc_inflex_err->SetBinContent(j+1,inflex_err);
00273                         hc_sigma_err->SetBinContent(j+1,sigma_err);
00274 
00275                         hinflex->Fill(inflex);
00276                         hsigma->Fill(sigma);
00277 
00278                         #ifdef DISPLAY
00279                                 cout<<" Channel\t" << j << "\t" << inflex << "\t width\t" << sigma << endl ;
00280                         #endif
00281                 }
00282         }
00283         
00284         ////////////////////////////////////////////////////////////////////////////////
00285         // Write Scurves
00286         TFile * tf = new TFile(scurve_file,"RECREATE");
00287         hvalue->Write();
00288         hnpulse_threshold->Write();
00289         for (int j=0;j<64;j++)
00290         {       hscurve[j]->Write();}
00291         tf->Close();
00292         ////////////////////////////////////////////////////////////////////////////////
00293         
00294         #ifdef WRITE
00295                 root_filename.ReplaceAll(".root", "");
00296                 TString outfile = root_directory + "../RESULTS/" +  "parameters_" + root_filename + ".txt";
00297                 ofstream out ;
00298                 out.open(outfile) ;
00299                 for (int j=0;j<64;j++)
00300                 {       out<<j<<" "<<hc_inflex->GetBinContent(j+1)<<" "<<hc_inflex_err->GetBinContent(j+1)<<" "<<hc_sigma->GetBinContent(j+1)<<" "<<hc_sigma_err->GetBinContent(j+1)<<endl;
00301                 }
00302         #endif
00303         #ifdef PLOT
00304                 TApplication theApp("App", &argc, argv);
00305                 TString adj;
00306                 if (dac==0){adj = "Low";}
00307                 if (dac==1){adj = "Medium";}
00308                 if (dac==2){adj = "High";}
00309                 
00310                 c1->Divide(8,8);
00311                 //c1->cd();
00312                 TString htitle = adj + " threshold";
00313                 for (int j=0;j<64;j++)
00314                 {       //c0->cd(j/8,j%8);
00315                         c1->cd(j+1);
00316                         hscurve[j]->SetLineWidth(2);
00317                         hscurve[j]->Draw("");
00318                         c1->Update() ;
00319                 }
00320                 theApp.Run(kTRUE) ;
00321                 c1->Update() ;
00322         #endif
00323     return 0 ;
00324 }
00325 
00326 
00327 
00328 //**********************************************************************************************
00329 //**********************************************************************************************
00330 
00331 
00332 void usage(char *progname)
00333 {
00334         printf("Usage : %s directory rootfile [0|1|2]", progname);
00335         printf("        Where [0|1|2] is the dac number\n");
00336         printf("        scurve_rootfile.root is created\n");
00337 }
00338 
00339 //**********************************************************************************************
00340 //**********************************************************************************************
00341 #ifdef PLOT
00342 void set_style_myplain()
00343 {
00344         style_myplain->SetCanvasBorderMode(0);
00345         style_myplain->SetCanvasColor(0);
00346         style_myplain->SetDrawBorder(0);
00347         style_myplain->SetPadBorderMode(0);
00348         style_myplain->SetPadColor(10);
00349         style_myplain->SetFrameLineColor(1);
00350         style_myplain->SetFrameFillColor(5);
00351         style_myplain->SetFrameFillStyle(0);
00352         style_myplain->SetFrameBorderMode(0);
00353 
00354         //style_myplain->SetFrameLineWidth(5); //test for setframelinecolor !
00355 
00356         style_myplain->SetLegendBorderSize(1);
00357 
00358         style_myplain->SetTextColor(231);
00359 
00360 //      style_myplain->SetFillColor(0); // If enable : impossible to fill histo with color !!! see note in the end !
00361         style_myplain->SetLineColor(231);
00362 
00363         style_myplain->SetStatColor(10);
00364         style_myplain->SetStatTextColor(1);
00365         style_myplain->SetStatBorderSize(1) ;
00366         style_myplain->SetStatX(0.9);
00367         style_myplain->SetStatY(0.9);
00368         style_myplain->SetOptStat("");//"emr" by default
00369         style_myplain->SetOptFit(0111);
00370 
00371         style_myplain->SetTitleTextColor(1);
00372         style_myplain->SetTitleBorderSize(0);
00373         style_myplain->SetTitleAlign(23);
00374         style_myplain->SetTitleX(0.5);
00375 
00376         style_myplain->SetAxisColor(1,"x");     // bleu
00377         style_myplain->SetAxisColor(1,"y");
00378         style_myplain->SetAxisColor(1,"z");
00379         style_myplain->SetLabelColor(1,"x");
00380         style_myplain->SetLabelColor(1,"x");
00381         style_myplain->SetLabelColor(1,"y");
00382         style_myplain->SetLabelColor(1,"z");
00383         style_myplain->SetLabelSize(0.03,"x");
00384         style_myplain->SetLabelSize(0.03,"y");
00385         style_myplain->SetLabelSize(0.03,"z");
00386         style_myplain->SetTitleColor(1,"x");
00387         style_myplain->SetTitleColor(1,"y");
00388         style_myplain->SetTitleColor(1,"z");
00389         style_myplain->SetTitleFillColor(10);
00390 
00391         style_myplain->SetHistFillColor(10);
00392         //style_myplain->SetHistFillStyle(0);
00393         style_myplain->SetHistFillStyle(1001);
00394         style_myplain->SetHistLineColor(1);                     // 231 !!!
00395 
00396         style_myplain->SetFuncColor(kOrange+10);        // dark orange
00397         style_myplain->SetPalette(1);                           // temp�rature like
00398         
00399 //      style_myplain->SetPaperSize(100,100);  // for Hi Res plots only !
00400 
00401         style_myplain->cd();
00402 
00403         gROOT->SetStyle("myplain"); 
00404 
00405         gROOT->ForceStyle(true);
00406 
00407 
00408 
00409 // frame style not applied ! why ????????
00410 // do it manually : right click on frame then "use current style"
00411 }
00412 #endif

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7