Articles
Posted by Andy Buckley on Jul 08, 2008
This is a fun one. Take the following mini C++ program:
#include <iostream>
using namespace std;
int main() {
cout << "abcdefgh" + 0 << endl;
cout << "abcdefgh" + 1 << endl;
cout << "abcdefgh" + 2 << endl;
cout << "abcdefgh" + 3 << endl;
cout << "abcdefgh" + 4 << endl;
cout << "abcdefgh" + 8 << endl;
return EXIT_SUCCESS;
}
What happens? Well, at first glance you’d think that the output will be a list of concatenated strings like abcdefgh0, abcdefgh1 etc. After all, that’s a pretty common idiom in just about every language that’s more sophisticated than PDP11 assembler… oh, hang on, C/C++ *is* PDP11 assembler in disguise. So these string literals won’t behave like std::string — they’ll probably do something evil. And so it comes to pass:
buckley@d18:~/tmp/cc$ g++ -o teststradd teststradd.cc
buckley@d18:~/tmp/cc$ ./teststradd
abcdefgh
bcdefgh
cdefgh
defgh
efgh
(empty line)
I expected something evil, but this particular behaviour was a surprise: the first n characters are removed. What’s actually going on is that the string literal is a const char* pointer, and the +n is incrementing that pointer by n units. The printout then starts at the offset and writes until it finds the terminating null character!
So much for the reason — the main point is: WTF?!? One more reason to relegate C++ to purely system tasks like writing memory managers, device drivers and other bit-wrangling tools. Once strings, UIs and all that stuff get involved, we should be carefully segregating the C++ to places where it can’t do any harm, and using Java, Python, D… or whatever for the touchy-feely stuff.
Continue reading…
Posted by Andy Buckley on Jun 06, 2008
Hey physicists! Lack of an observed Higgs boson getting you down? Well fret no longer: you can make your own, thanks to the miracle of ROOT! Look, here’s one I made earlier:

Okay, so everything’s wrong: it’s not a very good impression of a Higgs, I know (and let’s not even mention the dismal message that this sends about physicist aesthetics and the attention paid to typesetting by the ROOT authors) But this is a worrying effect, given that this is the CINT macro that produced it:
{
double edges[19] = {-3.0, -2.7, -2.4, -2.1, -1.8, -1.5, -1.2, -0.9, -0.6, -0.3,
0.0, 0.3, 0.6, 0.9, 1.2, 1.8, 2.4, 2.7, 3.0};
// Double-size bins ^ ^
TH1F h("wrong", "This is not a real peak, it's just binning", 18, edges);
h.FillRandom("gaus", 10000);
h.SetMinimum(0.0);
h.Draw();
}
Yes, that plot is a random Gaussian distribution, according to ROOT. The big peak on the RHS is created by the two bins of width 0.6 (as opposed to 0.3 everywhere else). I hope it’s obvious that this is wrong! If it were a bar graph where the width of bins has no meaning, then it would be correct, but the idea of a histogram is that bin heights are set by the sum of weights in the bin divided by the bin width. Or, expressed as they tell you at school, a histogram’s area, rather than height, is the thing that reflects the number of “events” in that bin. For differential distributions (i.e. densities), which account for about 99% of all physics distributions, histograms are the only sensible statistical display to use, since they maintain the distribution’s shape as an invariant under arbitrary rebinnings: with asymptotically high statistics, a histogram should have heights equal to the mean value of the true distribution between the bin edges, a criterion which bar plots do not satisfy. Or, more loosely speaking, the choice of bin edge position on a distribution shouldn’t have any significance!
This is a silly mistake, a schoolboy error. It’s like trying to uniformly sample a spherical surface without accounting for the d(cos(theta)) measure factor: the 1D measures here are the bin widths. And it’s a dangerous error from a physics perspective, as the plot above shows: in a real physics analysis, it’s conceivable that you would bin more tightly around a region of interest, like a potential Higgs peak, in which case ROOT would actually display a dip! It’s amazing that no one seems to have noticed this in 15+ years of ROOT being used by the HEP community. I don’t use ROOT enough to know if this is a known issue — students and postdocs that I’ve mentioned this to have been surprised. Maybe it reflects the tendency of the community to make private work-arounds rather than report bugs upstream — not that my experiences of trying to report bugs on ROOT have been very encouraging — or just that with the lack of LHC data no-one has made any non-uniformly binned histograms yet!
Given that my gripes against ROOT are well-publicised, it’s with some trepidation (albeit also a fair chunk of smugness) that I’m writing this, but this issue needs to be publicised and fixed. The fix, fortunately, is just for the rendering system to include the width factor when calculating bin heights: the API’s GetBinData(index) function name doesn’t imply anything wrong about heights, it’s just being used inappropriately. Fixing it should definitely be done, and wouldn’t be hard, but it’s difficult to know how much existing code relies on this behaviour.
Continue reading…
Posted by Andy Buckley on May 02, 2008
It’s probably because I’m tired from writing computing lecture notes and example code ‘til late last night, but today just seems to have been chock full of people doing stupid things in my general direction. I can understand why Don Knuth disconnected himself from email in 1990 — the internet is just full of stupid people doing things badly.
Continue reading…
Posted by Andy Buckley on Apr 28, 2008
I was pointed at this interview with maths/compsci/typesetting guru Don Knuth this morning. Apart from clearly being a wizard in the Feynman sense*, it’s wonderfully inspiring to see someone so at ease with the world and his place in it that he can say this:
Andrew: In late 2006, you were diagnosed with prostate cancer. How is your health today?
Donald: Naturally, the cancer will be a serious concern. I have superb doctors.
At the moment I feel as healthy as ever, modulo being 70 years old. Words flow freely
as I write TAOCP and as I write the literate programs that precede drafts of TAOCP.
I wake up in the morning with ideas that please me, and some of those ideas actually
please me also later in the day when I’ve entered them into my computer.
On the other hand, I willingly put myself in God’s hands with respect to how much more
I'll be able to do before cancer or heart disease or senility or whatever strikes. If
I should unexpectedly die tomorrow, I’ll have no reason to complain, because my life has
been incredibly blessed.
Wow.
[*] “There are two kinds of geniuses: the ‘ordinary’ and the ‘magicians’. An ordinary genius is a fellow whom you and I would be just as good as, if we were only many times better. There is no mystery as to how his mind works. Once we understand what they’ve done, we feel certain that we, too, could have done it. It is different with the magicians. Even after we understand what they have done it is completely dark. Richard Feynman is a magician of the highest calibre.” — Mark Kac
Posted by Andy Buckley on Apr 16, 2008
Last week I finally decided to waste some money and upgrade my computer speakers from the acceptable-but-a-wee-bit-harsh-and-tinny £50 Creative Gigaworks T20 pair that I’ve been using for about 9 months to a set of Bose Companion 3’s.
The online reviews for the Creative Gigaworks speakers were just too good for me to ignore 9 months ago… and I think there’s something to the advice that computer speakers with subwoofers will be boomy and unsubtle — good for games maybe, but rubbish for music and wannabe-audiophile ears (like mine, ha!) But the moment I got them out of the box (mm, nice build quality) and plugged in (hmm, bit tinny…) I knew it wasn’t quite right. Next time I’d just pay stupid money and get the Boses.
And so it has come to pass, and blimey they’re brilliant. They are expensive, as any hi-fi nerd worth their salt will tell you, and yes I could probably have got the same sound for less by piecing together an amplifier from one manufacturer, floorstander speakers from another, and cabling from a third. But it doesn’t matter, because these are tiny, come in one box, look and feel brilliant and sound outstanding. Convenience and aesthetics are worth spending money on, sometimes, and I didn’t see anyone else selling speakers which produce sound of this quality from such tiny units.
Well, I nearly did. The Acoustic Energy Aegos were a serious contender and at a bit over half the price they certainly beat Bose on economic grounds. But life ain’t so simple: the Boses are prettier, and they come with a beautifully designed little remote unit with a soft rubber volume control, touch-sensitive on/off panel and discrete headphone/input jack sockets. The AEs force you to rummage under the desk for the subwoofer to control the volume or plug your headphones in — so I seem to have reached the point in my life where I’m prepared to pay £80 not to have to bend over. Life’s funny, isn’t it?
Anyway, my point is: if you listen to a lot of music through your computer speakers, and space and nice design are important things, then the Bose Companion speakers are fantastic little life-enhancing devices. Yes, you can do the same thing cheaper, for which the audio geek comments here may be useful, but you’ll have to shop around a lot to find anything that does it with half as much style. IMO :)
Continue reading…
Posted by Andy Buckley on Apr 02, 2008
My laptop died last week (conveniently during a trip to UCL when I really wanted it to work) and so I took the opportunity to upgrade it to the beta version of Ubuntu Hardy. First impressions are all good — everything becomes progressively more Mac-like and shiny — although I have some issues with the usability of the Compiz window effects, pretty though they undoubtedly are. The upgrade also made me think again about my Colemak keyboard layout, which has been necessary on my laptop since I prised all the keys off 6 months ago and put them back down in a different order.
Continue reading…
Posted by Andy Buckley on Mar 09, 2008
Given that the digitising and onlineification of The Night Climbers of Cambridge was such a success, it seemed only rational to go through the whole palaver again. Ahem. And lo, Cambridge Nightclimbing online has come to be, courtesy of a variety of open source tools documented at some length in the colophon.
This 1970 book, by “Hederatus”, was the 1960s answer to Whipplesnaith and is just as full of derring do and fine photographs, only with more of an emphasis on coffee and cigarettes. And as if that isn’t enough, you can also have fun trying to guess which of the protagonists ended up as a current MP… Enjoy.
Posted by Andy Buckley on Feb 25, 2008
We’re back from holiday now - a week of skiing in La Plagne with Jo’s brothers and friends. On the whole it was great, not least just to get away and chill a bit. The size of the group, and the variation in skiing level, made coordinating things a bit awkward, though: it’s difficult when you have to get back across the valleys in time for lunch or the end of ski-school. On getting back to work, I really appreciate how simple life is when all you need to concern yourself with is “can I get down this route without a nasty accident?” :) I remember feeling the same last year after 10 days of ice climbing… maybe the time of year makes the feeling that bit more intense.
I bought Didier Givois’ “Les Cles de Paradiski” book in Plagne Centre on the first day (he was prepared to post me a copy beforehand but the expense didn’t seem worthwhile - it’s a heavy book) and spent the evenings poring over it with the help of a map. It’s a pretty good book but, coming from a climbing background, I’d have preferred a clear photo(s) and route guide for each route in place of some of the spectacular but generic “awesome skiing” photos that take up a lot of pages. Maybe he wants to protect people from themselves by not providing enough info. Maybe he knows that most people will never ski the routes and will just enjoy having a chunky coffee table book. Maybe he just likes big photos. But to me it seems there’s no reason why a pocket-sized route guide, as you’d get for climbing, wouldn’t work for off piste skiing. The same safety disclaimers would apply: it’s not the information about routes that leads to injury — it’s unprepared and over-confident users that do that. (I’m one to talk, though: since no-one else in our group was up for “silly skiing”, I had to pick the routes with little avalanche hazard and do them alone. Not ideal.)
I also read Mark Twight’s collected articles, Kiss or Kill while I was away. Mark is as forceful and controversial a writer as he is a climber, and the subtitle “Confessions of a Serial Climber” sums things up pretty well — his take on climbing is far more intense, uncompromising and psychopathic than your average weekend warrior, myself included. On emotional baggage, he repeatedly declares that he’s “got good with the knife”, cutting away his ties to friends, family and lovers when the chance to die on a mountain comes a-calling. Dark stuff, and refreshingly different from most climbers’ writing. Reading the book while in a ski resort was an interesting contrast — skiing is the perfect example of a commodotised, homogenised sport, whose purer forms are these days a relative obscurity. Every time I left the pistes and headed for a couloir, trudging upward high above the neon-suited goons, I thought of Kiss or Kill. Every time I returned to the bars and creperies I recognised a bit of the Twight darkness in my loathing of the “dude, I did a black run… hardcore” attitude. And all the dudes with cool hats in the lift queues… argh: life would be better if the plastic boot and the carving ski hadn’t been invented. I found this time, more than any other ski trip, that skiing between markers, on maintained, secured pistes no longer holds much interest for me — what I like these days in skiing is the bits that most resemble offshoots of mountaineering. This resonated with the book — what I find unappetising in resort skiing is much the same as what it is in ice climbing comps that rub Twight up the wrong way. Not that my skiing really compares to super-light, alpine style mountaineering — that way lies undeserved ego massage — but it was reassuring to find a voice that I could agree with while hanging out in resortsville. I suspect the reason his books interest me is that I recognise a lot of my own motivation for pushing myself in climbing or skiing in Mark’s introspections, only watered down a thousandfold. He’d probably hate me for compromising.
Anyhow, we’re back and I’m reeling this off at the end of the first day back at work. It was never going to be good, but with a seminar to give in a week’s time, no data or slides for it and a plethora of things to do in-between, it’s not been a good ‘un. Plus, all my suspicions about crappy numerical algorithms in Rivet seem to have come true in the last week — at least Hendrick Hoeth has been paying attention and sorting out the stuff I haven’t had the time to work on. Damn, not enough time. At times like these I feel like wielding the knife myself a bit, and cutting away a bunch of the things that I’m meant to be responsible for. But it’ll pass — give it a couple of weeks and I’ll be adding more projects to the TODO list again…
Continue reading…
Posted by Andy Buckley on Feb 12, 2008
I like this bug that I found in a colon-separated path splitting function in some of our C++ code:
while (size_t pos = pathstr.find(":") != string::npos) {
// split string at position pos, i.e. the next colon...
}
I couldn’t see the bug, so I re-wrote it without the cheeky 2-in-1 assign and compare:
while (true) {
size_t pos = pathstr.find(":");
if (pos == string::npos) break;
// split string at position pos...
}
It worked immediately. What happens with the code above is that the path string always gets chopped one character at a time, rather than at the colons. This is because the != operator takes precedence over the assignment (=) operator: the first level of evaluation effectively gives while (size_t pos = true) ... as long as there’s a colon somewhere in the string. Automatic type conversion then converts the boolean true to an integer 1 and the string gets chomped 1 character at a time until no more colons are found.
Take the hint: don’t try to do two things at once, even if you can.
Posted by Andy Buckley on Feb 11, 2008
Our Durham City MP, Roberta Blackman-Woods came to visit us in the physics department earlier today, to discuss the funding crisis in particle physics and astrophysics brought on by our piss-poor research council and a government department which didn’t realise the significance of inflation or full economic costing on academic grants. They also forgot to include the VAT on the 2012 Olympics, so maybe this shouldn’t be such a big shock. The difference is that no-one said “oh well, we’ll just have to drop the 1500 metres” — the money just appeared, hundreds of times more of it than the STFC budget for the same period.
Anyway, the session wasn’t bad — our MP at least appears to be on our side and sympathetic to concerns about young researchers having their careers cut short and the knock-on impact of these cuts on physics outreach — but tellingly she didn’t actually commit to doing anything either. So here’s another letter winging its way to her courtesy of WriteToThem, sadly before I’ve had a response to the last one.
Continue reading…
Posted by Andy Buckley on Jan 25, 2008
Having read again Ian Pearson MP’s dismal response to the STFC young researchers’ letter, I finally got round to sending my MP a letter on the subject today, via WriteToThem, which is copied below. Since I’m trying to squeeze understanding this situation in among a lot of other work, I hope I haven’t got the wrong end of the stick too much. Also, apologies for the crappy English — I didn’t have time to finesse it, or find a better word than “decimate” for describing 25% cuts.
Anyway, here it is, for the record…
Continue reading…
Posted by Andy Buckley on Jan 23, 2008
As some might be aware, the Government either has a death wish for fundamental science in the UK, or is just showing its affection for it in a very strange way: cutting our funding by more than a quarter.
There’s a complex back-story to this, but sort-of-briefly:
- Back in July 2007 the Particle Physics and Astronomy Research Council (PPARC — the body that people like me get their research money and wages from) was merged with another council, CCLRC, to form the Science and Technology Research Council. STFC does the same fundamental science as PPARC did, plus a range of more industrially-connected science which needs big technology. This set off the spidey-senses of a lot of PPARC scientists, as our research doesn’t directly hit the fashionable “technology transfer” targets. But the merger was promised to be properly funded, i.e. to ensure that deficits from CCLRC wouldn’t impinge on the ex-PPARC projects, so everyone breathed a sigh of relief.
- Since at least November 2007, STFC has been aware of a Government funding deal from the Department for Industry, Universities and Skills (DIUS) which effectively gives it a “flat cash” funding projection for the next 3 years. While this looks okay, due to science research funding concurrently changing to a full economic costing (FEC) model, it will actually leave STFC with a deficit of more than 25% on the part of the budget that isn’t already ring-fenced for international subscriptions like CERN.
- Despite vigorous campaigning from scientists, the STFC has announced its “Delivery Plan” to meet this constraint, consisting of a full UK withdrawal from the International Linear Collider programme and the southern Gemini telescope, as well as an expected >10% cut in number of postdoctoral research staff (i.e. people like me). The cuts are likely to lead to closure of many university physics departments, but the Government continues to claim that it’s increasing funding, despite being perfectly aware that such a naive picture doesn’t acknowledge the effects of FEC.
There’s lots more information in these places, if your interest is piqued:
Anyway, frankly, it’s rather disappointing to start a research career and then find your subject being shut down a few years later, for the sake of what is really very little money: STFC’s deficit is £80m over 3 years, which isn’t much compared to the roughly £1.5bn per year for the Iraq war, £12bn for the dysfunctional, unwanted NHS Connecting for Health scheme, potentially £30bn over Northern Rock, £8bn, or maybe more now, for the 2012 London Olympics, probably the upper end of £6bn-£20bn for the “National Identity Management Scheme” (ID cards++) and so-on, ad infinitum. Naturally, this really makes us feel wanted; maybe “blue skies” science should have kept the Web and the MRI scanner to ourselves.
Anyway, lest you get the impression that STFC are righteously and courageously defending particle physics, astronomy and suchlike against an onslaught from a government obsessed with financial short-termism, take a look at this BBC article about STFC CEO’s testimony before the DIUS funding committee on Monday. In particular, note that PPARC science hasn’t done terribly well out of the merger: Mason cites Diamond, space science and laser research (all formerly CCLRC projects) as doing well, while “We have had to constrain some investments (particularly in the particle physics and astronomy programme)”. No shit.
Anyway, this all looks like we’re going to hell in a handcart, but good for the IOP and the RAS for doing what STFC should have done all along and proposing a sensible, affordable way to postpone these cuts until after the situation has been reviewed. As they say, “We’re talking about £20m — it’s not a terrific amount of money, in order not to allow things to go beyond the point of no return.” Hear, hear.
Continue reading…
Posted by Andy Buckley on Jan 04, 2008
I just upgraded my old (3+ years… I guess that’s old these days) 32 bit single-core Celeron PC to a 64 bit dual-core AMD X2 processor, along the way taking the opportunity to pick a replacement motherboard that does all the video, LAN, Firewire etc. things for which I was previously using extension cards. No big news, except that I’m happy to report that no OS upgrade was needed: my 32 bit Ubuntu Gutsy has neatly popped itself into 32 bit compatibility mode, so I don’t need to do a major software reinstall at a busy time. Cool!
The only thing that was a bit of a faff was getting the video output in the right mode, and that was entirely because of nVidia’s binary-only driver distribution — Ubuntu needs to be prodded (via the GUI) to enable this driver since it’s not an open source compatible way of doing things. Otherwise everything is perfect and it’s nice to have the two processor load trails appearing in my system monitor when I compile things now :-)
Posted by Andy Buckley on Jan 01, 2008
…and I don’t feel any different.” Thank you, Death Cab for Cutie, for proving once again that every occasion has a melancholy song to accompany it.
Not much to report from Buckley Towers, other than that pre-Christmas was pretty hectic, early 2008 looks set to be just as busy, and the last two weeks have been a wonderfully welcome break among friends, relatives etc. We headed off to Belfast in the car (and complete with cat) on 21st December and generally vegged in my parents’ new house for the next 9 days. Christmas itself was lovely, it was good to see people again and I even managed to see some school friends for the first time in almost 10 years. The jury is out on whether my football skills have improved at all in the last decade. We got back from Belfast just in time to see Jo’s brother Martin for an evening, brother James for an hour and then zipped down to Cambridge for an impromptu New Year’s celebration. Cracking stuff, though the mileage was pretty high… a dent in our collective resolution to be yet more ethically conscious this year :(
This break was also unusual in that they haven’t got any internet connection yet, so I had to go cold turkey for almost a fortnight. It’s good to be back in electronic contact with the world, although I haven’t yet dared look at my email. I’m now relishing the chance to sit down in front of a computer to organise things, write some code, buy things, plan buying more things, get back to work, scan another night-climbing book and write inanities (like this). Sad but true.
Posted by Andy Buckley on Dec 03, 2007
I actually wrote this about a month ago, but for various reasons it hasn’t emerged ‘til now… so if it seems hokey and out of date, I blame the posting delay rather than my intrinsic lack of insightfulness :)
Nathan recently blogged about Python’s neat approach to getter and setter methods, namely the property() constructor. This allows programmers to seamlessly “upgrade” from naive direct access to class members e.g. foo.x to contract-maintaining getter and setter functions without changing the interface, by declaring
def getX(): ...
def setX(newx): ...
x = property('x', get=getX, set=setX
This is very sweet and I’m ashamed to say that despite writing more Python than Nathan I wasn’t aware of it. (This lack of advertisement is neither new nor unique to properties — Python’s “special” method names aren’t exactly promoted in the official docs, either.) As well as (as Nathan points out) breaking the tedious routine of getter/setter boilerplate code (“just in case it becomes huge and complex later…”), first class properties also have a nice aesthetic consequence which is that there is only one standard way to access data members. While I would squirm a bit at seeing foo.getx() rather than foo.getX(), I have no problem at all with foo.x. Obviously this is stupidity on my part, but not having the room to build opposing schools of coding convention is in its way rather liberating. We’re so accustomed to seeing property access denoted by parentheses, that I find this notation rather refreshing: if used to be that you had to go to rather obscure, usually functional, languages to get such a change of syntactic scenery, so it’s nice to see the architects of a major and popular language thinking beyond the more redundant wrinkles of C-centric syntax.
On the other hand, while this is undoubtedly cool, I do still have The Fear when it comes to deploying Python on big projects. The lack of any significant pre-run type-checking still scares me and I sometimes get the feeling that weakly typed languages are a fast-track route to unmaintainable code unless some serious development discipline is wielded. On the other hand, the biggest bits of Python code I’ve written are more maintainable than they have any right to be! Ideally, Python would have some sort of “static duck-type checking”, i.e. attempting to verify that the required object methods should normally be present. That’s impossible generally, perhaps to the extent that it’s a completely dumb idea, but with C++ I find that ditching polymorphic typing via inheritance hierarchies in favour of template-based duck typing can be very handy. Of course, the downside is that the rest of C++ is evil. (Largely due to its unholy trinity of raw pointers, manual memory management and the legacy can of worms that is the C preprocessor. A recent long discussion with an experienced C++er has only reinforced my opinion that most clever C++ tricks and idioms exist to fix problems introduced by one of these three. And the innocuous looking preprocessor is responsible for a good half of those. Work I’ve done since then on heavily templatey (i.e. header-obsessive) C++ code has reinforced it even more.)
Java is much better — not only does it have a garbage collector and a compilation model without medieval overtones, but since version 1.5 we also benefit from a more powerful and subtle generics system than C++, built-in reflection and the killer metadata feature: annotations. Even more cool is the porential of the combination of Java with Jython: the Jython interpreter speaks and writes Java bytecode, meaning that true interchangeability of code objects is possible. Not only possible, but easy: Jython can run Java library classes out of the box without the need for wrapper systems like SWIG and Boost.Python. I think this really is the way of the future: a common bytecode on top of a lightweight VM allows programmers to choose appropriate languages for different tasks without either reinventing wheels or breaking a sweat. This isn’t just faciful extrapolation, either: the JVM apparently now plays host to over 200 languages including Scala, a neat functional OO language, Jython, BeanScript, PNuts and Groovy. Maybe the time of the virtual machine has come after all.
Continue reading…