<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Andre Holzner&#039;s Blog</title>
	<atom:link href="http://aholzner.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://aholzner.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Sun, 29 Jan 2012 20:55:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='aholzner.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Andre Holzner&#039;s Blog</title>
		<link>http://aholzner.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://aholzner.wordpress.com/osd.xml" title="Andre Holzner&#039;s Blog" />
	<atom:link rel='hub' href='http://aholzner.wordpress.com/?pushpress=hub'/>
		<item>
		<title>A list of follow-ups on the recent CNGS/Opera result on the neutrino velocity</title>
		<link>http://aholzner.wordpress.com/2011/09/28/a-list-of-follow-ups-on-the-recent-cngsopera-result-on-the-neutrino-velocity/</link>
		<comments>http://aholzner.wordpress.com/2011/09/28/a-list-of-follow-ups-on-the-recent-cngsopera-result-on-the-neutrino-velocity/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 19:41:59 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arXiv]]></category>
		<category><![CDATA[CNGS]]></category>
		<category><![CDATA[hep-ex]]></category>
		<category><![CDATA[neutrino]]></category>
		<category><![CDATA[OPERA]]></category>
		<category><![CDATA[preprint]]></category>
		<category><![CDATA[velocity]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=624</guid>
		<description><![CDATA[As found on hep-ex on arXiv (no guarantee that this list is exhaustive, last updated 2011-10-06): arXiv:1110.1162: Probing neutrino masses with neutrino-speed experiments arXiv:1110.0821: Testing the OPERA Superluminal Neutrino Anomaly at the LHC arXiv:1110.0736: Resolving 7 problems with OPERA&#8217;s superluminal neutrino experiment arXiv:1110.0644: A classical model explaining the OPERA velocity paradox arXiv:1110.0595: Is there a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=624&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As found on hep-ex on arXiv (no guarantee that this list is exhaustive, last updated 2011-10-06):</p>
<ul>
<li><a href="http://arxiv.org/abs/1110.1162">arXiv:1110.1162</a>: Probing neutrino masses with neutrino-speed experiments</li>
<li><a href="http://arxiv.org/abs/1110.0821">arXiv:1110.0821</a>: Testing the OPERA Superluminal Neutrino Anomaly at the LHC</li>
<li><a href="http://arxiv.org/abs/1110.0736">arXiv:1110.0736</a>: Resolving 7 problems with OPERA&#8217;s superluminal neutrino experiment</li>
<li><a href="http://arxiv.org/abs/1110.0644">arXiv:1110.0644</a>: A classical model explaining the OPERA velocity paradox</li>
<li><a href="http://arxiv.org/abs/1110.0595">arXiv:1110.0595</a>: Is there a neutrino speed anomaly?</li>
<li><a href="http://arxiv.org/abs/1109.6667">arXiv:1109.6667</a>: Constraints and tests of the OPERA superluminal neutrinos</li>
<li><a href="http://arxiv.org/abs/1110.0424">arXiv:1110.0424</a>: How large is the fraction of superluminal neutrinos at OPERA?</li>
<li><a href="http://arxiv.org/abs/1110.0351">arXiv:1110.0351</a>: OPERA, SN1987a and energy dependence of superluminal neutrino velocity</li>
<li><a href="http://arxiv.org/abs/1110.0243">arXiv:1110.0243</a>: Using an Einstein&#8217;s idea to explain OPERA faster than light neutrinos. </li>
<li><a href="http://arxiv.org/abs/1110.0241">arXiv:1110.0241</a>: Superluminal Neutrinos at OPERA Confront Pion Decay Kinematics</li>
<li><a href="http://arxiv.org/abs/1110.0239">arXiv:1110.0239</a>: A simple explanation of OPERA results without strange physics</li>
<li><a href="http://arxiv.org/abs/1109.6631">arXiv:1109.6631</a>: Superluminal Neutrinos and a Curious Phenomenon in the Relativistic Quantum Hamilton-Jacobi Equation</li>
<li><a href="http://arxiv.org/abs/1109.6624">arXiv:1109.6624</a>: Superluminal neutrino and spontaneous breaking of Lorentz invariance</li>
<li><a href="http://arxiv.org/abs/1109.6562">arXiv:1109.6562</a>: New Constraints on Neutrino Velocities</li>
<li><a href="http://arxiv.org/abs/1109.6354">arXiv:1109.6354</a>: Neutrino Shortcuts in Spacetime</li>
<li><a href="http://arxiv.org/abs/1109.6296">arXiv:1109.6296</a>: On the Possibility of Superluminal Neutrino Propagation</li>
<li><a href="http://arxiv.org/abs/1109.6238">arXiv:1109.6238</a>: Comparison of muon and neutrino times from decays of mesons in the atmosphere</li>
<li><a href="http://arxiv.org/abs/1109.6097">arXiv:1109.6097</a>: Neutrino speed anomaly as a signal of Lorentz violation</li>
<li><a href="http://arxiv.org/abs/1109.5924">arXiv:1109.5924</a>: Mass-dependent Lorentz Violation and Neutrino Velocity</li>
<li><a href="http://arxiv.org/abs/1109.5749">arXiv:1109.5749</a>: Superluminal neutrinos at the OPERA?</li>
<li><a href="http://arxiv.org/abs/1109.5727">arXiv:1109.5727</a>: A possible statistical mechanism of anomalous neutrino velocity in OPERA experiment?</li>
<li><a href="http://arxiv.org/abs/1109.5721">arXiv:1109.5721</a>: A comment on the OPERA result and CPT</li>
<li><a href="http://arxiv.org/abs/1109.5671">arXiv:1109.5671</a>: OPERA&#8217;s superluminal muon-neutrino velocity and an FPS-type model of Lorentz violation</li>
<li><a href="http://arxiv.org/abs/1109.5599">arXiv:1109.5599</a>: Comments on the recent velocity measurement of the muon neutrinos by the OPERA Collaboration</li>
<li><a href="http://arxiv.org/abs/1109.5368">arXiv:1109.5368</a>: Inconsistence of super-luminal Opera neutrino speed with SN1987A neutrinos burst and with flavor neutrino mixing</li>
<li><a href="http://arxiv.org/abs/1109.4980">arXiv:1109.4980</a>: Superluminal neutrinos in long baseline experiments and SN1987a</li>
</ul>
<p>Here is the original OPERA preprint: <a href="http://arxiv.org/abs/1109.4897">arXiv:1109.4897</a>: Measurement of the neutrino velocity with the OPERA detector in the CNGS beam </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/624/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/624/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/624/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/624/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/624/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/624/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/624/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/624/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=624&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2011/09/28/a-list-of-follow-ups-on-the-recent-cngsopera-result-on-the-neutrino-velocity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>A TMVA example in pyROOT</title>
		<link>http://aholzner.wordpress.com/2011/08/27/a-tmva-example-in-pyroot/</link>
		<comments>http://aholzner.wordpress.com/2011/08/27/a-tmva-example-in-pyroot/#comments</comments>
		<pubDate>Sat, 27 Aug 2011 07:53:00 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[BDT]]></category>
		<category><![CDATA[boosted decision tree]]></category>
		<category><![CDATA[multivariate analysis]]></category>
		<category><![CDATA[PyROOT]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[tmva]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=542</guid>
		<description><![CDATA[This is an example showing how to use TMVA using python/pyROOT. TMVA is a toolkit for multivariate analysis in ROOT which is widely used in High Energy Physics data analysis. The example code in this post is made available under the Apache License Version 2.0. If you want to experiment with the code examples shown, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=542&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">This is an example showing how to use <a href="http://tmva.sourceforge.net/" target="_blank">TMVA</a> using python/pyROOT. TMVA is a toolkit for multivariate analysis in <a href="http://root.cern.ch" target="_blank">ROOT</a> which is widely used in High Energy Physics data analysis.</p>
<p align="justify">The example code in this post is made available under the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache License Version 2.0</a>. If you want to experiment with the code examples shown, it&#8217;s best to do so in a newly created directory. It has been tested with ROOT 5.30/00 which includes TMVA 4.1.2 . Copying and pasting of the example code below directly into the interactive python shell is <strong>strongly discouraged</strong> as crashes of the python interpreter were observed when doing this. Also, the indentation is not taken into account correctly, leading to syntax errors. Copying the code into a python file and then running this file with <tt>python -i </tt> however should work.</p>
<h2>Generating the samples</h2>
<p align="justify">For simplicity and to allow for easy visualization, we will work in two dimensions. First, we&#8217;ll generate two samples (&#8216;signal&#8217; and &#8216;background&#8217;) drawn from Gaussian distributions with different means and fill them into a TNtuple:</p>
<p><pre class="brush: python;">
import ROOT

# create a TNtuple
ntuple = ROOT.TNtuple(&quot;ntuple&quot;,&quot;ntuple&quot;,&quot;x:y:signal&quot;)

# generate 'signal' and 'background' distributions
for i in range(10000):
    # throw a signal event centered at (1,1)
    ntuple.Fill(ROOT.gRandom.Gaus(1,1), # x
                ROOT.gRandom.Gaus(1,1), # y
                1)                      # signal
    
    # throw a background event centered at (-1,-1)
    ntuple.Fill(ROOT.gRandom.Gaus(-1,1), # x
                ROOT.gRandom.Gaus(-1,1), # y
                0)                       # background
</pre></p>
<p align="justify">In order to visualize the generated distributions, we can do the following:</p>
<p><pre class="brush: python;">
# keeps objects otherwise removed by garbage collected in a list
gcSaver = []

# create a new TCanvas
gcSaver.append(ROOT.TCanvas())

# draw an empty 2D histogram for the axes
histo = ROOT.TH2F(&quot;histo&quot;,&quot;&quot;,1,-5,5,1,-5,5)
histo.Draw()

# draw the signal events in red
ntuple.SetMarkerColor(ROOT.kRed)
ntuple.Draw(&quot;y:x&quot;,&quot;signal &gt; 0.5&quot;,&quot;same&quot;)

# draw the background events in blue
ntuple.SetMarkerColor(ROOT.kBlue)
ntuple.Draw(&quot;y:x&quot;,&quot;signal &lt;= 0.5&quot;,&quot;same&quot;)
</pre></p>
<p align="justify">In my case, the resulting plot looked like this:</p>
<p align="center">
<a href="http://aholzner.files.wordpress.com/2011/08/sig-bg-events.png"><img src="http://aholzner.files.wordpress.com/2011/08/sig-bg-events.png?w=544&#038;h=368" alt="" title="sig-bg-events" width="544" height="368" class="aligncenter size-full wp-image-558" /></a></p>
<p align="justify">where the red points correspond to &#8216;signal&#8217; and the blue points correspond to &#8216;background&#8217;. We can start training a classifier which attempts to label individual points as &#8216;signal&#8217; or &#8216;background&#8217; based on the value of their coordinates x and y.</p>
<p align="justify">The following code is inspired by the standard TMVA classification example (<a href="http://root.cern.ch/viewvc/tags/v5-30-00/tmva/test/TMVAClassification.C?view=markup" target="_blank">$ROOTSYS/tmva/test/TMVAClassification.C</a>). First, we&#8217;ll initialize TMVA and create a factory object:</p>
<p><pre class="brush: python;">
ROOT.TMVA.Tools.Instance()

# note that it seems to be mandatory to have an
# output file, just passing None to TMVA::Factory(..)
# does not work. Make sure you don't overwrite an
# existing file.
fout = ROOT.TFile(&quot;test.root&quot;,&quot;RECREATE&quot;)

factory = ROOT.TMVA.Factory(&quot;TMVAClassification&quot;, fout,
                            &quot;:&quot;.join([
                                &quot;!V&quot;,
                                &quot;!Silent&quot;,
                                &quot;Color&quot;,
                                &quot;DrawProgressBar&quot;,
                                &quot;Transformations=I;D;P;G,D&quot;,
                                &quot;AnalysisType=Classification&quot;]
                                     ))
</pre></p>
<p align="justify">The parameters given to the constructor of <tt>ROOT.TMVA.Factory</tt> are described in section 3.1 of the current <a href="http://tmva.sourceforge.net/docu/TMVAUsersGuide.pdf" target="_blank">TMVA user&#8217;s guide</a>. Verbosity is disabled with the <tt>!V</tt> (&#8216;not verbose&#8217;) option while <tt>!Silent</tt> (&#8216;not silent&#8217;) still allows for some level of reporting. <tt>Color</tt> enables the use of color and <tt>DrawProgressBar</tt> enables displaying the progress of training. Currently, some of these options actually correspond to the default setting so need not necessarily be specified. The values given to the <tt>Transformations</tt> parameter only affect testing and visualization, not the training (according to the manual).</p>
<p align="justify">Now we declare which variables should be used for classification (the &#8216;independent&#8217; variables) and add the tree for signal and background events (which are stored in the same tree in this example, a cut on the variable <tt>signal</tt> is used to distinguish between signal and background events):</p>
<p><pre class="brush: python;">
factory.AddVariable(&quot;x&quot;,&quot;F&quot;)
factory.AddVariable(&quot;y&quot;,&quot;F&quot;) 

factory.AddSignalTree(ntuple)
factory.AddBackgroundTree(ntuple)

# cuts defining the signal and background sample
sigCut = ROOT.TCut(&quot;signal &gt; 0.5&quot;)
bgCut = ROOT.TCut(&quot;signal &lt;= 0.5&quot;)

factory.PrepareTrainingAndTestTree(sigCut,   # signal events
                                   bgCut,    # background events
                                   &quot;:&quot;.join([
                                        &quot;nTrain_Signal=0&quot;,
                                        &quot;nTrain_Background=0&quot;,
                                        &quot;SplitMode=Random&quot;,
                                        &quot;NormMode=NumEvents&quot;,
                                        &quot;!V&quot;
                                       ]))

</pre></p>
<p align="justify">The parameters given to <tt>PrepareTrainingAndTestTree</tt> are described in section 3.1.4 of the current <a href="http://tmva.sourceforge.net/docu/TMVAUsersGuide.pdf" target="_blank">TMVA user&#8217;s guide</a>. Essentially, the available events are separated randomly into two equally large sets for training and testing and verbosity is disabled.</p>
<p align="justify">We then configure a classifier to learn to distinguish the two samples generated above. As an example, we use a boosted decision tree:</p>
<p><pre class="brush: python;">
method = factory.BookMethod(ROOT.TMVA.Types.kBDT, &quot;BDT&quot;,
                   &quot;:&quot;.join([
                       &quot;!H&quot;,
                       &quot;!V&quot;,
                       &quot;NTrees=850&quot;,
                       &quot;nEventsMin=150&quot;,
                       &quot;MaxDepth=3&quot;,
                       &quot;BoostType=AdaBoost&quot;,
                       &quot;AdaBoostBeta=0.5&quot;,
                       &quot;SeparationType=GiniIndex&quot;,
                       &quot;nCuts=20&quot;,
                       &quot;PruneMethod=NoPruning&quot;,
                       ]))

factory.TrainAllMethods()
factory.TestAllMethods()
factory.EvaluateAllMethods()
</pre></p>
<p align="justify">The options (described in section 8.12.2 of the current <a href="http://tmva.sourceforge.net/docu/TMVAUsersGuide.pdf" target="_blank">TMVA user&#8217;s guide</a>) can be summarized as follows:</p>
<ul>
<li>the help text should not be printed</li>
<li>verbosity is disabled</li>
<li>850 trees should be used</li>
<li>leaf nodes must contain at least 150 events</li>
<li>the depth of the trees is limited to 3</li>
<li><a target="_blank" href="http://en.wikipedia.org/wiki/AdaBoost">adaptive boosting</a> should be used</li>
<li>the <a target="_blank" href="http://en.wikipedia.org/wiki/Gini_coefficient">Gini index</a> should be used to select the best variable to be used in each tree node</li>
<li>20 steps should be used when scanning cuts on a variable and</li>
<li>no pruning of trees after they have been constructed should be applied.</li>
</ul>
<hr />
<h2>Examining the trained classifier</h2>
<p align="justify">After running this, TMVA should have created a file <tt>weights/TMVAClassification_BDT.weights.xml</tt> which contains the structure of trained classifier. In order to evaluate the classification function at arbitrary coordinates, we create an instance of <tt>ROOT.TMVA.Reader</tt>:</p>
<p><pre class="brush: python;">
reader = ROOT.TMVA.Reader()
</pre></p>
<p align="justify">To calculate the value of the classifier for a given input coordinate (x,y), we first create two arrays (with one element each) for the variables x and y such that later on we can take (C++) references of the first element which we pass to the reader (see also <a target="_blank" href="http://root.cern.ch/phpBB3/viewtopic.php?f=14&amp;t=12289#p53106">this discussion</a> on the ROOT bulletin board):</p>
<p><pre class="brush: python;">
import array
varx = array.array('f',[0]) ; reader.AddVariable(&quot;x&quot;,varx)
vary = array.array('f',[0]) ; reader.AddVariable(&quot;y&quot;,vary)
</pre></p>
<p align="justify">Now that we have given the reader the variables x and y, we can read the weights file:</p>
<p><pre class="brush: python;">
reader.BookMVA(&quot;BDT&quot;,&quot;weights/TMVAClassification_BDT.weights.xml&quot;)
</pre></p>
<p align="justify">In order to plot the BDT output as function of x and y, we can use the following code snippet:</p>
<p><pre class="brush: python;">
# create a new 2D histogram with fine binning
histo2 = ROOT.TH2F(&quot;histo2&quot;,&quot;&quot;,200,-5,5,200,-5,5)

# loop over the bins of a 2D histogram
for i in range(1,histo2.GetNbinsX() + 1):
    for j in range(1,histo2.GetNbinsY() + 1):
        
        # find the bin center coordinates
        varx[0] = histo2.GetXaxis().GetBinCenter(i)
        vary[0] = histo2.GetYaxis().GetBinCenter(j)
        
        # calculate the value of the classifier
        # function at the given coordinate
        bdtOutput = reader.EvaluateMVA(&quot;BDT&quot;)
        
        # set the bin content equal to the classifier output
        histo2.SetBinContent(i,j,bdtOutput)

gcSaver.append(ROOT.TCanvas())
histo2.Draw(&quot;colz&quot;)

# draw sigma contours around means
for mean, color in (
    ((1,1), ROOT.kRed), # signal
    ((-1,-1), ROOT.kBlue), # background
    ):
    
    # draw contours at 1 and 2 sigmas
    for numSigmas in (1,2):
        circle = ROOT.TEllipse(mean[0], mean[1], numSigmas)
        circle.SetFillStyle(0)
        circle.SetLineColor(color)
        circle.SetLineWidth(2)
        circle.Draw()
        gcSaver.append(circle)

ROOT.gPad.Modified()
</pre></p>
<p align="justify">The output should look like this:</p>
<p><a href="http://aholzner.files.wordpress.com/2011/08/classifier-output.png"><img src="http://aholzner.files.wordpress.com/2011/08/classifier-output.png?w=544&#038;h=368" alt="" title="classifier-output" width="544" height="368" class="aligncenter size-full wp-image-562" /></a></p>
<p align="justify">The overlaid ellipses are the one and two sigma contours around the mean signal and background. Note that these are ellipses because the x and y axes have a different scale. One can nicely see how the classifier approximates the Gaussian distributions with a set of trees representing rectangular regions.</p>
<p align="justify">Typically, one also wants to look at the distribution of the classifier output for signal and background, e.g. to get an idea how well the two samples can be separated (although the <a target="_blank" href="http://en.wikipedia.org/wiki/Receiver_operating_characteristic">ROC curve</a> is more suitable for comparing the performance of different classifiers). TMVA fills a tree for the events in the test sample which we can use for this purpose:</p>
<p><pre class="brush: python;">
# fill histograms for signal and background from the test sample tree
ROOT.TestTree.Draw(&quot;BDT&gt;&gt;hSig(22,-1.1,1.1)&quot;,&quot;classID == 0&quot;,&quot;goff&quot;)  # signal
ROOT.TestTree.Draw(&quot;BDT&gt;&gt;hBg(22,-1.1,1.1)&quot;,&quot;classID == 1&quot;, &quot;goff&quot;)  # background

ROOT.hSig.SetLineColor(ROOT.kRed); ROOT.hSig.SetLineWidth(2)  # signal histogram
ROOT.hBg.SetLineColor(ROOT.kBlue); ROOT.hBg.SetLineWidth(2)   # background histogram

# use a THStack to show both histograms
hs = ROOT.THStack(&quot;hs&quot;,&quot;&quot;)
hs.Add(ROOT.hSig)
hs.Add(ROOT.hBg)

# show the histograms
gcSaver.append(ROOT.TCanvas())
hs.Draw()
</pre></p>
<p align="justify">which for me produced the following output:</p>
<p><a href="http://aholzner.files.wordpress.com/2011/08/sig-bg-histos.png"><img src="http://aholzner.files.wordpress.com/2011/08/sig-bg-histos.png?w=544&#038;h=368" alt="" title="sig-bg-histos" width="544" height="368" class="aligncenter size-full wp-image-570" /></a></p>
<p>Enjoy !</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/542/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=542&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2011/08/27/a-tmva-example-in-pyroot/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>

		<media:content url="http://aholzner.files.wordpress.com/2011/08/sig-bg-events.png" medium="image">
			<media:title type="html">sig-bg-events</media:title>
		</media:content>

		<media:content url="http://aholzner.files.wordpress.com/2011/08/classifier-output.png" medium="image">
			<media:title type="html">classifier-output</media:title>
		</media:content>

		<media:content url="http://aholzner.files.wordpress.com/2011/08/sig-bg-histos.png" medium="image">
			<media:title type="html">sig-bg-histos</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging unresponsive Java GUI applications</title>
		<link>http://aholzner.wordpress.com/2011/06/25/debugging-unresponsive-java-gui-applications/</link>
		<comments>http://aholzner.wordpress.com/2011/06/25/debugging-unresponsive-java-gui-applications/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 11:53:09 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdb]]></category>
		<category><![CDATA[locking]]></category>
		<category><![CDATA[multithreaded]]></category>
		<category><![CDATA[swing]]></category>
		<category><![CDATA[SwingWorker]]></category>
		<category><![CDATA[unresponsive]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=471</guid>
		<description><![CDATA[You probably heard about it: one should not run time-consuming operations on the event dispatch thread in Java GUI applications but use SwingWorker instead to run lengthy tasks, possibly showing a dialog box once the task is completed. One typical case of running tasks on the event dispatch thread is in a callback to the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=471&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">You probably heard about it: one should not run time-consuming operations on the event dispatch thread in Java GUI applications but use <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html">SwingWorker</a> instead to run lengthy tasks, possibly showing a dialog box once the task is completed. One typical case of running tasks on the event dispatch thread is in a callback to the user&#8217;s action such as pressing a button or selecting a menu item from a pop-up menu.</p>
<p style="text-align:justify;">I&#8217;m working on an application which runs external commands very often and the running time of such commands can be several minutes. Wherever possible, commands are run only on demand and the states of the objects in the application are updated according to the command&#8217;s output. While care was taken to use <tt>SwingWorker</tt> in obvious places the application still became unresponsive (i.e. the windows were not repainted for minutes) because the event dispatching thread was waiting to lock an object which was locked by another thread running a lengthy task (an external command in this case). In the following I describe how I found such locks with the standard Java debugger.</p>
<h3 style="text-align:justify;">Starting the program and the debugger</h3>
<p>The application I was working on was packaged into a single .jar file. So instead of starting the program with</p>
<pre>java -jar myapp.jar</pre>
<p>I started the Java virtual machine and my application and made them listen to connections from a debugger:</p>
<pre>java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=localhost:10101 -jar myapp.jar</pre>
<p style="text-align:justify;">This instructs the Java virtual machine to bind to socket 10101 on localhost to listen to a connection from <tt>jdb</tt>. The <tt>suspend=n</tt> part instructs it to start program execution immediately.</p>
<p style="text-align:justify;"><strong>WARNING:</strong> as far as I understood is there no authentication involved when connecting with <tt>jdb</tt> (at least not when running it as described above). In other words: all users having access to this machine can also connect to the process you are debugging and control it !</p>
<p>On a second console, I started the Java debugger:</p>
<pre>jdb -attach localhost:10101</pre>
<p>The command <tt>help</tt> on the debugger console will give a short list of all commands understood by <tt>jdb</tt>.</p>
<h3>Finding the locks</h3>
<p>I waited until the GUI froze. At that point, I did:</p>
<pre>suspend</pre>
<p>to suspend all threads. I got a quick overview of the state of all threads by using the command <tt>threads</tt>:</p>
<pre>Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x832 Reference Handler           cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x831  Finalizer                   cond. waiting
  (java.lang.Thread)0x830                         Signal Dispatcher           running
  (java.lang.Thread)0x82f                         Java2D Disposer             cond. waiting
  (java.lang.Thread)0x901                         TimerQueue                  cond. waiting
Group main:
  (java.lang.Thread)0x82e                         AWT-XAWT                    running
  (java.lang.Thread)0x82d                         AWT-Shutdown                cond. waiting
  (java.awt.EventDispatchThread)0x82c             AWT-EventQueue-0            waiting in a monitor
  (java.lang.Thread)0x82b                         pool-2-thread-1             cond. waiting
  (java.lang.Thread)0x82a                         pool-1-thread-1             cond. waiting
  (java.lang.Thread)0x829                         pool-1-thread-2             cond. waiting
  (java.lang.Thread)0x828                         pool-1-thread-3             cond. waiting
  (java.lang.Thread)0x827                         pool-1-thread-4             cond. waiting
  (java.lang.Thread)0x826                         pool-1-thread-5             cond. waiting
  (java.lang.Thread)0x825                         pool-1-thread-6             cond. waiting
  (java.lang.Thread)0x824                         pool-1-thread-7             cond. waiting
  (java.lang.Thread)0x823                         pool-1-thread-8             cond. waiting
  (java.lang.Thread)0x8db                         DestroyJavaVM               running
  (java.lang.Thread)0x9cd                         SwingWorker-pool-3-thread-1 cond. waiting
  (java.lang.Thread)0x9d6                         SwingWorker-pool-3-thread-2 waiting in a monitor
  (java.lang.Thread)0x9d8                         SwingWorker-pool-3-thread-3 cond. waiting
  (java.lang.UNIXProcess$1$1)0x9d9                process reaper              running
  (java.lang.Thread)0x9da                         SwingWorker-pool-3-thread-4 waiting in a monitor
  (java.lang.Thread)0x9db                         SwingWorker-pool-3-thread-5 waiting in a monitor
  (java.lang.Thread)0x9dc                         SwingWorker-pool-3-thread-6 waiting in a monitor
  (java.lang.Thread)0x9dd                         SwingWorker-pool-3-thread-7 cond. waiting
  (java.lang.UNIXProcess$1$1)0x9de                process reaper              running
  (java.lang.Thread)0x9df                         SwingWorker-pool-3-thread-8 cond. waiting
  (java.lang.UNIXProcess$1$1)0x9e0                process reaper              running
  (java.lang.UNIXProcess$1$1)0x9e1                process reaper              running</pre>
<p>The important thing to note is the line:</p>
<pre>  (java.awt.EventDispatchThread)0x82c             AWT-EventQueue-0            waiting in a monitor</pre>
<p style="text-align:justify;">Which means that the event dispatch thread is actually waiting to obtain a lock on an object &#8212; but which one ? First of all, I wanted to know where (in the source code) the thread was waiting. I could print the stack trace of this thread by doing:</p>
<pre>where 0x82c</pre>
<p>(0x82c is the thread id obtained from the above output). This gave me something like:</p>
<pre>[1] MyTableModel.getValueAt (MyTableModel.java:136)
[2] javax.swing.JTable.getValueAt (JTable.java:2,686)
[3] javax.swing.JTable.prepareRenderer (JTable.java:5,703)
...</pre>
<p style="text-align:justify;">So indeed, the thread was stuck somewhere in my code. Looking at the corresponding line of source code, I had to go up a few lines to find the variable (the field <tt>myList</tt> of the class <tt>MyTableModel</tt>) in the <tt>synchronized</tt> statement which I was locking on.</p>
<p style="text-align:justify;">However, I needed also to find out which other thread currently holds the lock on this variable in order to solve the problem. I could do this by doing running the following command in <tt>jdb</tt>:</p>
<pre>lock this.myList</pre>
<p style="text-align:justify;">(where the <tt>jdb</tt> prompt showed me that the current thread was AWT-EventQueue0 because of the previous commands and the current stack frame was 1, so no need to set the current thread or stack frame). The output of the above command was:</p>
<pre>com.sun.tools.example.debug.expr.ParseException: Unable to complete expression. Thread not suspended for method invoke
Owned by: SwingWorker-pool-3-thread-1, entry count: 1
Waiting thread: SwingWorker-pool-3-thread-2
Waiting thread: AWT-EventQueue-0</pre>
<p>again, from the output of the <tt>threads</tt> command (see above) I could find the thread id of the thread <tt>SwingWorker-pool-3-thread-1</tt> which owned the lock on <tt>myList</tt>. A quick</p>
<pre>where 0x9cd</pre>
<p style="text-align:justify;">revealed that indeed the lock on <tt>myList</tt> was held by a thread which was waiting for an external command to complete:</p>
<pre>[1] java.lang.Object.wait (native method)
[2] java.lang.Object.wait (Object.java:485)
[3] java.lang.UNIXProcess.waitFor (UNIXProcess.java:165)
[4] Utils.runCommand (Utils.java:102)
...</pre>
<p>and I could also easily find the <tt>synchronized</tt> statement which acquired the lock on <tt>myList</tt>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/471/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=471&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2011/06/25/debugging-unresponsive-java-gui-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>Facebook Hacker Cup 2011 Round 2 Problem 3 – some discussion</title>
		<link>http://aholzner.wordpress.com/2011/02/08/facebook-hacker-cup-round-2-problem-3-%e2%80%93-some-discussion/</link>
		<comments>http://aholzner.wordpress.com/2011/02/08/facebook-hacker-cup-round-2-problem-3-%e2%80%93-some-discussion/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 21:21:00 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[bonus assignments]]></category>
		<category><![CDATA[combinatorics]]></category>
		<category><![CDATA[coprime]]></category>
		<category><![CDATA[double counting]]></category>
		<category><![CDATA[exponentiation by squaring]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hacker cup]]></category>
		<category><![CDATA[inclusion-exclusion principle]]></category>
		<category><![CDATA[Möbius function]]></category>
		<category><![CDATA[prime number]]></category>
		<category><![CDATA[set union]]></category>
		<category><![CDATA[sieve of Eratosthenes]]></category>
		<category><![CDATA[square-free integer]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=303</guid>
		<description><![CDATA[Facebook&#8217;s Hacker Cup online round 2 took place some time ago. This article discusses solutions to problem 3 &#8216;Bonus assignments&#8217; (the original problem statement can be found by going to  http://www.facebook.com/hackercup and following the corresponding links there). Essentially, one had to count the possibilities to choose N numbers a1&#8230;aN (where N can range from one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=303&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Facebook&#8217;s Hacker Cup online round 2 took place some time ago. This article discusses solutions to problem 3 &#8216;Bonus assignments&#8217; (the original problem statement can be found by going to  <a href="http://www.facebook.com/hackercup">http://www.facebook.com/hackercup</a> and following the corresponding links there). Essentially, one had to count the possibilities to choose N numbers a<sub>1</sub>&#8230;a<sub>N</sub> (where N can range from one to one million) such that:</p>
<ol>
<li>the smallest of these numbers must be between two given limits A and B while the largest number must be in the range C to D. Obviously, the other N-2 numbers must be between the smallest and largest number. Note that each of these boundaries A,B,C,D may be as large as one million.</li>
<li>at least one of the N numbers must have a non-zero remainder when divided by an unknown integer number P which is greater than one.</li>
</ol>
<p style="text-align:justify;">The brute force approach would be as follows:</p>
<ul>
<li>generate all possible sets of N numbers between A and D
<ul>
<li>check if the set satisfies requirement 1</li>
<li>for each accepted set, loop over all possible values P and
<ul>
<li>check whether for each value P <em>at least one</em> a<sub>i</sub> fulfills condition 2. If yes count this set in.</li>
</ul>
</li>
</ul>
</li>
</ul>
<p style="text-align:justify;">The number of sets to generate and investigate is (D-A+1)<sup>N</sup>. For each set we must verify that the minimum element is in the range A..B and the maximum element is in the range C..D. Then, for each set, we have to scan over the number P we have to test the remainder of all N numbers after division by P, so the time complexity of the brute force approach is O((D-A+1)<sup>N</sup> * D * D). Assuming the most unfavorable values for the parameters we end up with 1&#8217;000&#8217;000<sup>1&#8217;000&#8217;002</sup> = 10<sup>6&#8217;000&#8217;012</sup> tests. Comparing this number to the <a href="http://en.wikipedia.org/wiki/Observable_universe#Matter_content">estimated number of atoms in the universe</a> we&#8217;d rather enumerate all atoms before trying this brute force approach. And we conclude that there must be a smarter way than generating all possible sets of numbers and test them. Nevertheless, it is instructive to implement the brute force solution to check (with the examples given along with the problem statement) if one has understood the problem.</p>
<hr />
<p style="text-align:justify;">A first simplification can be achieved by trying to get rid of the &#8216;asymmetric&#8217; constraints on the minimum and maximum elements. If we could calculate the number of bonus assignments by replacing condition 1 by a simpler constraint, e.g. just one common range A..D for all elements (i.e. the smallest element would not have to be smaller or equal to C) our problem would be simpler. Let&#8217;s call this number f(A,D).</p>
<p>We can visualize the pairs of minimum and maximum values of a tuple a<sub>1</sub>&#8230;a<sub>N</sub> as points in the leftmost triangle of the following figure:</p>
<p><a href="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-drawing-square2.png"></a><a href="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-drawing-square4.png"><img class="aligncenter size-large wp-image-386" style="display:block;margin-left:auto;margin-right:auto;" title="2011-02-fbhc-drawing-square" src="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-drawing-square4.png?w=939&#038;h=227" alt="" width="939" height="227" /></a></p>
<p style="text-align:justify;">The shaded area in the leftmost part of the figure corresponds to those tuples which have their smallest value (vertical axis) in the range A..B and their largest value (horizontal axis) in the range C..D. As indicated in the figure can we write this shaded area as sum and difference of triangular areas which are calculable with by function f, namely:</p>
<ul>
<li style="text-align:justify;">f(A,D), represented by the first triangle after the equal sign.</li>
<li>We have also counted those assignments for which the largest value is below C, we thus must subtract f(A,C-1), corresponding to the second triangle after the equal sign</li>
<li>similarly we also included those assignments for which the smallest value is above C, we thus must subtract f(B+1,D), the third triangle after the equal sign</li>
<li>we have subtracted the number those assignments which have the lowest value ≥ B+1 and the highest value ≤ C-1 twice, so we must add f(B+1,C-1) again (indicated by the rightmost triangle)</li>
</ul>
<p style="text-align:justify;">We have simplified the problem to finding all possible assignments of N values which are all in the range A to D.</p>
<hr />
<p style="text-align:justify;">In order to get closer to finding a solution, let&#8217;s first make another simplification and look at the case N = 1. Our task is to cross out all numbers in the range A..D which are integer multiples of any value P greater than 2 (and less than D). So one would start by crossing out all integer multiples of 2, then all integer multiples of 3 as shown in the following diagram:</p>
<p><a href="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-prime-multiples.png"><img class="aligncenter size-full wp-image-380" style="display:block;margin-left:auto;margin-right:auto;" title="2011-02-fbhc-prime-multiples" src="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-prime-multiples.png?w=544" alt=""   /></a></p>
<p style="text-align:justify;">In this diagram, the numbers being &#8216;crossed out&#8217; are those on the horizontal axis and the numbers whose multiples we cross out are shown on the vertical axis. Each time a number is &#8216;crossed out&#8217;, a circle is placed in the corresponding column. Prime numbers are shown in red on the vertical axis.</p>
<p style="text-align:justify;">We start by crossing out multiples of 2. Then we cross out those of 3. We notice that we don&#8217;t need to cross the multiples of 4 because it is a multiple of 2 and we have crossed out all multiples of 4 already when crossing out all multiples of 2. Indeed, the fact that 4 has been crossed out already when we arrive there tells us that all its multiples have been crossed out already. So as a first generalization, we could think of counting all integer multiples of prime numbers in the range A..D and add all counts together. Numbers like 4 are shown in gray in the figure.</p>
<p style="text-align:justify;">However, we also notice that when we crossed out all multiples of 2 and of 3 we also crossed out multiples of 6 in both cases ! This would mean that by counting the multiples of 2 and adding the number of multiples of 3 we would have <strong>double counted</strong> all multiples of 6. We thus must <strong>subtract</strong> the number of multiples of 6 in A..D. Such numbers are shown in blue in the figure.</p>
<p style="text-align:justify;">So we add another rule: for each pair of (distinct) primes p and q we must subtract the number of integer multiples of p*q in the range A..D. Note that we only consider products of <em>distinct</em> primes. For example multiples of p*p are a subset of the multiples of p but are <strong>not</strong> a subset of the multiples of q, i.e. not all of them are in the <strong>overlap</strong> of the multiples of p and the multiples of q. The multiples of p*p*q and p*q*q and p*p*q*q etc. on the other hand are already contained in the multiples of p*q.</p>
<p>How many times do we count 30 ? Let&#8217;s consider all numbers which divide 30:</p>
<ul>
<li>30 is a multiple of 2 (which is prime), so we counted it when counting multiples of 2</li>
<li>30 is a multiple of 3 (which is prime), so we counted it again when counting multiples of 3</li>
<li>30 is a multiple of 5 (which is prime), so we counted it again when counting multiples of 5</li>
<li>30 is a multiple of 6, so we have subtracted one from the count when counting multiples of 2 * 3</li>
<li>30 is a multiple of 10, so we have subtracted one from the count when counting 2 * 5</li>
<li>30 is a multiple of 15, so we have subtracted one from the count when counting 3 * 5</li>
</ul>
<p style="text-align:justify;">Which leaves us with zero at the end, indicating that 30 would <strong>not</strong> contribute to the number of numbers (in A..D) which are divided by any number P in the range 2..D. But this is not true, 30 is divided by several numbers as we just saw. We can fix this by adding another rule for multiples of three distinct primes (2,3 and 5 in this case) we must (again) add one to the count of a number (the reason for which 15 is shown in green).</p>
<p style="text-align:justify;">We start to see a pattern:</p>
<ul>
<li><strong>count</strong> the number of integer multiples in A..D of all <strong>prime numbers</strong> P (P ≤ D)</li>
<li><strong>subtract</strong> the number of integer multiples in A..D of all <strong>pairs</strong> of products of prime numbers (P ≤ D)</li>
<li><strong>add</strong> the number of integer multiples in A..D of all <strong>triples</strong> of products of prime numbers (P ≤ D)</li>
<li>&#8230;</li>
<li><strong>add</strong> the number of integer multiples in A..D of all <strong>k-tuples</strong> of products of prime numbers (P ≤ D) <strong>if k is odd</strong></li>
<li><strong>subtract</strong> the number of integer multiples in A..D of all <strong>k-tuples</strong> of products of prime numbers (P ≤ D) <strong>if k is even</strong></li>
</ul>
<p style="text-align:justify;">By the way the primes and products of distinct prime numbers are (apart from the number 1) exactly the set of <strong><a href="http://en.wikipedia.org/wiki/Square-free_integer">square-free integers</a></strong>.</p>
<p style="text-align:justify;">To put things on a more rigorous foundation we notice that we actually try to determine the size of a set (the number of values in A..D which can be written as integer multiple of any P ≥ 2) which we can write as a <a href="http://en.wikipedia.org/wiki/Union_%28set_theory%29">union</a> of overlapping subsets (namely multiples of one or more primes). Let S<sub>i</sub> denote the set of integer multiples in the range A..D of the i&#8217;th prime. To calculate the length of the union, we use the <strong><a href="http://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle">Inclusion-Exclusion principle for set unions</a></strong>, which reads:</p>
<p style="text-align:justify;"><img src='http://s0.wp.com/latex.php?latex=%5Cbiggl%7C%5Cbigcup_%7Bi%7D+S_i%5Cbiggr%7C+%3D%5Csum_%7Bi%7D%5Cleft%7CS_i%5Cright%7C+-%5Csum_%7Bi+%3C+j%7D%5Cleft%7CS_i%5Ccap+S_j%5Cright%7C+%5Cqquad+%2B%5Csum_%7Bi+%3C+j+%3C+k%7D%5Cleft%7CS_i%5Ccap+S_j%5Ccap+S_k%5Cright%7C%5C+%5Cldots%5C+%2B%5C++%5Cleft%28-1%5Cright%29%5E%7Bk-1%7D+%5Csum_%7Bi_1+%3C+%5Cldots+%3C+i_k%7D%5Cleft%7CS_%7Bi_1%7D%5Ccap+%5Cldots+%5Ccap+S_%7Bi_k%7D%5Cright%7C+%2B++%5Cldots%5C+%2B+%5Cleft%28-1%5Cright%29%5E%7Bn-1%7D+%5Cleft%7CS_1%5Ccap%5Cldots%5Ccap+S_n%5Cright%7C++&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;biggl|&#92;bigcup_{i} S_i&#92;biggr| =&#92;sum_{i}&#92;left|S_i&#92;right| -&#92;sum_{i &lt; j}&#92;left|S_i&#92;cap S_j&#92;right| &#92;qquad +&#92;sum_{i &lt; j &lt; k}&#92;left|S_i&#92;cap S_j&#92;cap S_k&#92;right|&#92; &#92;ldots&#92; +&#92;  &#92;left(-1&#92;right)^{k-1} &#92;sum_{i_1 &lt; &#92;ldots &lt; i_k}&#92;left|S_{i_1}&#92;cap &#92;ldots &#92;cap S_{i_k}&#92;right| +  &#92;ldots&#92; + &#92;left(-1&#92;right)^{n-1} &#92;left|S_1&#92;cap&#92;ldots&#92;cap S_n&#92;right|  ' title='&#92;biggl|&#92;bigcup_{i} S_i&#92;biggr| =&#92;sum_{i}&#92;left|S_i&#92;right| -&#92;sum_{i &lt; j}&#92;left|S_i&#92;cap S_j&#92;right| &#92;qquad +&#92;sum_{i &lt; j &lt; k}&#92;left|S_i&#92;cap S_j&#92;cap S_k&#92;right|&#92; &#92;ldots&#92; +&#92;  &#92;left(-1&#92;right)^{k-1} &#92;sum_{i_1 &lt; &#92;ldots &lt; i_k}&#92;left|S_{i_1}&#92;cap &#92;ldots &#92;cap S_{i_k}&#92;right| +  &#92;ldots&#92; + &#92;left(-1&#92;right)^{n-1} &#92;left|S_1&#92;cap&#92;ldots&#92;cap S_n&#92;right|  ' class='latex' /></p>
<p style="text-align:justify;">The intersections <img src='http://s0.wp.com/latex.php?latex=S_%7Bi_1%7D+%5Ccap+%5Cldots+%5Ccap+S_%7Bi_k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='S_{i_1} &#92;cap &#92;ldots &#92;cap S_{i_k}' title='S_{i_1} &#92;cap &#92;ldots &#92;cap S_{i_k}' class='latex' /> are simply the multiples of the products of the (distinct) primes i<sub>1</sub> * &#8230; * i<sub>k</sub>. We also see that there is a factor (-1)<sup>k-1</sup> which is positive if the number k of distinct primes is odd and negative if k is even, as we suspected above. Incidentally, this is exactly the negative of the <strong><a href="http://en.wikipedia.org/wiki/M%C3%B6bius_function">Möbius function</a></strong>, which we denote as μ here.</p>
<p style="text-align:justify;">We can tabulate this function for the values 2..D with an algorithm similar to the <strong><a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a></strong>:</p>
<ul>
<li>we initialize the table of the values of μ with all ones</li>
<li>whenever we encounter a prime number, we set the corresponding value in the table to -1 (a prime number is the product of an odd number of distinct primes, namely just the &#8216;product&#8217; of itself).
<ul>
<li>We flip the sign of all multiples of the prime. For a number which is the product of k distinct primes, this will happen k times and thus at the end the corresponding value in the table will have a value of (-1)<sup>k</sup>. For any multiple of p which is divisible <strong>more than once</strong> by p (i.e. is not square free), we set the value of μ to zero (and future sign flips will thus leave this value at zero).</li>
</ul>
</li>
</ul>
<p style="text-align:justify;">The time complexity of the Sieve of Eratosthenes (and thus also of  this method of computing the values of the Möbius function) is O(D *  log(log(D))) which is growing only slightly faster than linear.</p>
<p style="text-align:justify;">By the way, you certainly already have noticed that for N=1 <em>all</em> numbers in A..D can be written as multiples of a number P ≥ 2, i.e. there will always be zero possible bonus assignments (unless 1 is part of the allowed range for numbers). No matter what number a<sub>1</sub> one chooses, if price the P happens to be a<sub>1</sub>, the remainder of a<sub>1</sub> after division by P is zero. In fact, for N dimensions, an N-tuple only needs at least two numbers which are <a href="http://en.wikipedia.org/wiki/Coprime">coprime</a> (i.e. have no common non-trivial divisor) which &#8216;help each other&#8217;, i.e. even if the price is set to one of these two coprimes, the other &#8216;protects&#8217; the tuple. The values of the other N-2 members of the tuple can be any values from the allowed range. However, to correctly count all possible assignments while avoiding double counting (some of the other N-2 members can also have the same value as one of these two coprimes and therefore the number of permutations leading  to distinct sets is difficult to count) makes this a tedious task to say the least.</p>
<hr />
<p style="text-align:justify;">We still need to find an expression for the number of multiples of P in the range A to D (inclusive). This can be derived from the following arguments: let&#8217;s first simplify the task again to counting all multiples of P ≤ D. We must be careful with the &#8216;edge case&#8217; if D is an integer multiple of P:</p>
<ul>
<li style="text-align:justify;">if D is an integer multiple of P, the number of multiples of P smaller than or equal to D is exactly D/P</li>
<li>if D is <strong>not</strong> an integer multiple of P, the largest multiple of P smaller than D is P * ⌊ D/P ⌋ and thus the number of integer multiples of P smaller than D is ⌊ D/P ⌋.</li>
</ul>
<p style="text-align:justify;">One sees that for the first bullet D/P is equal to ⌊ D/P ⌋ so the number of multiples of P less than or equal to D is ⌊ D/P ⌋. To count the number of integer multiples in the range A to D, we subtract the number of multiples of P less than or equal to (A-1) from the number of multiples of P less than or equal to D or in other words: ⌊ D/P ⌋ &#8211; ⌊ (A-1)/P ⌋ .</p>
<p style="text-align:justify;">One can compare this to what one gets from the application of Theorem 1 of the paper at <a href="http://arxiv.org/abs/1002.3254">http://arxiv.org/abs/1002.3254</a>.</p>
<hr />
<p style="text-align:justify;">To summarize the case N=1 we can write the number of values in A..D which are an integer multiple of at least one number P (2≤ P ≤ D) as:</p>
<p><img src='http://s0.wp.com/latex.php?latex=-+%5Csum_%7B2+%5Cle+P+%5Cle+D%7D+%5Cmu%28P%29+%5Ccdot+%5Cleft%28%5Clfloor+D%2FP+%5Crfloor+-+%5Clfloor%28A-1%29%2FP+%5Crfloor%5Cright%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='- &#92;sum_{2 &#92;le P &#92;le D} &#92;mu(P) &#92;cdot &#92;left(&#92;lfloor D/P &#92;rfloor - &#92;lfloor(A-1)/P &#92;rfloor&#92;right)' title='- &#92;sum_{2 &#92;le P &#92;le D} &#92;mu(P) &#92;cdot &#92;left(&#92;lfloor D/P &#92;rfloor - &#92;lfloor(A-1)/P &#92;rfloor&#92;right)' class='latex' /></p>
<hr />
<p style="text-align:justify;">How to generalize this to N dimensions ? One would first be tempted to enumerate all <em>allowed</em> N-tuples by trying to enumerate all those for which there is <strong>at least one a<sub>i</sub></strong> which is <strong>not</strong> divided by a number P. However, as we pointed out above, this is difficult to deal with, the &#8216;at least&#8217; being a contributor to this difficulty. It is easier to count the <strong>complement</strong>, namely all tuples for which a number P divides <strong>all</strong> elements of the tuple. These tuples are the <a href="http://en.wikipedia.org/wiki/Cartesian_product#Cartesian_square_and_Cartesian_power">&#8216;Cartesian power&#8217;</a> of the set of multiples of P in the range A..D. Or in other words, for each a<sub>i</sub> we are free to select any integer multiple of P which is in the range A..D and repetitions of values among a<sub>1</sub>..a<sub>N</sub> are allowed. Thus the number of N-tuples for which P divides <strong>all</strong> elements is simply (⌊ D/P ⌋ &#8211; ⌊ (A-1)/P ⌋)<sup>N.</sup>. We can add up these terms for all values of P under consideration like in the one-dimensional case.</p>
<hr />
<p style="text-align:justify;">To summarize: for f(A,D) we have the following expression:</p>
<p><img src='http://s0.wp.com/latex.php?latex=f%28A%2CD%29+%3D+%28D-A%2B1%29%5EN+-+%28+-+%5Csum_%7B2+%5Cle+P+%5Cle+D%7D+%5Cmu%28P%29+%5Ccdot+%5Cleft%28%5Clfloor+D%2FP+%5Crfloor+-+%5Clfloor%28A-1%29%2FP+%5Crfloor%5Cright%29%5EN%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='f(A,D) = (D-A+1)^N - ( - &#92;sum_{2 &#92;le P &#92;le D} &#92;mu(P) &#92;cdot &#92;left(&#92;lfloor D/P &#92;rfloor - &#92;lfloor(A-1)/P &#92;rfloor&#92;right)^N)' title='f(A,D) = (D-A+1)^N - ( - &#92;sum_{2 &#92;le P &#92;le D} &#92;mu(P) &#92;cdot &#92;left(&#92;lfloor D/P &#92;rfloor - &#92;lfloor(A-1)/P &#92;rfloor&#92;right)^N)' class='latex' /></p>
<p style="text-align:justify;">And the value sought is (f(A,D) &#8211; f(A,C-1) &#8211; f(B+1,D) + f(B+1,C+1)) mod 1000000007 as discussed above.</p>
<p style="text-align:justify;">Looking at the time complexity, we note that the above sum involves powers of N (which can be large).  An efficient method to calculate such powers is <a href="http://en.wikipedia.org/wiki/Exponentiation_by_squaring">exponentiation by squaring</a> which has O(log(N)) time complexity. The complexity of the sum is therefore O(D * log(N)) and the overall asymptotic behaviour (when taking into account the calculation of the Möbius function) is O(D * (log(N) +  log(log(D)))).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/303/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=303&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2011/02/08/facebook-hacker-cup-round-2-problem-3-%e2%80%93-some-discussion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>

		<media:content url="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-drawing-square4.png?w=1024" medium="image">
			<media:title type="html">2011-02-fbhc-drawing-square</media:title>
		</media:content>

		<media:content url="http://aholzner.files.wordpress.com/2011/02/2011-02-fbhc-prime-multiples.png" medium="image">
			<media:title type="html">2011-02-fbhc-prime-multiples</media:title>
		</media:content>
	</item>
		<item>
		<title>Facebook Hacker Cup 2011 Round 2 Problem 1 &#8211; some discussion</title>
		<link>http://aholzner.wordpress.com/2011/02/07/facebook-hacker-cup-round-2-problem-1-some-discussion/</link>
		<comments>http://aholzner.wordpress.com/2011/02/07/facebook-hacker-cup-round-2-problem-1-some-discussion/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 12:23:22 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[convolution]]></category>
		<category><![CDATA[discrete logarithm]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[fast Fourier transform]]></category>
		<category><![CDATA[fft]]></category>
		<category><![CDATA[finite fields]]></category>
		<category><![CDATA[Galois fields]]></category>
		<category><![CDATA[hacker cup]]></category>
		<category><![CDATA[Karatsuba]]></category>
		<category><![CDATA[scotts new trick]]></category>
		<category><![CDATA[time complexity]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=260</guid>
		<description><![CDATA[(see also the discussion of problem 3) Facebook&#8217;s Hacker Cup online round 2 took place recently. The first problem (&#8216;Scott&#8217;s New Trick&#8217;) was about counting products of elements of two series which are smaller than a given value L, modulo a given prime number P (see http://www.facebook.com/hackercup and links therein to see the exact problem [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=260&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>(see also the <a href="http://aholzner.wordpress.com/2011/02/08/facebook-hacker-cup-round-2-problem-3-%E2%80%93-some-discussion/">discussion of problem 3</a>)</p>
<p>Facebook&#8217;s Hacker Cup online round 2 took place recently. The first problem (&#8216;Scott&#8217;s New Trick&#8217;) was about counting products of elements of two series which are smaller than a given value L, modulo a given prime number P (see <a href="http://www.facebook.com/hackercup">http://www.facebook.com/hackercup</a> and links therein to see the exact problem statement). These two series were defined as <a href="http://en.wikipedia.org/wiki/Recurrence_relation#Linear_homogeneous_recurrence_relations_with_constant_coefficients">linear homogeneous recurrence relations with constant coefficients</a>.</p>
<p>The brute force approach consists of explicitly calculating all products of the two series (which have length M and N respectively), resulting in a complexity of O(M*N). With M and N allowed to go to ten million, M*N can be as high as 10<sup>14</sup>. Even if one was able to test one pair per processor clock cycle, it would still take 10<sup>5</sup> seconds (more than a day) with a 1 GHz processor with this approach in the worst case.</p>
<p>As we are performing operations on a <a href="http://en.wikipedia.org/wiki/Finite_field">finite field</a> GF(P), one could imagine that the series of values starts repeating at some point and thus one would not have to calculate pairs of products of <em>all</em> elements of a series but only look at the elements of one period.</p>
<p>However, each element a<sub>i</sub> was defined in terms of its <strong>two</strong> preceding elements a<sub>i-1</sub> and a<sub>i-2</sub> so there are P*P different states (rather than P) when generating the series and thus the period of the two series is bounded by P<sup>2</sup>. With a maximum value of 250&#8217;000 for P, P<sup>2</sup> is 62.5 billions in the worst case, corresponding to 62.5 seconds worth of CPU cycles on a 1 GHz processor. Moreover, the two series a and b need not have the same period and thus one would have to consider the <a href="http://en.wikipedia.org/wiki/Least_common_multiple">least common multiple</a> in terms of number of pairs of the two periods, which is O(P<sup>4</sup>) in the worst case and thus not feasible.</p>
<p>A more elegant idea is to just count the <strong>number of occurrences</strong> of each of the possible P values in each series a and b, test for all possible products of integers a<sub>i</sub>, b<sub>j</sub> smaller than P whether they satisfy the condition (to be less than the given number L) and then weight the accepted products with (number of occurrences of a<sub>i</sub>) * (number of occurrences of b<sub>j</sub>). Such an approach reduces the complexity from O(N*M) to O(M+N+P<sup>2</sup>). However, this is still at least at the limit for the calculation of 20 test cases within six minutes if not impossible.</p>
<p>A more efficient solution makes use of <strong><a href="http://mathworld.wolfram.com/DiscreteLogarithm.html">discrete logarithms</a></strong>. In other words, we write each integer x<sub>i</sub> as x<sub>i</sub> = g<sup>y<sub>i</sub></sup> where y<sub>i</sub> is called the discrete logarithm of x<sub>i</sub> with respect to the basis g. g is must be a <a href="http://en.wikipedia.org/wiki/Primitive_root_modulo_n">primitive root</a> modulo P to ensure that for any integer x<sub>i</sub> ∈ [0..P-1] there is a value y<sub>i</sub> which satisfies this equation. It takes O(P) time to calculate a map of all x<sub>i</sub> to their logarithms y<sub>i</sub> by simply calculating the powers 0..P-1 of g by repeated multiplication. Thus we can reformulate our problem from:</p>
<ul>
<li>find all pairs of elements of the field GF(P) whose <strong>product</strong> is equal to a given value V</li>
</ul>
<p>to</p>
<ul>
<li>find all pairs of elements of the field GF(P) whose <strong>sum</strong> is equal to a given value log<sub>g</sub>V</li>
</ul>
<p>where we would vary V from 0 to L-1. Note that taking the logarithm of each element in GF(P) simply corresponds to a reordering of the elements which we have to take into account when considering how often an integer appears in the sequences a and b (see above).</p>
<p>What do we gain from this transformation ? When writing down the transformed task as an equation, we want to calculate:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=u_k+%3D+%5Cdisplaystyle%5Csum_%7Bi%3D0%7D%5E%7BP-1%7D+w_i+%5Ccdot+v_%7Bk-i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='u_k = &#92;displaystyle&#92;sum_{i=0}^{P-1} w_i &#92;cdot v_{k-i}' title='u_k = &#92;displaystyle&#92;sum_{i=0}^{P-1} w_i &#92;cdot v_{k-i}' class='latex' /></p>
<p>where w<sub>i</sub> and v<sub>i</sub> are the number of occurrences of element g<sup>w<sub>i</sub></sup> in the series a and g<sup>v<sub>i</sub></sup> in the series b, respectively. u<sub>k</sub> is thus the sum of the products of all weights (i.e. number of occurrences) associated to pairs whose product is g<sup>i + (k-i)</sup> = g<sup>k</sup>. k is the discrete logarithm of V (introduced above) with respect to the base g.</p>
<p>Naturally one would think that one would need to calculate P products for these P equations (k ranges from 0 to P-1), thus still requiring O(P<sup>2</sup>) operations. However, looking at it a bit more carefully, one realizes that this in fact a <strong><a href="http://en.wikipedia.org/wiki/Convolution#Circular_discrete_convolution">discrete (circular) convolution</a></strong>. And for this task, <a href="http://en.wikipedia.org/wiki/Convolution#Fast_convolution_algorithms">fast convolution algorithms</a> exist which have O(P * log(P)) time complexity. One common approach for fast convolution is to transform the two sequences into Fourier space (using a <a href="http://en.wikipedia.org/wiki/Fast_Fourier_transform">fast Fourier transform</a>) where the <a href="http://en.wikipedia.org/wiki/Convolution_theorem">convolution of the series corresponds to the product of the transformed series</a> and transform them back to the original space (using the inverse transform).</p>
<p>Another possibility is to use a <a href="http://en.wikipedia.org/wiki/Multiplication_algorithm#Karatsuba_multiplication">Karatsuba</a> like algorithm for convolution which has time complexity O(P<sup>1.585</sup>). This algorithm is designed to multiply two numbers written as x<sub>n</sub> * b<sup>n</sup>+&#8230;+x<sub>0</sub> * b<sup>0</sup> and y<sub>n</sub> * b<sup>n</sup>+&#8230;+y<sub>0</sub> * b<sup>0</sup> where the x<sub>i</sub> and y<sub>i</sub> are the &#8216;digits&#8217;  and b is the <a href="http://en.wikipedia.org/wiki/Base_%28mathematics%29">radix</a>. On the other hand, multiplying these two sums is this equivalent to finding the convolution of the series of coefficients x and y.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/260/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=260&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2011/02/07/facebook-hacker-cup-round-2-problem-1-some-discussion/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Jython as a configuration language for Java programs</title>
		<link>http://aholzner.wordpress.com/2010/11/26/using-jython-as-a-configuration-language-for-java-programs/</link>
		<comments>http://aholzner.wordpress.com/2010/11/26/using-jython-as-a-configuration-language-for-java-programs/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 21:18:18 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jython]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=128</guid>
		<description><![CDATA[I recently tried using Jython as a configuration language for a Java program. In fact, this was inspired by the fact that the main C++ project I&#8217;m working with at work has switched to python as a configuration language some time ago. This has several advantages: no need to invent a configuration language from scratch [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=128&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently tried using Jython as a configuration language for a Java program. In fact, this was inspired by the fact that the main C++ project I&#8217;m working with at work has switched to python as a configuration language some time ago. This has several advantages:</p>
<ul>
<li>no need to invent a configuration language from scratch</li>
<li>no need to write a parser for such a language</li>
<li>some users might already know python and thus they will not need to learn a new language (and python/jython is also useful beyond writing configuration files for the program)</li>
<li>the power of a programming language:
<ul>
<li>conditional statements</li>
<li>looping</li>
<li>generate configurations depending on parameters, contents of external files, environment variables etc.</li>
</ul>
</li>
</ul>
<p>An example configuration file could then look like:</p>
<pre>input_files = [ "/etc/fstab", "/etc/mtab" ]
output_file = "/tmp/test.txt"
</pre>
<p>but we can also use some more complex logic inside the configuration file:</p>
<pre>import glob, time
input_files = glob.glob("/etc/*tab")
output_file = "/tmp/test-" + time.strftime("%Y-%m-%d") + ".txt"
</pre>
<p>This example should give you an idea how powerful the concept is. In fact, whatever is possible in Jython is also possible to do in the configuration file.</p>
<p>It is often natural to group related parameters into a class, such as InputFile in the following example configuration:</p>
<pre>input_files = [
  InputFile(name = "/etc/fstab", startLine = 2, endLine = 5, commentChar = '#'),
  InputFile(name = "/etc/mtab", endLine = 3),
  InputFile(name = "/etc/crontab"),
]
</pre>
<p>The underlying Java implementation of InputFile could look as follows:</p>
<pre>public class InputFile
{
  ...

  /** Keyword arguments constructor. */
  public InputFile(PyObject values[], String names[])
  {
    System.out.println("called kwargs constructor: names=" +
      Arrays.asList(names) + " values=" + Arrays.asList(values));
     // store the given values in fields
    ...
  }
}</pre>
<p>Note that keyword arguments constructors (requested <a href="http://bugs.jython.org/issue1503">here</a>) are only supported in recent Jython releases (since 2.5.2-b2 according to the <a href="http://www.jython.org/latest.html">release notes</a>). They are a bit more complicated to implement on the Java side than classic constructors but the use of keyword arguments in the configuration file makes it much more readable.</p>
<p>In order to read a configuration from a Jython file one needs to embed a Jython interpreter into the application. For this, it is necessary to add jython.jar from the Jython installation to the corresponding Java project.</p>
<p>The code for reading the configuration might look like this:</p>
<pre>public class JythonConfigReader
{

  public JythonConfigReader(String input_fname)
  {
    // instantiate a Jython interpreter
    PythonInterpreter interpreter = new PythonInterpreter();

    // import configuration objects package in the Jython interpreter
    interpreter.exec("from my.package.with.configuration.classes import InputFile");

    // read the input file with the Jython interpreter
    // note that this will throw an exception if the python
    // code is not valid
    interpreter.execfile(input_fname);

    // get local variables
    PyStringMap locals = (PyStringMap) interpreter.getLocals();

    if (! locals.has_key("input_files"))
      throw new IllegalArgumentException("parameter input_files not found");

    // get the list of input files
    PyObject input_files = locals.__getitem__("input_files");

    if (! (input_files instanceof PyList))
      throw new IllegalArgumentException("parameter input_files is not a list");

    PyList input_files_list = (PyList) input_files;
    Iterator iter = input_files_list.iterator();

    // loop over all elements of the list
    while (iter.hasNext())
    {
      Object obj = iter.next();

      if (! (obj instanceof InputFile))
        throw new IllegalArgumentException("input_files contains objects of type other than InputFile");

      System.out.println("got an input file");

      // process this input file
      ...

    } // loop over all elements of input_files
  }
}
</pre>
<p>One potential danger of using Jython as a configuration language is that if one only looks at the names of the known parameters in locals() (such as input_files in the above example), one might miss mis-spelled optional parameters. To prevent such problems to some extent one can define a top-level configuration class and add other configuration objects as members, e.g. like:</p>
<pre>config = Configuration()
config.input_files = [ InputFile(...), InputFile(...) ]
</pre>
<p>and then insist that there is only one top level Configuration object.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=128&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2010/11/26/using-jython-as-a-configuration-language-for-java-programs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>An animation of adding elements to a heap</title>
		<link>http://aholzner.wordpress.com/2010/10/24/an-animation-of-adding-elements-to-a-heap/</link>
		<comments>http://aholzner.wordpress.com/2010/10/24/an-animation-of-adding-elements-to-a-heap/#comments</comments>
		<pubDate>Sun, 24 Oct 2010 17:39:01 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[bubble up]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[trident]]></category>
		<category><![CDATA[visualization]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=217</guid>
		<description><![CDATA[Here is an animation of how elements are inserted into a heap (sometimes used to implement a prioirty queue). At the top you can see the elements arranged in a tree, at the bottom is the corresponding array representation of the heap. The correspondence between the tree nodes and the array is such that the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=217&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is an animation of how elements are inserted into a <a href="http://en.wikipedia.org/wiki/Heap_%28data_structure%29">heap</a> (sometimes used to implement  a <a href="http://en.wikipedia.org/wiki/Priority_queue">prioirty queue</a>). At the top you can see the elements arranged in a tree, at the bottom is the corresponding array representation of the heap. The correspondence between the tree nodes and the array is such that the children of array element j are stored in positions 2j+1 and 2j+2 (where the indexing starts at 0).</p>
<p>The algorithm corresponds to what is described in section 4.3.2 of in <a href="http://www.amazon.com/Algorithm-Design-Manual-Steve-Skiena/dp/0387948600">&#8220;The Algorithm Design Manual&#8221;</a> by Steven Skiena. Each time an element is added, it is put into the first empty position in the array representation at the bottom. This corresponds to making the new element a child of one of the leaf nodes. Then, the element is &#8216;bubbled up&#8217; (swapped with its parent) as long as it is smaller than the parent.</p>
<p>Running the example requires <a href="http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136112.html">Java Webstart</a> (and is at your own risk, the author does not take any responsibility for any negative effects caused by running this example). To start the demo, click <a href="https://sites.google.com/site/andreholzneralgovis/heapadding01/HeapAdding01.jnlp?attredirects=0">here</a>.  This demo makes use of the <a href="http://kenai.com/projects/trident/pages/Home">Trident java animation library</a>.</p>
<p>Comments and suggestions are welcome !</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/217/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=217&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2010/10/24/an-animation-of-adding-elements-to-a-heap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>An animation of the Quicksort algorithm</title>
		<link>http://aholzner.wordpress.com/2010/10/17/an-animation-of-the-quicksort-algorithm/</link>
		<comments>http://aholzner.wordpress.com/2010/10/17/an-animation-of-the-quicksort-algorithm/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 09:55:33 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[quicksort]]></category>
		<category><![CDATA[Skiena]]></category>
		<category><![CDATA[trident]]></category>
		<category><![CDATA[webstart]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=214</guid>
		<description><![CDATA[Here is a (preliminary) animation of the Quicksort algorithm. At the same time, this is a small project to try out the Trident java animation library. Running the example requires Java Webstart (and is at your own risk, the author does not take any responsibility for any negative effects caused by running this example). To [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=214&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is a (preliminary) animation of the <a href="http://en.wikipedia.org/wiki/Quicksort">Quicksort</a> algorithm. At the same time, this is a small project to try out the <a href="http://kenai.com/projects/trident/pages/Home">Trident java animation library</a>.</p>
<p>Running the example requires <a href="http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136112.html">Java Webstart</a> (and is at your own risk, the author does not take any responsibility for any negative effects caused by running this example). To start the demo, click <a href="https://sites.google.com/site/andreholzneralgovis/quicksort01/QuickSort01.jnlp?attredirects=0">here</a>.</p>
<p>A few explanations:</p>
<ul>
<li>The example is modeled after the Quicksort implementation as given in <a href="http://www.amazon.com/Algorithm-Design-Manual-Steve-Skiena/dp/0387948600">&#8220;The Algorithm Design Manual&#8221;</a> by Steven Skiena (Section 4.6)</li>
<li>Those boxes which are already at the final position are drawn in white (instead of gray)</li>
<li>The boxes with dark red background are those currently being worked on (between the variables l and h). The variables l and h themselves are shown by the boxes &#8216;L&#8217; and &#8216;H&#8217;</li>
<li>The variable i is displayed by the box I</li>
<li>The pivot (against which the element at i is compared) is at position h in this implementation</li>
<li>The box F corresponds to the value of the variable &#8216;firsthigh&#8217; in Skiena&#8217;s book</li>
</ul>
<p>You&#8217;ll notice that:</p>
<ul>
<li>after i reached the right end of the range currently being worked on (end of routine &#8216;partition&#8217;), the value originally at h will be put where &#8216;firsthigh&#8217; is and is at its final position. The elements left of it (in the current range) will have a smaller value, the ones on the right a larger value.</li>
</ul>
<p>Comments and suggestions are welcome !</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=214&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2010/10/17/an-animation-of-the-quicksort-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>Filling a TNtuple in pyROOT</title>
		<link>http://aholzner.wordpress.com/2010/10/14/filling-a-tntuple-in-pyroot/</link>
		<comments>http://aholzner.wordpress.com/2010/10/14/filling-a-tntuple-in-pyroot/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 09:07:54 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PyROOT]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[TNtuple]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=203</guid>
		<description><![CDATA[I recently came across the problem that I wanted to create a TNtuple with 635 variables. I wanted to avoid using a TTree if possible (how to do this is discussed here). I found a solution in a tutorial (page 17) how to use fill a TNtuple with few variables (TNTuple currently has two Fill(..) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=203&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently came across the problem that I wanted to create a TNtuple with 635 variables. I wanted to avoid using a TTree if possible (how to do this is discussed <a href="http://phacker.org/2009/03/08/using-pyroot-to-write-a-ttree/">here</a>).</p>
<p>I found a solution in a <a href="http://www.usatlas.bnl.gov/PAT/meetings/Quarrie-PhysicsAnalysis.pdf">tutorial</a> (page 17) how to use fill a TNtuple with few variables (TNTuple currently has two Fill(..) methods: One taking up to 15 values and another one taking an array).</p>
<pre>So the example in the tutorial did not work for me as I have more than 15 values per row. I managed to do this however with code similar to the following one:
<code>
import ROOT, array</code>

# the variable names for the output tuple
varnames = [ ... ]</pre>
<pre># open the output file
fout = ROOT.TFile("output.root", "RECREATE")
fout.cd()</pre>
<pre># create the ntuple
output_tuple = ROOT.TNtuple("tuple","tuple",":".join(varnames))</pre>
<pre># calculate the values to be stored in the output tuple
values = [ ... ]</pre>
<pre># loop over events
...
    # convert the list of values to an float array and fill the output tuple
    output_tuple.Fill(array.array("f'',values))</pre>
<pre># write the tuple to the output file and close it
fout.cd()
output_tuple.Write()</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/203/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=203&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2010/10/14/filling-a-tntuple-in-pyroot/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
		<item>
		<title>Generating C++ index files for Eclipse from the command line (Linux)</title>
		<link>http://aholzner.wordpress.com/2010/09/26/generating-c-index-files-for-eclipse-from-the-command-line-linux/</link>
		<comments>http://aholzner.wordpress.com/2010/09/26/generating-c-index-files-for-eclipse-from-the-command-line-linux/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 16:31:58 +0000</pubDate>
		<dc:creator>aholzner</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[CDT]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[large scale project]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[PDOM]]></category>
		<category><![CDATA[Persisted Document Object Model]]></category>

		<guid isPermaLink="false">http://aholzner.wordpress.com/?p=171</guid>
		<description><![CDATA[I finally managed to produce C++ index files by running the Eclipse CDT indexer from the command line (as opposed to running it from the IDE). I downloaded the most recent &#8216;Eclipse with CDT bundle&#8217; from http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr1 and unpacked it. I ran the indexer as follows on Linux: cd path-from-where-i-unpacked-the-eclipse-cdt-distribution cd eclipse ./eclipse \ -data [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=171&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I finally managed to produce C++ index files by running the Eclipse CDT indexer from the command line (as opposed to running it from the IDE).</p>
<ol>
<li>I downloaded the most recent &#8216;Eclipse with CDT bundle&#8217; from <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr1" target="_top">http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr1</a> and unpacked it.</li>
<li>I ran the indexer as follows on Linux:
<pre>cd path-from-where-i-unpacked-the-eclipse-cdt-distribution
cd eclipse
./eclipse \
-data /tmp/ws \
-nosplash \
-application org.eclipse.cdt.core.GeneratePDOM \
-target /tmp/test.pdom \
-source /path-to-my-source-directory \
-id "" \
-include /path-to-my-source-directory</pre>
<p>where</p>
<ul>
<li><tt>path-to-my-source-directory</tt> is the directory where the source I wanted to index is. For this project, this is also the top directory where its include files are searched.</li>
<li><tt>path-from-where-i-unpacked-the-eclipse-cdt-distribution</tt> is the directory from which I unpacked the downloaded eclipse-cdt distribution.</li>
<li>I created a new directory <tt>/tmp/ws</tt> to be used as a pristine workspace (I suspected that in some attempts indexing stopped unexpectedly because it found some corrupted files in the workspace from previous indexing attempts).</li>
<li><tt>/tmp/test.pdom</tt> is the index file to be generated.</li>
<li>The id option seems to be required and seems to be some kind of identifier identifying the indexed content, so I gave it the empty string.</li>
</ul>
</li>
</ol>
<p>Running this on the 2.5 million lines of C++ code project I&#8217;m working  with at work took 21 minutes. I got zillions of error messages about unknown symbols which are  mostly from the standard C++ and from external libraries. The summary at the end of the program was as follows:</p>
<pre>C/C++ Indexer: Project '__prebuilt_index_temp__1285516188327' (13085 sources, 12099 headers)
 Options: indexer='PDOMFastIndexer', parseAllFiles=true, unusedHeaders=useDefaultLanguage, skipReferences=false,
   skipImplicitReferences=false, skipTypeReferences=false, skipMacroReferences=false.
 Database: 172355584 bytes
 Timings: 1074226 total, 209100 parser, 107105 resolution, 371885 index update.
 Errors: 0 internal, 61032 include, 1806 scanner, 3366 syntax errors.
 Names: 973647 declarations, 4173506 references, 1259869(19.66%) unresolved.
 Cache[34MB]: 763063990 hits, 1360364(0.18%) misses.
Indexer: completed PDOMRebuildTask[1076475ms]
== Generation ends</pre>
<p>Indeed, <tt>/bin/ls -lh</tt> reports the size of the generated index file as 165 MBytes.</p>
<p>The project I ran it on also has other types of files (mostly xml, python and Fortran lines of source code according to <tt>sloccount</tt>). Running the CDT indexer on all files (without removing the non-C++ files) seemed to cause problems: the indexer stopped after a few seconds without producing an index file but unfortunately not saying anything about why it stopped prematurely.</p>
<p>I found some documentation on the command line indexer&#8217;s arguments <a href="http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.cdt.doc.isv/guide/dom/index/prebuiltIndexes.html">here</a> (in the &#8220;Common command-line options&#8221; section).</p>
<p>Now I can start exploiting the information contained in the  generated index file.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aholzner.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aholzner.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aholzner.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aholzner.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aholzner.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aholzner.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aholzner.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aholzner.wordpress.com/171/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aholzner.wordpress.com&amp;blog=9753242&amp;post=171&amp;subd=aholzner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aholzner.wordpress.com/2010/09/26/generating-c-index-files-for-eclipse-from-the-command-line-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/516193e369370693f2991ccec37b4dfc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aholzner</media:title>
		</media:content>
	</item>
	</channel>
</rss>
