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
00054
00055
00056
00057 #define CHIP_ID 45
00058
00059
00060 using namespace std;
00061 void usage(char *progname) ;
00062 #ifdef PLOT
00063 void set_style_myplain() ;
00064 TStyle *style_myplain=new TStyle("myplain","classic style");
00065 #endif
00066
00067
00068
00069
00070
00071
00072 int main(int argc, char**argv){
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 if(argc != 4)
00084 { usage(argv[0]) ;
00085 return 1 ;
00086 }
00087 TString root_directory = argv[1];
00088 TString root_filename = argv[2];
00089 int dac = atoi(argv[3]);
00090
00091 #ifdef PLOT
00092 set_style_myplain() ;
00093 #endif
00094
00095 TCanvas *c1 = new TCanvas("c1","Test MICROROC",0,0,1000,1000);
00096 TString root_file = root_directory + root_filename;
00097 TString scurve_file = root_directory + "scurve_" + root_filename;
00098 int nbChannel = 0;
00099 float nbEvt = 0;
00100 int threshold = 0;
00101
00102 int hardid = 0;
00103 int value = 0;
00104 int serial = 0;
00105
00106 Double_t content = 0;
00107 UInt_t chipId ;
00108 int dac_min = 1025;
00109 int dac_max = 0;
00110
00111 TH1I * hvalue = new TH1I("hvalue","",5,0,5);
00112 TH1I * hnpulse_threshold = new TH1I("hnpulse_threshold","",1024,0,1024);
00113
00114 TH1I * hscurve[64] ;
00115 TH1I * hscurve_deriv[64] ;
00116 TString name, title, name_deriv, title_deriv ;
00117 for (int j=0;j<64;j++)
00118 { name = Form("hscurve_%i",j);
00119 title = Form("SCurve from channel %i",j);
00120 hscurve[j] = new TH1I(name,title,1024,0,1024);
00121 name_deriv = Form("deriv_%i",j);
00122 title_deriv = Form("Derivative from channel %i",j);
00123 hscurve_deriv[j] = new TH1I(name_deriv,title_deriv,1024,0,1024);
00124 }
00125
00126 TFile f(root_file);
00127
00128 int ttiter=0 ;
00129
00130
00131 TIter nextkey(f.GetListOfKeys());
00132 TKey *key;
00133 while (key = (TKey*)nextkey())
00134 {
00135 TTree *tree = (TTree*)key->ReadObj();
00136 MTRun *run=NULL;
00137 try
00138 {
00139 run=dynamic_cast<MTRun *>(tree->GetUserInfo()->First());
00140
00141
00142
00143
00144
00145
00146 bool showConfig = true;
00147 if ( run != NULL && showConfig)
00148 {
00149
00150 MTDetector* det = run->GetDetector();
00151
00152
00153 const std::map<UInt_t,MTChamber*>& chambers = det->GetChambers();
00154 for( std::map<UInt_t,MTChamber*>::const_iterator ii=chambers.begin(); ii!=chambers.end(); ++ii)
00155 {
00156 const MTChamber& chamber= *((*ii).second);
00157
00158
00159
00160 const std::map<UInt_t,MTBoard*>& boards = chamber.GetBoards();
00161 for( std::map<UInt_t,MTBoard*>::const_iterator ii=boards.begin(); ii!=boards.end(); ++ii)
00162 {
00163 const MTBoard& board= *((*ii).second);
00164
00165
00166
00167
00168 map<UInt_t,MTChip*> chips = board.GetChips();
00169 for( map<UInt_t,MTChip*>::const_iterator iiChip=chips.begin(); iiChip!=chips.end(); ++iiChip)
00170 {
00171 const MTChip& chip = *((*iiChip).second);
00172 if ( chip.IsConfigured() )
00173 {
00174
00175 if (chip.GetSerialNumber() == 14) {
00176 try{
00177
00178 const MTMicrorocChip µChip = dynamic_cast<const MTMicrorocChip &> (chip);
00179
00180 if (dac==0){threshold = microChip.GetThresholdDac_0();}
00181 if (dac==1){threshold = microChip.GetThresholdDac_1();}
00182 if (dac==2){threshold = microChip.GetThresholdDac_2();}
00183
00184 }
00185 catch (...) {}
00186
00187 if (threshold<dac_min){dac_min = threshold;}
00188 if (threshold>dac_max){dac_max = threshold;}
00189
00190 MTEvent *evt = new MTEvent();
00191 TBranch *branch= tree->GetBranch("MTEvent");
00192 branch->SetAddress(&evt);
00193 MTChannel* channel = NULL;
00194 nbEvt = tree->GetEntries();
00195
00196
00197
00198
00199 for( int evtNum = 0; evtNum < nbEvt ; evtNum++)
00200 {
00201 tree->GetEntry(evtNum);
00202
00203
00204
00205 nbChannel = evt->GetNchannel();
00206 for(int i=0;i<nbChannel ;i++)
00207 { channel = (MTChannel*)evt->GetChannels()->UncheckedAt(i);
00208
00209
00210 if (chipId = channel->GetChipId() == CHIP_ID)
00211 {hardid = channel->GetHardId();
00212 value = channel->GetDigitalValue();
00213 hvalue->Fill(value);
00214 if (value==(dac+1))
00215 { hscurve[hardid]->Fill(threshold);
00216 #ifdef R_DEBUG
00217 cout << "---- digital value:" << value <<endl;
00218 cout << "---- hard id:" << hardid <<endl;
00219
00220
00221 cout << "---- chip id:" << chipId <<endl;
00222 #endif
00223 }}
00224 }
00225 }
00226
00227
00228
00229
00230
00231 }
00232 }
00233 }
00234 }
00235 }
00236 }
00237
00238
00239
00240
00241
00242
00243
00244 }
00245 catch (MicroException e) {}
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 TString t_scurve_file = root_directory + root_filename ;
00259
00260
00261 int tmin = 0;
00262 int tmed = 0;
00263 int tmax = 0;
00264 int delta = 0;
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 TH1F * hc_inflex = new TH1F("hc_inflex","",64,0,64);
00275 TH1F * hc_sigma = new TH1F("hc_sigma","",64,0,64);
00276
00277 TH1F * hc_inflex_err = new TH1F("hc_inflex_err","",64,0,64);
00278 TH1F * hc_sigma_err = new TH1F("hc_sigma_err","",64,0,64);
00279
00280 TH1F * hc_inflex_sigma = new TH1F("hc_inflex_sigma","",64,0,64);
00281
00282
00283
00284
00285
00286
00287
00288
00289 float scurve_max = 0;
00290 float scurve_inflex = 0;
00291 float inflex = 0;
00292 float sigma = 0;
00293 float inflex_err = 0;
00294 float sigma_err = 0;
00295 float chi2 = 0;
00296 float ndf = 0;
00297 Double_t deriv = 0;
00298
00299 bool start_100 ;
00300 bool has_flipped ;
00301 Int_t noisy;
00302 Int_t noise_limit = 2;
00303 Int_t noise_window = 10 ;
00304 Int_t flip_limit = 30 ;
00305
00306 for (int j=0;j<64;j++)
00307 { noisy = 0 ;
00308 for (int k=dac_min+1;k<=dac_max;k++)
00309 { deriv = ( hscurve[j]->GetBinContent(k)) - (hscurve[j]->GetBinContent(k+1));
00310
00311
00312 if (k==dac_min+1)
00313 { if (hscurve[j]->GetBinContent(k)>=100)
00314 start_100 = 1 ;
00315 else start_100 = 0 ;
00316 }
00317
00318 if (k>(dac_max - noise_window))
00319 { if (hscurve[j]->GetBinContent(k)>=noise_limit)
00320 noisy++ ;
00321
00322 }
00323
00324
00325
00326 if (k==dac_max-1)
00327 { if (hscurve[j]->GetBinContent(k)<=flip_limit && start_100 == 1 )
00328 { has_flipped = 1 ;
00329
00330
00331
00332 }
00333 else has_flipped = 0 ;
00334 }
00335 if (deriv > 0.)
00336 { hscurve_deriv[j]->SetBinContent(k,deriv);
00337 hscurve_deriv[j]->SetEntries((hscurve_deriv[j]->GetEntries())+deriv);
00338
00339 }
00340 }
00341 if (has_flipped == 0)
00342 { cout << "****** " << argv[0] << ": Warning! Chip " << serial << " Channel " << j << " has not flipped" ;
00343 if (start_100 == 1) cout << " (stuck to 100 %)" << endl ;
00344 else cout << " (stuck to 0 %)" << endl ;
00345 }
00346 if (noisy==noise_window) cout << "****** " << argv[0] << ": Warning! Chip " << serial << " Channel " << j << " may be noisy, check root scurve file !" << endl ;
00347
00348 }
00349
00350
00351 TF1 * gaussfunc[64];
00352 for (int j=0;j<64;j++)
00353 { name = Form("g_%i",j);
00354 gaussfunc[j] = new TF1(name,"gaus",0,1024);
00355 }
00356
00357 for (int j=0;j<64;j++)
00358 {
00359 gaussfunc[j]->SetParameter(0, hscurve_deriv[j]->GetMaximumBin());
00360 gaussfunc[j]->SetParLimits(0, 1, 100);
00361 gaussfunc[j]->SetParameter(1, hscurve_deriv[j]->GetMean() );
00362
00363 gaussfunc[j]->SetParLimits(1, dac_min, dac_max) ;
00364 gaussfunc[j]->SetParameter(2, hscurve_deriv[j]->GetRMS() );
00365 gaussfunc[j]->SetParLimits(2, 0.01, hscurve_deriv[j]->GetRMS()) ;
00366
00367
00368
00369 hscurve_deriv[j]->Fit(gaussfunc[j],"WBQ", "",hscurve_deriv[j]->GetMean()-20, hscurve_deriv[j]->GetMean() + 20 );
00370
00371 inflex = gaussfunc[j]->GetParameter(1);
00372 if (inflex<=dac_min || inflex>=dac_max)
00373 { cout << "****** " << argv[0] << ": Warning! Chip " << serial << " Channel " << j << ", fit may be not correct ! Inflexion: " << inflex << " Dac min: " << dac_min << " Dac max: " << dac_max << endl ;
00374 }
00375 sigma = gaussfunc[j]->GetParameter(2);
00376 inflex_err = gaussfunc[j]->GetParError(1);
00377 sigma_err =gaussfunc[j]->GetParError(2);
00378
00379 hc_inflex->SetBinContent(j+1,inflex);
00380 hc_sigma->SetBinContent(j+1,sigma);
00381
00382 hc_inflex_err->SetBinContent(j+1,inflex_err);
00383 hc_sigma_err->SetBinContent(j+1,sigma_err);
00384
00385
00386
00387 }
00388
00389
00390
00391
00392
00393 TFile * tf = new TFile(scurve_file,"RECREATE");
00394 hvalue->Write();
00395 hnpulse_threshold->Write();
00396 for (int j=0;j<64;j++)
00397 { hscurve[j]->Write();
00398 hscurve_deriv[j]->Write();
00399
00400 }
00401 tf->Close();
00402
00403
00404 #ifdef WRITE
00405 root_filename.ReplaceAll(".root", "");
00406
00407 TString outfile = root_directory + "../RESULTS/" + root_filename + ".txt";
00408 ofstream out ;
00409 out.open(outfile) ;
00410 for (int j=0;j<64;j++)
00411 { 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;
00412 }
00413 #endif
00414 #ifdef PLOT
00415 TApplication theApp("App", &argc, argv);
00416 TString adj;
00417 if (dac==0){adj = "Low";}
00418 if (dac==1){adj = "Medium";}
00419 if (dac==2){adj = "High";}
00420
00421 c1->Divide(8,8);
00422
00423 TString htitle = adj + " threshold";
00424 for (int j=0;j<64;j++)
00425 {
00426 c1->cd(j+1);
00427 hscurve[j]->SetLineWidth(2);
00428 hscurve[j]->Draw("");
00429 c1->Update() ;
00430 }
00431 theApp.Run(kTRUE) ;
00432 c1->Update() ;
00433 #endif
00434 return 0 ;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443 void usage(char *progname)
00444 {
00445 printf("Usage : %s directory rootfile [0|1|2]", progname);
00446 printf(" Where [0|1|2] is the dac number\n");
00447 printf(" scurve_rootfile.root is created\n");
00448 }
00449
00450
00451
00452 #ifdef PLOT
00453 void set_style_myplain()
00454 {
00455 style_myplain->SetCanvasBorderMode(0);
00456 style_myplain->SetCanvasColor(0);
00457 style_myplain->SetDrawBorder(0);
00458 style_myplain->SetPadBorderMode(0);
00459 style_myplain->SetPadColor(10);
00460 style_myplain->SetFrameLineColor(1);
00461 style_myplain->SetFrameFillColor(5);
00462 style_myplain->SetFrameFillStyle(0);
00463 style_myplain->SetFrameBorderMode(0);
00464
00465
00466
00467 style_myplain->SetLegendBorderSize(1);
00468
00469 style_myplain->SetTextColor(231);
00470
00471
00472 style_myplain->SetLineColor(231);
00473
00474 style_myplain->SetStatColor(10);
00475 style_myplain->SetStatTextColor(1);
00476 style_myplain->SetStatBorderSize(1) ;
00477 style_myplain->SetStatX(0.9);
00478 style_myplain->SetStatY(0.9);
00479 style_myplain->SetOptStat("");
00480 style_myplain->SetOptFit(0111);
00481
00482 style_myplain->SetTitleTextColor(1);
00483 style_myplain->SetTitleBorderSize(0);
00484 style_myplain->SetTitleAlign(23);
00485 style_myplain->SetTitleX(0.5);
00486
00487 style_myplain->SetAxisColor(1,"x");
00488 style_myplain->SetAxisColor(1,"y");
00489 style_myplain->SetAxisColor(1,"z");
00490 style_myplain->SetLabelColor(1,"x");
00491 style_myplain->SetLabelColor(1,"x");
00492 style_myplain->SetLabelColor(1,"y");
00493 style_myplain->SetLabelColor(1,"z");
00494 style_myplain->SetLabelSize(0.03,"x");
00495 style_myplain->SetLabelSize(0.03,"y");
00496 style_myplain->SetLabelSize(0.03,"z");
00497 style_myplain->SetTitleColor(1,"x");
00498 style_myplain->SetTitleColor(1,"y");
00499 style_myplain->SetTitleColor(1,"z");
00500 style_myplain->SetTitleFillColor(10);
00501
00502 style_myplain->SetHistFillColor(10);
00503
00504 style_myplain->SetHistFillStyle(1001);
00505 style_myplain->SetHistLineColor(1);
00506
00507 style_myplain->SetFuncColor(kOrange+10);
00508 style_myplain->SetPalette(1);
00509
00510
00511
00512 style_myplain->cd();
00513
00514 gROOT->SetStyle("myplain");
00515
00516 gROOT->ForceStyle(true);
00517
00518
00519
00520
00521
00522 }
00523 #endif