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

Go to the documentation of this file.
00001 /* @version $Revision: 1328 $ * @modifiedby $Author: jacquem $ * @lastmodified $Date: 2011-10-03 17:04:17 +0200 (Mon, 03 Oct 2011) $ */
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/MTTrack.hh"
00030 #include "root/MTChamber.hh"
00031 #include "root/MTDetector.hh"
00032 #include "root/MTBoard.hh"
00033 #include "root/MTChip.hh"
00034 #include "root/MTChannel.hh"
00035 #include "root/MTEvent.hh"
00036 #include "root/MTDif.hh"
00037 #include "root/MTHardroc1Chip.hh"
00038 #include "root/MTHardroc2Chip.hh"
00039 #include "root/MTDiracChip.hh"
00040 #include "root/MTMicrorocChip.hh"
00041 
00042 #include "MicroException.hh"
00043 
00044 //#define DISPLAY
00045 #undef DISPLAY
00046 //#define R_DEBUG
00047 #undef R_DEBUG
00048 //#define PLOT
00049 #undef PLOT
00050 #define WRITE
00051 
00052 //#define CHIP_ID 32
00053 #define CHIP_ID 33
00054 
00055 using namespace std;
00056 void usage(char *progname) ;
00057 #ifdef PLOT
00058         void set_style_myplain() ;
00059         TStyle *style_myplain=new TStyle("myplain","classic style");
00060 #endif
00061 ////////////////////////////////////////////////////////////////////////////////////////////
00062 
00063 
00064 
00065 // src/analyse/root/example.cpp
00066 
00067 int main(int argc, char**argv){
00068 
00069   /*****************************************************************/
00070   /* ARGUMENTS TO THE PROGRAM **************************************/
00071   /* 1 - DIRECTORY OF THE ROOT FILE PRODUCED BY THE RECONSTRUCTION */
00072   /* 2 - NAME OF THE ROOT FILE *************************************/
00073   /* 3 - THRESHOLD THAT IS MOVED DURING THE CALIBRATION ************/
00074   /*    0 -> DAC_0 (LOW THRESHOLD) *********************************/
00075   /*    1 -> DAC_1 (MEDIUM THRESHOLD) ******************************/
00076   /*    2 -> DAC_2 (HIGH THRESHOLD) ********************************/
00077   /*****************************************************************/
00078         if(argc != 4)
00079         {       usage(argv[0]) ;
00080                 return 1 ;
00081         }
00082         TString root_directory = argv[1];
00083         TString root_filename = argv[2];
00084         int dac = atoi(argv[3]);
00085 
00086         #ifdef PLOT
00087                 set_style_myplain() ;
00088         #endif
00089         
00090         TCanvas *c1 = new TCanvas("c1","Test MICROROC",0,0,1000,1000);
00091         TString root_file = root_directory + root_filename;
00092         TString scurve_file = root_directory + "scurve_" + root_filename;
00093         int nbChannel = 0;
00094         float nbEvt = 0;
00095         int threshold = 0;
00096         //int chipid = 0;
00097         int hardid = 0;
00098         int value = 0;
00099         int serial = 0;
00100         //int content = 0;
00101         Double_t content = 0;
00102         UInt_t chipId ;
00103     int dac_min = 1025;
00104         int dac_max = 0;
00105         
00106         TH1I * hvalue = new TH1I("hvalue","",5,0,5);
00107         TH1I * hnpulse_threshold = new TH1I("hnpulse_threshold","",1024,0,1024);
00108 
00109         TH1I * hscurve[64] ;
00110         TH1I * hscurve_deriv[64] ;
00111         TString name, title, name_deriv, title_deriv ;
00112         for (int j=0;j<64;j++)
00113         {       name  = Form("hscurve_%i",j);
00114                 title = Form("SCurve from channel %i",j);
00115                 hscurve[j] = new TH1I(name,title,1024,0,1024);
00116                 name_deriv  = Form("deriv_%i",j);
00117                 title_deriv = Form("Derivative from channel %i",j);
00118                 hscurve_deriv[j] = new TH1I(name_deriv,title_deriv,1024,0,1024);
00119         }
00120   
00121         TFile f(root_file);
00122         
00123         int ttiter=0 ;
00124         
00125         // from example file !
00126         TIter nextkey(f.GetListOfKeys());
00127         TKey *key;
00128   while (key = (TKey*)nextkey())
00129   {
00130   TTree *tree = (TTree*)key->ReadObj();                
00131   MTRun *run=NULL;
00132   try 
00133         {
00134     run=dynamic_cast<MTRun *>(tree->GetUserInfo()->First());
00135 //    cout << "---------------------------- NEW TTREE ---------------------------"<< endl;
00136 //    ttiter++ ;
00137 //    cout << ttiter << endl ;
00138 //    nbEvt =0;
00139 //    nbHit = 0;
00140 
00141     bool showConfig = true;
00142     if ( run != NULL && showConfig) 
00143     {
00144         //run->Info(); 
00145                         MTDetector* det = run->GetDetector();
00146                         // loop over chamber config
00147                                 
00148                         const std::map<UInt_t,MTChamber*>& chambers = det->GetChambers();
00149                         for( std::map<UInt_t,MTChamber*>::const_iterator ii=chambers.begin(); ii!=chambers.end(); ++ii)
00150                         {
00151                                 const MTChamber& chamber= *((*ii).second);
00152                                 //cout << "chamber id: " << chamber.GetId() << endl;
00153                                 //cout << "chamber type: " << chamber.GetType() << endl;
00154 
00155                                 const std::map<UInt_t,MTBoard*>& boards = chamber.GetBoards();
00156                                 for( std::map<UInt_t,MTBoard*>::const_iterator ii=boards.begin(); ii!=boards.end(); ++ii)
00157                                 {
00158                                         const MTBoard& board= *((*ii).second);
00159                                         //board.Info();
00160 
00161                                         //cout << "\t\t\t**** Chips configuration ****" << endl ;
00162 
00163                                         map<UInt_t,MTChip*> chips = board.GetChips();
00164                                         for( map<UInt_t,MTChip*>::const_iterator iiChip=chips.begin(); iiChip!=chips.end(); ++iiChip)
00165                                         {
00166                                                 const MTChip& chip  = *((*iiChip).second);
00167                                                 if ( chip.IsConfigured() )
00168                                                 {
00169                                                 //cout <<  "   -- New Chip configuration:" <<  chip.GetSoftId().toString() << " softId[" << chip.GetSoftId().GetValue() <<"], serial Number[" << chip.GetSerialNumber() << "]"<< endl <<endl  <<endl ;
00170                                                         if (chip.GetSerialNumber() == 14) { // serial 14 = id 32 -> see xml file
00171                                                         try{
00172                                                         
00173                                                         const MTMicrorocChip &microChip = dynamic_cast<const MTMicrorocChip &> (chip);
00174 
00175                                                         if (dac==0){threshold = microChip.GetThresholdDac_0();}
00176                                                         if (dac==1){threshold = microChip.GetThresholdDac_1();}
00177                                                         if (dac==2){threshold = microChip.GetThresholdDac_2();}
00178                                                         //cout << "Chip: " << chip.GetSerialNumber() << " SoftId: " << chip.GetSoftId().toString() << " Threshold:" << threshold << " SoftId:" << endl ;
00179                                                         }
00180                                                         catch (...) {}
00181                                                         
00182                                                 if (threshold<dac_min){dac_min = threshold;}
00183                                         if (threshold>dac_max){dac_max = threshold;}
00184 
00185                                                 MTEvent *evt =  new MTEvent();
00186                                                 TBranch *branch= tree->GetBranch("MTEvent");
00187                                                 branch->SetAddress(&evt);
00188                                                 MTChannel* channel = NULL;
00189                                                 nbEvt = tree->GetEntries();
00190                                                 //cout << "Number of events in the tree: " << nbEvt << endl ;
00191 
00192 //                                              hnpulse_threshold->SetBinContent(threshold+1,nbEvt);
00193 
00194                                                 for( int evtNum = 0; evtNum < nbEvt ; evtNum++)
00195                                                 {       //if (evtNum%1000 == 0) {cout<<evtNum/nbEvt*100.<<" %"<<"\r"<<flush;}
00196                                                         tree->GetEntry(evtNum);
00197 //                                                      if (chip.GetSerialNumber() == 14) { 
00198                                                         //ttiter++ ;
00199                                                         //cout << "procees " << ttiter << endl ;
00200                                                         nbChannel = evt->GetNchannel();
00201                                                         for(int i=0;i<nbChannel ;i++)
00202                                                         {       channel = (MTChannel*)evt->GetChannels()->UncheckedAt(i);
00203                                                                 //if (channel->getChip()->GetSerialNumber() == 14)
00204                                                                 //cout << channel->>GetSerialNumber() << endl ;
00205                                                                 if (chipId = channel->GetChipId() == CHIP_ID) // numéro du chip
00206                                                                 {hardid = channel->GetHardId(); // numéro de la voie
00207                                                                 value = channel->GetDigitalValue();
00208                                                                 hvalue->Fill(value);
00209                                                                 if (value==(dac+1)) // ie 1 for low, 2 for med and 3 for high ....
00210                                                                 {       hscurve[hardid]->Fill(threshold);
00211                                                                         #ifdef R_DEBUG
00212                                                                                 cout << "---- digital value:" <<  value <<endl;
00213                                                                                 cout << "---- hard id:" << hardid <<endl;
00214                                                                                 //cout << "---- x:"<< channel->GetX() <<endl;
00215                                                                                 //cout << "---- y:" << channel->GetY() <<endl;
00216                                                                                 cout << "---- chip id:" << chipId <<endl;
00217                                                                         #endif
00218                                                                 }}
00219                                                         }
00220                                                 }
00221         
00222                                                         
00223                                                         
00224                                                         
00225                                                 
00226 } // fin serial == 14   
00227                                         }
00228                                 }
00229                         }
00230                 }
00231         }
00232         
00233         
00234 
00235         
00236         
00237         
00238         
00239         }
00240         catch (MicroException e) {}             
00241         }       
00242                                                 
00243 
00244 //      cout << "ttree iter " << ttiter << endl ;
00245 
00246 //cout << "           " << endl ;
00247 
00248         
00249         //===========================================================================*
00250         // Now analyze scurves  
00251         //===========================================================================*
00252 
00253         TString t_scurve_file = root_directory + root_filename ;//+ ".root";
00254         //cout << t_scurve_file<< endl;
00255 
00256         int tmin = 0;
00257         int tmed = 0;
00258         int tmax = 0;
00259         int delta = 0;
00260 
00261 //      TH1I * hscurve[64];
00262 
00263 /*      TH1F * hinflex = new TH1F("hinflex","",1024,0,1024);
00264         TH1F * hsigma = new TH1F("hsigma","",50,-2,2);
00265 
00266         TH1F * hinflex_err = new TH1F("hinflex_err","",100,0,10);
00267         TH1F * hsigma_err = new TH1F("hsigma_err","",100,0,2);
00268 */
00269         TH1F * hc_inflex = new TH1F("hc_inflex","",64,0,64);
00270         TH1F * hc_sigma = new TH1F("hc_sigma","",64,0,64);
00271 
00272         TH1F * hc_inflex_err = new TH1F("hc_inflex_err","",64,0,64);
00273         TH1F * hc_sigma_err = new TH1F("hc_sigma_err","",64,0,64);
00274 
00275         TH1F * hc_inflex_sigma = new TH1F("hc_inflex_sigma","",64,0,64);
00276 
00277 //      TString name;
00278 /*      for (int j=0;j<64;j++)
00279         {       name  = Form("hscurve_%i",j);
00280                 TH1I * h = (TH1I*)tf.Get(name);
00281                 hscurve[j] = h;
00282         }*/
00283 
00284         float scurve_max = 0;
00285         float scurve_inflex = 0;
00286         float inflex = 0;
00287         float sigma = 0;
00288         float inflex_err = 0;
00289         float sigma_err = 0;
00290         float chi2 = 0;
00291         float ndf = 0;
00292         Double_t deriv = 0;
00293         
00294         bool start_100 ; // test if a channel starts at 100% else warning
00295         bool has_flipped ; // test if each channel flips from 100% to 0% else, warning message
00296         Int_t noisy;    // test if each channel is noisy, ie has entry beyond flip point
00297         Int_t noise_limit = 2;  // number of entry accepted
00298         Int_t noise_window = 10 ;// number of bin defore dac_max to verify noise
00299         Int_t flip_limit = 30 ;
00300           
00301         for (int j=0;j<64;j++)
00302         {       noisy = 0 ;
00303                 for (int k=dac_min+1;k<=dac_max;k++) // ATTN : bin = dac + 1 !!
00304                 {       deriv = ( hscurve[j]->GetBinContent(k)) - (hscurve[j]->GetBinContent(k+1));
00305                         //cout << j << " " << k << " " << hscurve[j]->GetBinContent(k) << " " << hscurve[j]->GetBinContent(k+1) << " " << deriv << endl ;
00306                         // test if each channel has flipped
00307                         if  (k==dac_min+1) // first loop
00308                         {       if (hscurve[j]->GetBinContent(k)>=100)
00309                                         start_100 = 1 ;
00310                                 else start_100 = 0 ;
00311                         }
00312                         
00313                         if  (k>(dac_max - noise_window)) // test noisy channel
00314                         {       if (hscurve[j]->GetBinContent(k)>=noise_limit)
00315                                         noisy++ ;
00316                                 //cout << "Channel " << j << " dac " << k << " noise " << noisy << endl ;
00317                         }
00318                         
00319                         
00320                         
00321                         if  (k==dac_max-1) // last loop has flipped ?
00322                         {       if (hscurve[j]->GetBinContent(k)<=flip_limit && start_100 == 1 )
00323                                 {       has_flipped = 1 ;
00324                                         //if (hscurve[j]->GetBinContent(k)>0)
00325                                         //      noisy++;// = 0 ;
00326                                         //else noisy = 1 ;
00327                                 }
00328                                 else has_flipped = 0 ;
00329                         }                       
00330                         if (deriv > 0.)
00331                         {       hscurve_deriv[j]->SetBinContent(k,deriv);
00332                                 hscurve_deriv[j]->SetEntries((hscurve_deriv[j]->GetEntries())+deriv);
00333                                 //hscurve_deriv[j]->Fill(k,deriv);
00334                         }
00335                 }
00336                 if (has_flipped == 0)
00337                 {       cout << "****** " << argv[0] << ": Warning! Chip " << serial << " Channel " << j << " has not flipped" ;
00338                         if (start_100 == 1) cout << " (stuck to 100 %)" << endl ;
00339                         else cout << " (stuck to 0 %)" << endl ;
00340                 }
00341                 if (noisy==noise_window) cout << "****** " << argv[0] << ": Warning! Chip " << serial << " Channel " << j << " may be noisy, check root scurve file !" << endl ;
00342                 
00343          }
00344 
00345 // FIT
00346         TF1 * gaussfunc[64];
00347         for (int j=0;j<64;j++)
00348         {       name = Form("g_%i",j);
00349                 gaussfunc[j] = new TF1(name,"gaus",0,1024);
00350         }
00351         
00352         for (int j=0;j<64;j++)
00353         {       //gaussfunc[j]->SetParLimits(1, dac_min, dac_max) ;
00354                 gaussfunc[j]->SetParameter(0, hscurve_deriv[j]->GetMaximumBin());
00355                 gaussfunc[j]->SetParLimits(0, 1, 100);
00356                 gaussfunc[j]->SetParameter(1, hscurve_deriv[j]->GetMean() ); //GetMaximumBin()
00357                 //gaussfunc[j]->FixParameter(1, hscurve_deriv[j]->GetMean() );
00358                 gaussfunc[j]->SetParLimits(1, dac_min, dac_max) ; //hscurve_deriv[j]->GetMaximumBin()-10, hscurve_deriv[j]->GetMaximumBin() + 10) ;
00359                 gaussfunc[j]->SetParameter(2, hscurve_deriv[j]->GetRMS() );
00360                 gaussfunc[j]->SetParLimits(2, 0.01, hscurve_deriv[j]->GetRMS()) ;
00361 //              if (j==48)
00362 //              hscurve_deriv[j]->Fit(gaussfunc[j],"BV", "",hscurve_deriv[j]->GetMean()-15, hscurve_deriv[j]->GetMean() + 15 );
00363 //              else
00364                                 hscurve_deriv[j]->Fit(gaussfunc[j],"WBQ", "",hscurve_deriv[j]->GetMean()-20, hscurve_deriv[j]->GetMean() + 20 );
00365                 //hscurve_deriv[j]->Fit(gaussfunc[j],"Q");
00366                 inflex = gaussfunc[j]->GetParameter(1);
00367                 if (inflex<=dac_min || inflex>=dac_max)
00368                 {       cout << "****** " << argv[0] << ": Warning! Chip " << serial << " Channel " << j << ", fit may be not correct ! Inflexion: " << inflex << " Dac min: " << dac_min << " Dac max: " << dac_max << endl ;
00369                 }
00370                 sigma = gaussfunc[j]->GetParameter(2);
00371                 inflex_err = gaussfunc[j]->GetParError(1);
00372                 sigma_err =gaussfunc[j]->GetParError(2);
00373                 
00374                 hc_inflex->SetBinContent(j+1,inflex);
00375                 hc_sigma->SetBinContent(j+1,sigma);             
00376 
00377                 hc_inflex_err->SetBinContent(j+1,inflex_err);
00378                 hc_sigma_err->SetBinContent(j+1,sigma_err);
00379 
00380                 //cout << "Channel " << j << " init at " << hscurve_deriv[j]->GetMean() << " " << gaussfunc[j]->GetParameter(1) << endl ;
00381                 // cout << j << " " << inflex << " " << sigma << endl ;
00382         }
00383 
00384 
00385         //////////////////////////////////////////////////////////////////////////////////////////
00386         // Write Scurves
00387         //////////////////////////////////////////////////////////////////////////////////////////
00388         TFile * tf = new TFile(scurve_file,"RECREATE");
00389         hvalue->Write();
00390         hnpulse_threshold->Write();
00391         for (int j=0;j<64;j++)
00392         {       hscurve[j]->Write();
00393                 hscurve_deriv[j]->Write();
00394                 //gaussfunc[j]->Write();
00395         }
00396         tf->Close();
00397         
00398         
00399         #ifdef WRITE
00400                 root_filename.ReplaceAll(".root", "");
00401                 //TString outfile = root_directory + "../RESULTS/" +  "parameters_" + root_filename + ".txt";
00402                 TString outfile = root_directory + "../RESULTS/" +  root_filename + ".txt";
00403                 ofstream out ;
00404                 out.open(outfile) ;
00405                 for (int j=0;j<64;j++)
00406                 {       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;
00407                 }
00408         #endif
00409         #ifdef PLOT
00410                 TApplication theApp("App", &argc, argv);
00411                 TString adj;
00412                 if (dac==0){adj = "Low";}
00413                 if (dac==1){adj = "Medium";}
00414                 if (dac==2){adj = "High";}
00415                 
00416                 c1->Divide(8,8);
00417                 //c1->cd();
00418                 TString htitle = adj + " threshold";
00419                 for (int j=0;j<64;j++)
00420                 {       //c0->cd(j/8,j%8);
00421                         c1->cd(j+1);
00422                         hscurve[j]->SetLineWidth(2);
00423                         hscurve[j]->Draw("");
00424                         c1->Update() ;
00425                 }
00426                 theApp.Run(kTRUE) ;
00427                 c1->Update() ;
00428         #endif
00429     return 0 ;
00430 }
00431 
00432 
00433 
00434 //**********************************************************************************************
00435 //**********************************************************************************************
00436 
00437 
00438 void usage(char *progname)
00439 {
00440         printf("Usage : %s directory rootfile [0|1|2]", progname);
00441         printf("        Where [0|1|2] is the dac number\n");
00442         printf("        scurve_rootfile.root is created\n");
00443 }
00444 
00445 //**********************************************************************************************
00446 //**********************************************************************************************
00447 #ifdef PLOT
00448 void set_style_myplain()
00449 {
00450         style_myplain->SetCanvasBorderMode(0);
00451         style_myplain->SetCanvasColor(0);
00452         style_myplain->SetDrawBorder(0);
00453         style_myplain->SetPadBorderMode(0);
00454         style_myplain->SetPadColor(10);
00455         style_myplain->SetFrameLineColor(1);
00456         style_myplain->SetFrameFillColor(5);
00457         style_myplain->SetFrameFillStyle(0);
00458         style_myplain->SetFrameBorderMode(0);
00459 
00460         //style_myplain->SetFrameLineWidth(5); //test for setframelinecolor !
00461 
00462         style_myplain->SetLegendBorderSize(1);
00463 
00464         style_myplain->SetTextColor(231);
00465 
00466 //      style_myplain->SetFillColor(0); // If enable : impossible to fill histo with color !!! see note in the end !
00467         style_myplain->SetLineColor(231);
00468 
00469         style_myplain->SetStatColor(10);
00470         style_myplain->SetStatTextColor(1);
00471         style_myplain->SetStatBorderSize(1) ;
00472         style_myplain->SetStatX(0.9);
00473         style_myplain->SetStatY(0.9);
00474         style_myplain->SetOptStat("");//"emr" by default
00475         style_myplain->SetOptFit(0111);
00476 
00477         style_myplain->SetTitleTextColor(1);
00478         style_myplain->SetTitleBorderSize(0);
00479         style_myplain->SetTitleAlign(23);
00480         style_myplain->SetTitleX(0.5);
00481 
00482         style_myplain->SetAxisColor(1,"x");     // bleu
00483         style_myplain->SetAxisColor(1,"y");
00484         style_myplain->SetAxisColor(1,"z");
00485         style_myplain->SetLabelColor(1,"x");
00486         style_myplain->SetLabelColor(1,"x");
00487         style_myplain->SetLabelColor(1,"y");
00488         style_myplain->SetLabelColor(1,"z");
00489         style_myplain->SetLabelSize(0.03,"x");
00490         style_myplain->SetLabelSize(0.03,"y");
00491         style_myplain->SetLabelSize(0.03,"z");
00492         style_myplain->SetTitleColor(1,"x");
00493         style_myplain->SetTitleColor(1,"y");
00494         style_myplain->SetTitleColor(1,"z");
00495         style_myplain->SetTitleFillColor(10);
00496 
00497         style_myplain->SetHistFillColor(10);
00498         //style_myplain->SetHistFillStyle(0);
00499         style_myplain->SetHistFillStyle(1001);
00500         style_myplain->SetHistLineColor(1);                     // 231 !!!
00501 
00502         style_myplain->SetFuncColor(kOrange+10);        // dark orange
00503         style_myplain->SetPalette(1);                           // temp�rature like
00504         
00505 //      style_myplain->SetPaperSize(100,100);  // for Hi Res plots only !
00506 
00507         style_myplain->cd();
00508 
00509         gROOT->SetStyle("myplain"); 
00510 
00511         gROOT->ForceStyle(true);
00512 
00513 
00514 
00515 // frame style not applied ! why ????????
00516 // do it manually : right click on frame then "use current style"
00517 }
00518 #endif

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