Is failing to apply one's logic is the main cause of happening of ocd?

Not that simple. There are two main components of obessessive-compulsive disorder (a type of anxiety disorder): (1) intrusive and inappropriate thoughts, impulses, or images that cause a lot of anxiety or distress, and (2) repetitive behaviors (including mental "acts") that a person feels driven to perform in response to the obsession (or according to rigidly applied rules). There is even more to this diagnosis.
OCD is an anxiety. Disorder, often familial or seen in genetic syndromes (e.g., tourette's, williams'). Repetitive thoughts & fears recognized by the person with ocd as not reality-based (obsessions) lead to repetitive behaviors (compulsions), often unrelated to the obsessions, to reduce the anxiety temporarily. See a psychologist for cognitive-behavioral therapy & a psychiatrist for anti-anxiety medications.