def add_statistics(self, histo, pkl_name = None, fit = None) :
		print 'histo %f .. %f with %d bins' % (histo.GetBinLowEdge(1), histo.GetXaxis().GetBinUpEdge(histo.GetNbinsX()), histo.GetNbinsX())
		if pkl_name == None : pkl_name = self.name
		histos = {}
		histos['data'] = histo
		res = {}
		res['mean'    ] = histo.GetMean()
		res['mean_err'] = histo.GetMeanError()
		print 'Mean: %f' % res['mean']
		histos['stat'] = ROOT.TH1F('%s_stat' % pkl_name, 'stat', histo.GetNbinsX(), 0., 1.)
		histos['stat'].SetBinContent(1, self.run_no    )
		histos['stat'].SetBinError  (1, 0              )
		histos['stat'].SetBinContent(2, res['mean'    ])
		histos['stat'].SetBinError  (2, res['mean_err'])
		histos['stat'].SetBinContent(3, histo.GetRMS     ())
		histos['stat'].SetBinError  (3, histo.GetRMSError())
		histos['stat'].SetBinContent(4, histo.Integral())
		histos['stat'].SetBinContent(7, self.nstrips)
		if self.run_config_file != '' and self.run_config.has_section('%d' % self.run_no) and self.det_type == 'Dia' :
			histos['stat'].SetBinContent(5, eval(self.run_config.get('%d' % self.run_no, 'calibration'    )))
			histos['stat'].SetBinError  (5, eval(self.run_config.get('%d' % self.run_no, 'calibration_err')))
			histos['stat'].SetBinContent(6, eval(self.run_config.get('%d' % self.run_no, 'fluence'    ))/1e15)
			histos['stat'].SetBinError  (6, eval(self.run_config.get('%d' % self.run_no, 'fluence_err'))/1e15)
		if fit != None :
			for i in [0, 1, 2] :
				histos['stat'].SetBinContent(i+10, fit.GetParameter(i))
				histos['stat'].SetBinError  (i+10, fit.GetParError(i))
			res['sigma'    ] = fit.GetParameter(2)
			res['sigma_err'] = fit.GetParError(2)
			mean  = (fit.GetParameter(1), fit.GetParError(1))
			sigma = (fit.GetParameter(2), fit.GetParError(2))
			print 'Mean : %s +- %s' % rn.get_roundedNumber(*mean )
			print 'Sigma: %s +- %s' % rn.get_roundedNumber(*sigma)
		helper.save_object(res, '%s%s_stat.pkl' % (self.output_path, pkl_name))
		return histos
	def plot(self) :
		print '[status] plotting %s..' % self.name
		rd42Style()
		if self.histo_type == 'PulseHeight' :
			ROOT.gStyle.SetOptStat('m')
		if self.histo_type == 'Noise' :
			ROOT.gStyle.SetOptFit(01111)
#		ROOT.gStyle.SetDrawOption('colz')
#		ROOT.gStyle.SetCanvasDefW(1200)
		canvas = ROOT.TCanvas('%s_%s' % (self.name, self.rand.Integer(10000)), 'canvas')
		histo = self.get_histo()
		if 'PulseHeight_Cluster1-' in self.histo_type :
			for i in range(1, self.nstrips) :
				if self.position != '' :
					path_tmp = self.path.rstrip('%d/%s/' % (self.run_no, self.position))
				else :
					path_tmp = self.path.rstrip('%d/' % self.run_no)
				pl = plotter(self.config_file, path_tmp, self.output_path.rstrip('%d' % self.run_no), self.run_no, self.position, 'PulseHeight_Cluster%d_%s' % (i, self.histo_type.split('_')[-1]), self.run_config_file)
				histo_tmp = pl.get_histo()
				histo.Add(histo_tmp)
		canvas.cd()
		histo.Draw(self.draw_opt)
		histo.GetXaxis().SetTitle(self.xTitle)
		histo.GetYaxis().SetTitle(self.yTitle)
		self.draw_rd42Line()
		canvas.UseCurrentStyle()
		if histo.GetListOfFunctions().FindObject('palette') == None :
			histo.SetMaximum(1.4 * histo.GetMaximum())
		else :
#			canvas.SetWindowSize(1200, 600)
			canvas.SetRightMargin(canvas.GetRightMargin() + 0.08)
			pal = histo.GetListOfFunctions().FindObject('palette')
			pal_offset = 0.012
			pal.SetX1NDC(1. - canvas.GetRightMargin() + pal_offset)
			pal.SetX2NDC(1. - canvas.GetRightMargin() + pal_offset + 0.82*histo.GetZaxis().GetTickLength())
			pal.SetY1NDC(canvas.GetBottomMargin())
			pal.SetY2NDC(1. - canvas.GetTopMargin())
		if self.histo_type == 'FidCut' or self.histo_type == 'TrackPos' :
			self.save_TH2histo2table(histo, path = '%s%s.dat' % (self.output_path, self.name), rebinx = 4, rebiny = 4, xmin = 48., ymin = 48., sfx = 0.05, sfy = 0.05)
			fid_cut = self.get_fidCut()
			fid_cut.SetLineColor(ROOT.kRed)
#			fid_cut.Dump()
			canvas.cd()
			fid_cut.Draw('same')
		if 'Alignment_Plane2_YPred_DeltaX' in self.histo_type :
			if 'Post' in self.histo_type :
				self.save_TH2histo2table(histo, path = '%s%s.dat' % (self.output_path, self.name), rebinx = 3, rebiny = 5, xmin = 3000., ymin = -15., nxbins = 30, nybins = 96, sfx = 0.001, sfy = 1.)
			else :
				self.save_TH2histo2table(histo, path = '%s%s.dat' % (self.output_path, self.name), rebinx = 4, rebiny = 2, xmin = 3000., ymin = -260., nxbins = 32, nybins = 146, sfx = 0.001, sfy = 1.)
			return
		if 'Alignment_Plane2_XPred_DeltaY' in self.histo_type :
			if 'Post' in self.histo_type :
				self.save_TH2histo2table(histo, path = '%s%s.dat' % (self.output_path, self.name), rebinx = 4, rebiny = 13, xmin = 3000., ymin = -6.2, nxbins = 30, nybins = 37, sfx = 0.001, sfy = 1.)
			else :
				self.save_TH2histo2table(histo, path = '%s%s.dat' % (self.output_path, self.name), rebinx = 4, rebiny = 2, xmin = 3000., ymin = -540., nxbins = 32, nybins = 148, sfx = 0.001, sfy = 1.)
			return
		if self.histo_type == 'StripMeanPedestal_Dia' :
			profile = histo.ProfileX('profile', 1, -1, 'os')
			nbins = profile.GetNbinsX()
			sigma_histo = ROOT.TH1F('ped_sigma', 'ped_sigma', nbins, profile.GetBinLowEdge(1), profile.GetXaxis().GetBinUpEdge(nbins))
			for ibin in range(1, nbins+1) :
				projection = histo.ProjectionY('tmp%d' % ibin, ibin, ibin, 'o')
				sigma_histo.SetBinContent(ibin, projection.GetRMS())
				sigma_histo.SetBinError  (ibin, projection.GetRMSError())
			print profile.GetEntries()
			print profile.GetBinLowEdge(1)
			print profile.GetBinContent(1)
			print profile.GetBinError(1)
			histos = self.add_statistics(profile)
			print histos['data'].GetEntries()
			print histos['data'].GetBinLowEdge(1)
			print histos['data'].GetBinContent(1)
			print histos['data'].GetBinError(1)
			histos['sigma'] = sigma_histo
			processes = ['data', 'sigma', 'stat']
			print profile
			self.save_histo2table(histos = histos, processes = processes, path = '%s%s.dat' % (self.output_path, self.name), var = self.variable, bin_width = False)
			# sigma spectra
			histos = self.add_statistics(sigma_histo)
			processes = ['data', 'stat']
			self.save_histo2table(histos = histos, processes = processes, path = '%s%s.dat' % (self.output_path, 'StripPedestalSigma_Dia'), var = 'StripPedestalSigma', bin_width = False)
			return
		if self.histo_type == 'PulseHeight_ClusterSize' or self.histo_type == 'Eta_Dia_Area' :
			processes = ['data', 'stat']
			slice_name = self.histo_type
			if self.histo_type == 'Eta_Dia_Area' :
				slice_name = 'Eta_Area'
			slices = self.get_histoSlices(histo, path = self.output_path, slice_name = slice_name)
			for projection in slices :
				if '-' in projection :
					self.nstrips = int(projection.lstrip('1-'))
				else :
					self.nstrips = int(projection)
				print self.nstrips
				histos = self.add_statistics(slices[projection], slices[projection].GetName())
				self.save_histo2table(histos = histos, processes = processes, path = '%s%s.dat' % (self.output_path, slices[projection].GetName()), var = self.variable, bin_width = False)
			return
		canvas.Update()
#		ROOT.gPad.Update()
#		canvas.Dump()
#		raw_input('ok?')
		processes = ['data',]
		if self.return_value == 'mean' :
			histos = self.add_statistics(histo)
			processes.append('stat')
		elif self.return_value == 'sigma' :
			fit = histo.GetListOfFunctions().FindObject('histofitx')
			histos = self.add_statistics(histo, fit = fit)
			processes.append('stat')
			mean  = (fit.GetParameter(1), fit.GetParError(1))
			sigma = (fit.GetParameter(2), fit.GetParError(2))
			print 'Mean : %s +- %s' % rn.get_roundedNumber(*mean )
			print 'Sigma: %s +- %s' % rn.get_roundedNumber(*sigma)
			entries = []
			entries.append(('$\\chi^2 / \\ndof$', '{%.0f / %d}' % (fit.GetChisquare(), fit.GetNDF())))
			entries.append(('Mean' , '%s +- %s' % rn.get_roundedNumber(fit.GetParameter(1), fit.GetParError(1))))
			entries.append(('Sigma', '%s +- %s' % rn.get_roundedNumber(fit.GetParameter(2), fit.GetParError(2))))
			print entries
#			ROOT.gStyle.SetOptFit(0)
#			self.draw_statbox(entries)
		if self.histo_type != 'FidCut' and self.histo_type != 'TrackPos' :
			self.save_histo2table(histos = histos, processes = processes, path = '%s%s.dat' % (self.output_path, self.name), var = self.variable, bin_width = False)
		canvas.Print('%s%s.pdf' % (self.output_path, self.name))
		canvas.Print('%s%s.tex' % (self.output_path, self.name))
		return -1.
	def plot(self) :
		rd42Style()
		if self.histo_type == 'PulseHeight' :
			ROOT.gStyle.SetOptStat('m')
		if self.histo_type == 'Noise' :
			ROOT.gStyle.SetOptFit(01111)
#		ROOT.gStyle.SetDrawOption('colz')
#		ROOT.gStyle.SetCanvasDefW(1200)
		canvas = ROOT.TCanvas('%s_%s' % (self.histo_name, self.rand.Integer(10000)), 'canvas')
		histo = self.get_histo()
		histos = {}
		histos['data'] = histo
		canvas.cd()
		histo.Draw(self.draw_opt)
		histo.GetXaxis().SetTitle(self.xTitle)
		histo.GetYaxis().SetTitle(self.yTitle)
		self.draw_rd42Line()
		canvas.UseCurrentStyle()
		if histo.GetListOfFunctions().FindObject('palette') == None :
			histo.SetMaximum(1.4 * histo.GetMaximum())
		else :
#			canvas.SetWindowSize(1200, 600)
			canvas.SetRightMargin(canvas.GetRightMargin() + 0.08)
			pal = histo.GetListOfFunctions().FindObject('palette')
			pal_offset = 0.012
			pal.SetX1NDC(1. - canvas.GetRightMargin() + pal_offset)
			pal.SetX2NDC(1. - canvas.GetRightMargin() + pal_offset + 0.82*histo.GetZaxis().GetTickLength())
			pal.SetY1NDC(canvas.GetBottomMargin())
			pal.SetY2NDC(1. - canvas.GetTopMargin())
		if self.histo_type == 'FidCut' :
			self.save_TH2histo2table(histo, path = '%s%s.dat' % (self.output_path, self.histo_name))
			fid_cut = self.get_fidCut()
			fid_cut.SetLineColor(ROOT.kRed)
#			fid_cut.Dump()
			canvas.cd()
			fid_cut.Draw('same')
		canvas.Update()
#		ROOT.gPad.Update()
#		canvas.Dump()
#		raw_input('ok?')
		processes = ['data',]
		if self.return_value == 'mean' :
			mean = histo.GetMean()
			print 'Mean: %f' % mean
		elif self.return_value == 'sigma' :
			fit = histo.GetListOfFunctions().FindObject('histofitx')
			histos['fit'] = ROOT.TH1F('%s_fit' % self.histo_type, 'fit', histo.GetNbinsX(), 0., 1.)
			for i in [0, 1, 2] :
				histos['fit'].SetBinContent(i+1, fit.GetParameter(i))
				histos['fit'].SetBinError  (i+1  , fit.GetParError(i))
			processes.append('fit')
			mean  = (fit.GetParameter(1), fit.GetParError(1))
			sigma = (fit.GetParameter(2), fit.GetParError(2))
			print 'Mean : %s +- %s' % rn.get_roundedNumber(*mean )
			print 'Sigma: %s +- %s' % rn.get_roundedNumber(*sigma)
			entries = []
			entries.append(('$\\chi^2 / \\ndof$', '{%.0f / %d}' % (fit.GetChisquare(), fit.GetNDF())))
			entries.append(('Mean' , '%s +- %s' % rn.get_roundedNumber(fit.GetParameter(1), fit.GetParError(1))))
			entries.append(('Sigma', '%s +- %s' % rn.get_roundedNumber(fit.GetParameter(2), fit.GetParError(2))))
			print entries
#			ROOT.gStyle.SetOptFit(0)
#			self.draw_statbox(entries)
		if self.histo_type != 'FidCut' :
			self.save_histo2table(histos = histos, processes = processes, path = '%s%s.dat' % (self.output_path, self.histo_name), var = self.histo_type, bin_width = False)
		canvas.Print('%s%s.pdf' % (self.output_path, self.histo_name))
		canvas.Print('%s%s.tex' % (self.output_path, self.histo_name))
		return -1.