A tam o' shanter is a 19th century nickname for a traditional sort of brimless, usually wool, Scottish bonnet topped with a toorie (pom-pom). It, in turn, is named after "Tam o' Shanter," the eponymous hero of the poem by the late, great Robert "Robbie" Burns written in 1790.
"Tam o' Shanter" is part of a once-popular, comic, chiefly British poetic subgenre known as the "Wild Ride." The best known example of which is Lord Byron's "Mazeppa. " A later example is William Cowper's "The Diverting History of John Gilpin."