00001
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
00045 #undef DISPLAY
00046
00047 #undef R_DEBUG
00048
00049 #undef PLOT
00050 #define WRITE
00051
00052
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
00066
00067 int main(int argc, char**argv){
00068
00069
00070
00071
00072
00073
00074
00075
00076
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
00097 int hardid = 0;
00098 int value = 0;
00099 int serial = 0;
00100
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
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
00136
00137
00138
00139
00140
00141 bool showConfig = true;
00142 if ( run != NULL && showConfig)
00143 {
00144
00145 MTDetector* det = run->GetDetector();
00146
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
00153
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
00160
00161
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
00170 if (chip.GetSerialNumber() == 14) {
00171 try{
00172
00173 const MTMicrorocChip µChip = 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
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
00191
00192
00193
00194 for( int evtNum = 0; evtNum < nbEvt ; evtNum++)
00195 {
00196 tree->GetEntry(evtNum);
00197
00198
00199
00200 nbChannel = evt->GetNchannel();
00201 for(int i=0;i<nbChannel ;i++)
00202 { channel = (MTChannel*)evt->GetChannels()->UncheckedAt(i);
00203
00204
00205 if (chipId = channel->GetChipId() == CHIP_ID)
00206 {hardid = channel->GetHardId();
00207 value = channel->GetDigitalValue();
00208 hvalue->Fill(value);
00209 if (value==(dac+1))
00210 { hscurve[hardid]->Fill(threshold);
00211 #ifdef R_DEBUG
00212 cout << "---- digital value:" << value <<endl;
00213 cout << "---- hard id:" << hardid <<endl;
00214
00215
00216 cout << "---- chip id:" << chipId <<endl;
00217 #endif
00218 }}
00219 }
00220 }
00221
00222
00223
00224
00225
00226 }
00227 }
00228 }
00229 }
00230 }
00231 }
00232
00233
00234
00235
00236
00237
00238
00239 }
00240 catch (MicroException e) {}
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 TString t_scurve_file = root_directory + root_filename ;
00254
00255
00256 int tmin = 0;
00257 int tmed = 0;
00258 int tmax = 0;
00259 int delta = 0;
00260
00261
00262
00263
00264
00265
00266
00267
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
00278
00279
00280
00281
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 ;
00295 bool has_flipped ;
00296 Int_t noisy;
00297 Int_t noise_limit = 2;
00298 Int_t noise_window = 10 ;
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++)
00304 { deriv = ( hscurve[j]->GetBinContent(k)) - (hscurve[j]->GetBinContent(k+1));
00305
00306
00307 if (k==dac_min+1)
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))
00314 { if (hscurve[j]->GetBinContent(k)>=noise_limit)
00315 noisy++ ;
00316
00317 }
00318
00319
00320
00321 if (k==dac_max-1)
00322 { if (hscurve[j]->GetBinContent(k)<=flip_limit && start_100 == 1 )
00323 { has_flipped = 1 ;
00324
00325
00326
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
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
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 {
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() );
00357
00358 gaussfunc[j]->SetParLimits(1, dac_min, dac_max) ;
00359 gaussfunc[j]->SetParameter(2, hscurve_deriv[j]->GetRMS() );
00360 gaussfunc[j]->SetParLimits(2, 0.01, hscurve_deriv[j]->GetRMS()) ;
00361
00362
00363
00364 hscurve_deriv[j]->Fit(gaussfunc[j],"WBQ", "",hscurve_deriv[j]->GetMean()-20, hscurve_deriv[j]->GetMean() + 20 );
00365
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
00381
00382 }
00383
00384
00385
00386
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
00395 }
00396 tf->Close();
00397
00398
00399 #ifdef WRITE
00400 root_filename.ReplaceAll(".root", "");
00401
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
00418 TString htitle = adj + " threshold";
00419 for (int j=0;j<64;j++)
00420 {
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
00461
00462 style_myplain->SetLegendBorderSize(1);
00463
00464 style_myplain->SetTextColor(231);
00465
00466
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("");
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");
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
00499 style_myplain->SetHistFillStyle(1001);
00500 style_myplain->SetHistLineColor(1);
00501
00502 style_myplain->SetFuncColor(kOrange+10);
00503 style_myplain->SetPalette(1);
00504
00505
00506
00507 style_myplain->cd();
00508
00509 gROOT->SetStyle("myplain");
00510
00511 gROOT->ForceStyle(true);
00512
00513
00514
00515
00516
00517 }
00518 #endif